Created
December 21, 2016 06:39
-
-
Save lingsamuel/73d4ad9807dd5a0dbb013d813f68a6a3 to your computer and use it in GitHub Desktop.
aLittleJavascriptProblem
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
function *range(x, y) { | |
for (let i = x; i <= y; i++) | |
yield i; | |
} | |
function *map(generator, f) { | |
for (let x of generator) { | |
yield f(x); | |
} | |
} | |
function *reverse(generator) { | |
let a = generator.next(); | |
if (a.done === false) { | |
yield *reverse(generator); | |
yield a.value; | |
} | |
} | |
const foreach = (generator, f) => { | |
for (let x of generator) { | |
f(x); | |
} | |
}; | |
// generator is volatile, foreach is not pure | |
let num = range(1, 10); | |
num = map(num, (x => x * x)); | |
num = reverse(num); | |
foreach(num, console.log);% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment