Skip to content

Instantly share code, notes, and snippets.

@carbide-public
Last active March 24, 2017 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbide-public/56fe5a5dd4f69aa7a7d0f37ae115f092 to your computer and use it in GitHub Desktop.
Save carbide-public/56fe5a5dd4f69aa7a7d0f37ae115f092 to your computer and use it in GitHub Desktop.
Iterables
const mapWith = (fn, collection) =>
({
[Symbol.iterator] () {
const iterator = collection[Symbol.iterator]();
return {
next () {
const {done, value} = iterator.next();
return ({done, value: done ? undefined : fn(value)});
}
}
}
});
const a = [1,2,3,4];
const Evens = mapWith(x => x * 2, a);
const b = [];
for (const i of Evens) {
console.log(i);
b.push(i);
}
const c = (x) => x + " hello world";
console.log(c("hellow"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment