Skip to content

Instantly share code, notes, and snippets.

@gerardpaapu
Last active February 26, 2019 06:29
Show Gist options
  • Save gerardpaapu/6102527 to your computer and use it in GitHub Desktop.
Save gerardpaapu/6102527 to your computer and use it in GitHub Desktop.
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