Created
September 5, 2019 12:20
-
-
Save frhan/c723c87a7510badb111701d82cfa9a96 to your computer and use it in GitHub Desktop.
Very Basic of Function Programming in JS
This file contains 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 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