Created
November 18, 2012 23:19
-
-
Save travisbrown/4108026 to your computer and use it in GitHub Desktop.
FizzBuzz in the type system (faster)
This file contains 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 shapeless._, Nat._ | |
trait FizzBuzz[N <: Nat] { | |
type R3 <: Nat | |
type R5 <: Nat | |
def ti: ToInt[N] | |
def prev: List[String] | |
def d3: R3 =:= _0 | |
def d5: R5 =:= _0 | |
def step = if (d3 != null) if (d5 != null) "FizzBuzz" else "Fizz" else | |
if (d5 != null) "Buzz" else ti().toString | |
def steps = step :: prev | |
def show = println(steps.reverse.mkString("\n")) | |
} | |
case class F[N <: Nat, N3 <: Nat, N5 <: Nat](prev: List[String])(implicit | |
val ti: ToInt[N], | |
val d3: N3 =:= _0 = null, | |
val d5: N5 =:= _0 = null | |
) extends FizzBuzz[N] { | |
type R3 = N3 | |
type R5 = N5 | |
} | |
implicit val f1 = F[_1, _1, _1](Nil) | |
implicit def fN[N <: Nat, N3 <: Nat, N5 <: Nat, R3 <: Nat, R5 <: Nat](implicit | |
nf: FizzBuzz[N] { type R3 = N3; type R5 = N5 }, | |
ti: ToInt[Succ[N]], | |
m3: ModAux[Succ[N3], _3, R3], | |
m5: ModAux[Succ[N5], _5, R5], | |
d3: R3 =:= _0 = null, | |
d5: R5 =:= _0 = null | |
) = F[Succ[N], R3, R5](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