Created
September 23, 2016 06:52
-
-
Save mocheng/29af75aeca49b88b7ce866c83eb3c481 to your computer and use it in GitHub Desktop.
flatMap/monad in JavaScript
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
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