Last active
January 29, 2020 00:18
-
-
Save stephenjudkins/28ae48c73d94206e414f8d17279c4098 to your computer and use it in GitHub Desktop.
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._ | |
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