Skip to content

Instantly share code, notes, and snippets.

@milessabin
Created November 17, 2014 16:57
Show Gist options
  • Save milessabin/41231349b6bc2330db9e to your computer and use it in GitHub Desktop.
Save milessabin/41231349b6bc2330db9e to your computer and use it in GitHub Desktop.
scala> :paste
// Entering paste mode (ctrl-D to finish)
sealed trait Content
case class A() extends Content
case class B() extends Content
// Exiting paste mode, now interpreting.
defined trait Content
defined class A
defined class B
scala> import ops.hlist._
import ops.hlist._
scala> def prepend[E, L <: HList](e: E)(l: L)(implicit toList: ToList[E :: L, Content]): E :: L = e :: l
prepend: [E, L <: shapeless.HList](e: E)(l: L)(implicit toList: shapeless.ops.hlist.ToList[shapeless.::[E,L],Content])shapeless.::[E,L]
scala> val l = A() :: B() :: HNil
l: shapeless.::[A,shapeless.::[B,shapeless.HNil]] = A() :: B() :: HNil
scala> prepend(A())(l)
res0: shapeless.::[A,shapeless.::[A,shapeless.::[B,shapeless.HNil]]] = A() :: A() :: B() :: HNil
scala> class Appender[E, L <: HList, Out <: HList](e: E)(l: L)(implicit prepend: Prepend.Aux[L, E :: HNil, Out], toList: ToList[Out, Content]) {
| def go: Out = (e :: HNil).:::(l)(prepend)
| }
defined class Appender
scala> def append[E, L <: HList, Out <: HList](e: E)(l: L)(implicit prepend: Prepend.Aux[L, E :: HNil, Out], toList: ToList[Out, Content]): Appender[E, L, Out] =
| new Appender(e)(l)
append: [E, L <: shapeless.HList, Out <: shapeless.HList](e: E)(l: L)(implicit prepend: shapeless.ops.hlist.Prepend.Aux[L,shapeless.::[E,shapeless.HNil],Out], implicit toList: shapeless.ops.hlist.ToList[Out,Content])Appender[E,L,Out]
scala> append(A())(l).go
res2: this.Out = A() :: B() :: A() :: HNil
scala> res2.head
res3: A = A()
scala> res2.tail.head
res4: B = B()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment