Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
Created May 14, 2013 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gigamonkey/5579183 to your computer and use it in GitHub Desktop.
Save gigamonkey/5579183 to your computer and use it in GitHub Desktop.
I don't always write FizzBuzzes, but when I do, I do it without modulus.
object FizzBuzz extends App {
val nones = Stream.continually(None)
val fizzes: Stream[Option[String]] = nones.take(2) ++ Some("Fizz") #:: fizzes
val buzzes: Stream[Option[String]] = nones.take(4) ++ Some("Buzz") #:: buzzes
for (((fizz, buzz), n) <- fizzes zip buzzes zip (1 to 100)) {
println(fizz.map(_ + buzz.getOrElse("")).orElse(buzz).getOrElse(n))
}
}
@gigamonkey
Copy link
Author

Same idea but in Haskell and it fits in a Tweet:

main = mapM_ putStrLn $ zipWith max (map show [1..100]) (zipWith (++) (cycle ["","","Fizz"]) (cycle ["","","","","Buzz"]))

https://twitter.com/peterseibel/status/521883358360387584

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment