Skip to content

Instantly share code, notes, and snippets.

@mocheng
Created September 23, 2016 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mocheng/9726d24242d39a8e7b8124e66c5294bb to your computer and use it in GitHub Desktop.
Save mocheng/9726d24242d39a8e7b8124e66c5294bb to your computer and use it in GitHub Desktop.
Applicative in JavaScript
Array.prototype.ap = function(wrappedVals) {
var results = [];
this.map( (f)=> {
const mr = wrappedVals.map(f);
results = results.concat(mr);
});
return results;
}
const r = [x=>x*2, y=>y+3].ap([1,2,3]);
console.log(r);
//output: [ 2, 4, 6, 4, 5, 6 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment