Created
November 28, 2016 15:23
-
-
Save CarlosMChica/0e76111a8eac8fce82014ac721589bc5 to your computer and use it in GitHub Desktop.
Functional FizzBuzz
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
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