Skip to content

Instantly share code, notes, and snippets.

@frhan
Created September 5, 2019 12:20
Show Gist options
  • Save frhan/c723c87a7510badb111701d82cfa9a96 to your computer and use it in GitHub Desktop.
Save frhan/c723c87a7510badb111701d82cfa9a96 to your computer and use it in GitHub Desktop.
Very Basic of Function Programming in JS
var stream = {
map: function (list, fn) {
var newList = [];
for (item in list) {
var mappedItem = fn(list[item]);
newList.push(mappedItem);
}
return newList;
},
filter: function (list, predicate) {
var newList = [];
for (item in list) {
if (predicate(list[item])) {
newList.push(list[item]);
}
}
return newList;
}
}
var list = [1, 3, 5, 8];
console.log("Actual Items: " + list);
var mappedItems = stream.map(list, function (item) {
return item * 3;
});
console.log("Mapped Items: " + mappedItems);
var filteredItems = stream.filter(list, function (item) {
return (item % 2) == 0;
});
console.log("Filtered Items: " + filteredItems);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment