Last active
November 20, 2016 11:26
-
-
Save torbjo/2c7005593718126673ac6b128fd4520e 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
// Built-in Higher Order Functions for Array: | |
// map, filter, forEach, some, every, reduce | |
// The Emperor’s New Closure: FP in Javascript | |
// https://www.youtube.com/watch?v=a6azjixVy2g | |
l = [1,2,3,4,5] | |
function is_even(n) { return n%2==0; }; // predicate | |
function larger_than(n) { return function(m) { return m > n; } }; | |
l.reduce(function (a,b) { return a+b; }); // sum(l) | |
l.filter(is_even); // [2,4] | |
l.every(function (n) { return n<6; }); // true | |
l.every(is_even); // false | |
l.filter(is_even).every(is_even) // true | |
l.some(is_even); // true | |
l.some(larger_than(4)); // true | |
l.some(larger_than(5)); // false | |
l.map(Math.sqrt) // [sqrt(1), sqrt(2), sqrt(3), sqrt(4), sqrt(5)] | |
l = '123456789'.split('') // [ '1', '2', '3', '4', '5', ...] | |
Array.prototype.sum = function() { return this.reduce(function (a,b) { return a+b; }); } | |
l.sum(); // '123456789' | |
l.sum (l.map(Number)); // 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment