Last active
December 21, 2015 10:09
-
-
Save kylehill/6290366 to your computer and use it in GitHub Desktop.
Common functional programming patterns. From Functional Programming 101 talk at nodeDC/dcjQuery meetup, 8/22/2013
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 filter = function (array, action) { | |
| var outbound = []; | |
| for (var i = 0; i < array.length; i++) { | |
| if (action(array[i]) { | |
| outbound.push(array[i]); | |
| } | |
| } | |
| return outbound; | |
| }; | |
| var trueIfOdd = function(value) { | |
| return (value % 2 === 1); | |
| } | |
| var array = [1, 2, 3, 4]; | |
| filter(array, trueIfOdd); // returns [1, 3] |
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 find = function (array, action) { | |
| for (var i = 0; i < array.length; i++) { | |
| if (action(array[i]) { | |
| return array[i]; | |
| } | |
| } | |
| return undefined; | |
| }; | |
| var trueIfEven = function(value) { | |
| return (value % 2 === 0); | |
| } | |
| var array = [1, 2, 3, 4]; | |
| find(array, trueIfEven); // returns 2 |
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 forEach = function (array, action) { | |
| for (var i = 0; i < array.length; i++) { | |
| action(array[i]); | |
| } | |
| }; | |
| var annoyingAlert = function (value) { | |
| alert(value); | |
| } | |
| var array = [1, 2, 3, 4]; | |
| forEach(array, annoyingAlert); // alerts all values in sequence |
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 group = function (array, action) { | |
| var outbound = {}; | |
| for (var i = 0; i < array.length; i++) { | |
| var result = action(array[i]); | |
| outbound[result] = outbound[result] || []; | |
| outbound[result].push(array[i]); | |
| } | |
| return undefined; | |
| }; | |
| var awesomeDetector = function(value) { | |
| return (value.indexOf("Kyle") > -1 ? "awesome" : "still pretty cool"); | |
| } | |
| var array = ["Kyle", "John-David", "Greg", "Asa", "Edward", "Jonathan"]; | |
| group(array, awesomeDetector); | |
| /* returns | |
| { | |
| "awesome" : ["Kyle"], | |
| "still pretty cool" : ["John-David", "Greg", "Asa", "Edward", "Jonathan"] | |
| } | |
| */ | |
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 map = function (array, action) { | |
| var outbound = []; | |
| for (var i = 0; i < array.length; i++) { | |
| outbound.push(action(array[i])); | |
| } | |
| return outbound; | |
| }; | |
| var doubleValue = function(value) { | |
| return (value * 2); | |
| } | |
| var array = [1, 2, 3, 4]; | |
| map(array, doubleValue); // returns [2, 4, 6, 8] |
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 reduce = function (array, action, memory) { | |
| for (var i = 0; i < array.length; i++) { | |
| memory = action(memory, array[i]); | |
| } | |
| return memory; | |
| }; | |
| var sumValues = function(memory, value) { | |
| return (memory + value); | |
| } | |
| var array = [1, 2, 3, 4]; | |
| reduce(array, sumValues, 0); // returns 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment