Last active
November 22, 2016 17:57
-
-
Save jmccance/6365824ba63e52e1ea90c02386146fdc to your computer and use it in GitHub Desktop.
Monoidal Fizzbuzz in Scala
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
import scalaz._ | |
import scalaz.Scalaz._ | |
import scala.language.higherKinds | |
object Fizzbuzz extends App { | |
def nums = Stream.from(1).map(_.toString) | |
def every[A: Monoid](n: Int)(a: A): Stream[A] = | |
Stream.fill(n - 1)(Monoid[A].zero) ++ a #:: every(n)(a) | |
def fizzes = every(3)(Option("fizz")) | |
def buzzes = every(5)(Option("buzz")) | |
def pattern = fizzes.fzipWith(buzzes)(_ |+| _) | |
def fizzbuzzes = pattern.fzipWith(nums)(_.getOrElse(_)) | |
fizzbuzzes.take(150).foreach(println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment