Created
September 20, 2017 01:22
-
-
Save liorean/09ba89fb5bd958dc4def831a242cd374 to your computer and use it in GitHub Desktop.
FizzBuzz using infinite iterators
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* everysooften(num,val){ | |
let x=num; | |
while(0<--x) | |
yield; | |
yield val; | |
yield* everysooften(num,val)} | |
function* combine(...its){ | |
while(true) | |
yield its | |
.map(it=>it.next().value) | |
.reduce((a,b)=>void 0===b?a:void 0===a?b:a.concat(b),void 0)} | |
function* from(num){ | |
do yield num++ | |
while(true)} | |
function* stringify(it){ | |
let x; | |
do{ | |
x=it.next() | |
yield `${x.value}` | |
}while(!x.done)} | |
function* choose(...its){ | |
while(true) | |
yield its | |
.map(it=>it.next().value) | |
.find(it=>void 0!==it)} | |
function* take(num,it){ | |
while(num-->0) | |
yield it.next().value} | |
let | |
fizz=everysooften(3,'fizz') | |
,buzz=everysooften(5,'buzz') | |
,fizzbuzz=combine(fizz,buzz) | |
,range=stringify(from(1)) | |
,fizzbuzzes=choose(fizzbuzz,range) | |
,result=take(100,fizzbuzzes); | |
[...result].forEach(s=>console.log(s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment