This Gist was automatically created by Carbide, a free online programming environment.
Last active
March 24, 2017 00:03
-
-
Save carbide-public/56fe5a5dd4f69aa7a7d0f37ae115f092 to your computer and use it in GitHub Desktop.
Iterables
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
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