Skip to content

Instantly share code, notes, and snippets.

@sri-rang
Created December 25, 2011 15:50
Show Gist options
  • Save sri-rang/1519448 to your computer and use it in GitHub Desktop.
Save sri-rang/1519448 to your computer and use it in GitHub Desktop.
Functional Programming in JavaScript - 5
var forEach = function (list, action) {
for (var i = 0; i < list.length; i++) {
action(list[i]);
}
};
var map = function (mappingFunction, list) {
var result = [];
forEach(list, function (item) {
result.push(mappingFunction(item));
});
return result;
};
var doubleIt = function (item) {
if (typeof item === "number") {
return item * 2;
}
};
console.log(map(doubleIt, [1, 2, 3, 4, "WTF"]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment