Skip to content

Instantly share code, notes, and snippets.

@dvberkel
Created December 26, 2014 07:42
Show Gist options
  • Save dvberkel/df6dcb85495e33993f10 to your computer and use it in GitHub Desktop.
Save dvberkel/df6dcb85495e33993f10 to your computer and use it in GitHub Desktop.
A stab at fizzbuzz with streams
@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