Skip to content

Instantly share code, notes, and snippets.

@CarlosMChica
Created November 28, 2016 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosMChica/0e76111a8eac8fce82014ac721589bc5 to your computer and use it in GitHub Desktop.
Save CarlosMChica/0e76111a8eac8fce82014ac721589bc5 to your computer and use it in GitHub Desktop.
Functional FizzBuzz
class FizzBuzz {
private val fizzes = cycle("", "", "Fizz")
private val buzzes = cycle("", "", "", "", "Buzz")
private val fizzBuzzes = fizzes.zip(buzzes).map(x => x._1 ++ x._2)
private lazy val fizzBuzz = fizzBuzzes.zip(Stream.from(1)).map(x => if (x._1.isEmpty) x._2.toString else x._1)
def convert(x: Int): String = {
convert(1 to x)(x - 1)
}
def convert(input: Range): Seq[String] = {
fizzBuzz.take(input.end)
}
def cycle[T](a: T*) = Stream.continually(a.toSeq).flatten
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment