Skip to content

Instantly share code, notes, and snippets.

@cyb3rD
Created September 13, 2016 14:12
Show Gist options
  • Select an option

  • Save cyb3rD/d0c2a28057d6cfbebd1f230ff1031ebb to your computer and use it in GitHub Desktop.

Select an option

Save cyb3rD/d0c2a28057d6cfbebd1f230ff1031ebb to your computer and use it in GitHub Desktop.
Example of functional programmimg with JavaScript
/**
* 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