Skip to content

Instantly share code, notes, and snippets.

@stephenjudkins
Last active January 29, 2020 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenjudkins/28ae48c73d94206e414f8d17279c4098 to your computer and use it in GitHub Desktop.
Save stephenjudkins/28ae48c73d94206e414f8d17279c4098 to your computer and use it in GitHub Desktop.
@ {
import shapeless._
import shapeless.nat._
import shapeless.ops.nat._
import shapeless.ops.hlist._
trait Fizz
trait Buzz
trait Match[N <: Nat, FB] { def s: String }
implicit def fizz[N <: Nat](implicit m: Mod.Aux[N, _3, _0]):Match[N, Fizz] = new Match[N, Fizz] { def s = "fizz" }
implicit def buzz[N <: Nat](implicit m: Mod.Aux[N, _5, _0]):Match[N, Buzz] = new Match[N, Buzz] { def s = "buzz" }
def noShow[N <: Nat, FB]:Match[N, FB] = new Match[N, FB] { def s = "" }
case class Line[N](s: String)
object Line {
implicit def instance[N <: Nat](implicit f: Match[N, Fizz] = noShow[N, Fizz], b: Match[N, Buzz] = noShow[N, Buzz], toInt: ToInt[N]):Line[N] = Line[N](s"${toInt.apply}: ${f.s}${b.s}")
}
case class Lines[N <: Nat](l: List[String])
object Lines {
implicit def instance[N <: Nat, R <: HList, H <: HList](implicit
r: Range.Aux[_1, N, R],
l: LiftAll.Aux[Line, R, H],
tl: ToList[H, Line[_]],
): Lines[N] = Lines[N](tl(l.instances).map(_.s))
}
implicitly[Lines[_16]].l.foreach(println)
}
1:
2:
3: fizz
4:
5: buzz
6: fizz
7:
8:
9: fizz
10: buzz
11:
12: fizz
13:
14:
15: fizzbuzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment