This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var reduce = fn => (Arr, acc) => Arr.reduce(fn, acc); | |
var add = reduce((acc,x)=>acc+x); | |
add([1,2,3],0);// -> 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const program = (...list) => acc => list.reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const program = (...list) => | |
acc => | |
R.flatten(list).reduce( (acc,fn) => acc.then(fn), Promise.resolve(acc)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
`; |