Last active
October 31, 2019 12:52
-
-
Save MKRhere/f3f9139aad60c6603381ba81d69164a4 to your computer and use it in GitHub Desktop.
iter-fns
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
function* map(f, iter) { | |
for (const i of iter) { | |
yield f(i); | |
} | |
} | |
function* filter(f, iter) { | |
for (const i of iter) { | |
if (f(i)) yield i; | |
} | |
} | |
function reduce(f, iter, acc) { | |
for (const i of iter) { | |
f(i, acc); | |
} | |
return acc; | |
} | |
function collect(iter) { | |
const arr = []; | |
for (const i of iter) { | |
arr.push(i); | |
} | |
return arr; | |
} | |
console.log( | |
collect( | |
filter(x => x % 2 === 0, | |
map(x => x + 1, | |
[1, 2, 3, 4] | |
)))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment