Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Created November 18, 2012 19:31
Show Gist options
  • Save travisbrown/4107043 to your computer and use it in GitHub Desktop.
Save travisbrown/4107043 to your computer and use it in GitHub Desktop.
FizzBuzz in the type system
// Searching for large ModAux instances can be extremely slow.
// See this version for a faster solution: https://gist.github.com/4108026
import shapeless._, Nat._
trait FizzBuzz[N <: Nat] {
def steps: List[String]
def show = println(steps.reverse.mkString("\n"))
}
implicit object oneFizzBuzz extends FizzBuzz[_1] {
def steps = toInt[_1].toString :: Nil
}
implicit def nFizzBuzz[N <: Nat](implicit
nf: FizzBuzz[N],
ti: ToInt[Succ[N]],
m3: ModAux[Succ[N], _3, _0] = null,
m5: ModAux[Succ[N], _5, _0] = null
) = new FizzBuzz[Succ[N]] {
def steps = (
if (m3 != null && m5 != null) "FizzBuzz" else
if (m3 != null) "Fizz" else
if (m5 != null) "Buzz" else toInt[Succ[N]].toString
) :: nf.steps
}
type _100 =
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[
Succ[Succ[Succ[Succ[Succ[Succ[Succ[Succ[_22]]]]]]]]
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
implicitly[FizzBuzz[_100]].show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment