Created
December 26, 2014 07:42
-
-
Save dvberkel/df6dcb85495e33993f10 to your computer and use it in GitHub Desktop.
A stab at fizzbuzz with streams
This file contains hidden or 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
@Grab('com.bloidonia:groovy-stream:0.9.0') | |
import groovy.stream.* | |
def (n, f, b) = [ | |
{ [ it[0], it[1] + '' ] }, | |
{ [ it[0], it[1] + 'fizz' ] }, | |
{ [ it[0], it[1] + 'buzz' ] } | |
] | |
def fizzs = Stream.from([f, n, n]).repeat() | |
def buzzs = Stream.from([b, n, n, n, n]).repeat() | |
def fizzbuzzs = fizzs.zip(buzzs) { u, v -> [u, v] } | |
def s = Stream.from(0..100).zip(fizzbuzzs) { | |
index, fs -> fs.inject([index, '']) { r, g -> g(r) } | |
} | |
def t = s.map { tuple -> tuple[1].size() > 0 ? tuple[1] : tuple[0] } | |
t.each { println it } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment