Created
September 13, 2016 14:12
-
-
Save cyb3rD/d0c2a28057d6cfbebd1f230ff1031ebb to your computer and use it in GitHub Desktop.
Example of functional programmimg with JavaScript
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
| /** | |
| * Helper functions | |
| */ | |
| // Map array elements with functions | |
| var mapForEach = function(arr, fn) { | |
| var newArr = []; | |
| for (var i=0; i< arr.length; i++) { | |
| newArr.push( | |
| fn(arr[i]) // pass array item element to the function | |
| ); | |
| } | |
| return newArr; | |
| } | |
| var checkPastLimit = function(limiter, item) { | |
| return item > limiter; | |
| } | |
| var checkLimit = function(limiter) { | |
| // return checkPastLimit.bind(this, limiter); | |
| return function(limiter, item) { | |
| return item > limiter; | |
| }.bind(this, limiter); | |
| } | |
| /** | |
| * TESTING | |
| */ | |
| var arr1 = [1, 2, 3]; | |
| var arr2 = mapForEach(arr1, function(item) { | |
| return item * 3; | |
| }); | |
| console.log(arr2); | |
| var arr3 = mapForEach(arr1, checkPastLimit.bind(this, 1)); | |
| console.log(arr3); | |
| var arr4 = mapForEach(arr1, checkLimit(0)); | |
| console.log(arr4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment