Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
flatMap/monad in JavaScript
Array.prototype.flatMap = function(lambda) {
return [].concat.apply([], this.map(lambda));
};
const r = [1, 2, 3].flatMap(x => {
var arr = new Array(x);
for (var i=0; i<arr.length; ++i) {
arr[i] = x;
}
return arr;
});
console.log(r);
//output: [ 1, 2, 2, 3, 3, 3 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment