Skip to content

Instantly share code, notes, and snippets.

@noelmarkham
Created January 15, 2015 22:16
Show Gist options
  • Save noelmarkham/0f500fc4daa1c2b521b7 to your computer and use it in GitHub Desktop.
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
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