Skip to content

Instantly share code, notes, and snippets.

View dtipson's full-sized avatar

Drew dtipson

View GitHub Profile
@dtipson
dtipson / IO plus Array & Promise helpers.js
Last active October 1, 2019 07:56
Bare bones FP type utility lib so we can play around with functions that capture the composition of DOM read/writes, but in a pure way
// Let's make it possible to create pure functions even when we're
// dealing with impure operations that would have side effects!
// First we'll need a "Type" that can contain a (sometimes impure) function
function IO(fn) {
if (!(this instanceof IO)) {//make it simpler for end users to create a type without "new"
return new IO(fn);
}
this.runIO = fn;//IO now provides an extra control layer that allows the composition of unexecuted effects
@dtipson
dtipson / send-postMessage-events.js
Created September 25, 2018 19:45
Sending postMessage events
function throttle(fn, threshhold = 250, scope) {
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
@dtipson
dtipson / Partially applied reduce with arrow functions.js
Created November 5, 2015 16:45
Partially applied reduce with arrow functions.js
var reduce = fn => (Arr, acc) => Arr.reduce(fn, acc);
var add = reduce((acc,x)=>acc+x);
add([1,2,3],0);// -> 6
@dtipson
dtipson / Silly program example 2.js
Last active March 26, 2018 09:39
Silly program example #2
getUserCommentsbyId = program(
getUserbyIdAPI, // <-aysnc
pick('commenterID'), // <-sync
commentSearchAPI, // <-aysnc
R.head, // <-sync
R.tap(console.log.bind(console)) // <-sync, logs result to console
);
// these each return a promise resolved with a user's latest comment based on a userID
getUserCommentsbyId(345);
//returns a promise that resolves after a time
function wait(time){
return new Promise(function(resolve){
window.setTimeout(resolve,time);
});
}
//a function that, if called with just a time,
//returns a function that will return a wait...
//that resolves with whatever value it was given
@dtipson
dtipson / program in one line.js
Created November 7, 2015 21:30
ES2015 version of program
const program = (...list) => acc => list.reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc));
@dtipson
dtipson / quick-stream-self.js
Last active July 26, 2017 19:44
Can run in console on any https site on Chrome/Firefox
document.body.innerHTML = '';
const delay = milliseconds => x => new Promise(resolve => setTimeout(resolve, milliseconds, x));
//requestRecord :: Object (optional) -> Promise Stream
const requestRecord = (config={video:true, audio:true}) => {
return navigator.mediaDevices && navigator.mediaDevices.getUserMedia ?
navigator.mediaDevices.getUserMedia(config).then(delay(1400)) : // delay avoid startup flash
Promise.reject('no support for getUserMedia');
@dtipson
dtipson / program in one line plus flatten.js
Created November 7, 2015 21:35
Program in one line plus flatten
const program = (...list) =>
acc =>
R.flatten(list).reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc));
// mapping over an array with an async function
// would return an Array of Promises which isn't
// super useful to work with on its own
async function fooPromise(x){
return Promise.resolve(x*3);
}
const arr = [6,7,8,9];// -> [Promise[18],Promise[21],Promise[24],Promise[27]]]
@dtipson
dtipson / inlineFuncAsWorker.js
Last active June 5, 2017 09:41
Another simple implementation of pushing a potentially costly operation off to a worker thread
//main function for creating an inline worker:
//inlineWorker:: Function -> a -> Promise b
const inlineWorker = fn => msg => {
const scriptString = `const func = ${fn.toString()};
addEventListener('message', function(e) {
Promise.resolve(e.data)
.then(func)
.then(postMessage);
}, false);
`;