Created
January 15, 2015 22:16
-
-
Save noelmarkham/0f500fc4daa1c2b521b7 to your computer and use it in GitHub Desktop.
Scala coding dojo: Adding one to the first parameter of a case class, when that parameter is an integer
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.ops.hlist.IsHCons | |
object ShapelessDojo { | |
def addOneToCaseClass[C, H <: HList, E, T <: HList] | |
(c: C) | |
(implicit gen: Generic.Aux[C, H], | |
h: IsHCons.Aux[H, E, T], | |
ev: E =:= Int, | |
ev2: (Int :: T) =:= H | |
): C = { | |
val hList = gen.to(c) | |
val elem = hList.head | |
val tail = hList.tail | |
val newElem = elem + 1 | |
gen.from(newElem :: tail) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment