Skip to content

Instantly share code, notes, and snippets.

@MKRhere
Last active October 31, 2019 12:52
Show Gist options
  • Save MKRhere/f3f9139aad60c6603381ba81d69164a4 to your computer and use it in GitHub Desktop.
Save MKRhere/f3f9139aad60c6603381ba81d69164a4 to your computer and use it in GitHub Desktop.
iter-fns
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