Skip to content

Instantly share code, notes, and snippets.

@akiomik
Forked from travisbrown/fizzbuzz-faster.scala
Created November 17, 2014 05:51
Show Gist options
  • Save akiomik/7b4c1fd076743fb044f5 to your computer and use it in GitHub Desktop.
Save akiomik/7b4c1fd076743fb044f5 to your computer and use it in GitHub Desktop.
import shapeless._
import shapeless.Nat._
import shapeless.ops._
import shapeless.ops.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