Last active
February 26, 2019 06:29
-
-
Save gerardpaapu/6102527 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 apply = Function.prototype.apply; | |
var flatten = apply.bind([].concat, []); | |
Array.of = function (a) { | |
return [a]; | |
}; | |
Array.prototype.chain = function (f) { | |
return flatten(this.map(f)); | |
}; | |
function liftA2(f) { | |
return function (a, b) { | |
return a.chain(function (left) { | |
return b.chain(function (right) { | |
return a.constructor.of(f(left, right)); | |
}); | |
}); | |
}; | |
}; | |
var couples = liftA2(function (dude, dudette) { | |
return dude + ' + ' + dudette; | |
})( | |
['jon', 'jacob', 'jim'], | |
['rachel', 'rebecca', 'rowena'] | |
); | |
console.log(couples); // every male/female pairing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment