Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Last active January 19, 2017 03:05
Show Gist options
  • Select an option

  • Save kevinmeredith/654a2c4da41e49c01426518e33885292 to your computer and use it in GitHub Desktop.

Select an option

Save kevinmeredith/654a2c4da41e49c01426518e33885292 to your computer and use it in GitHub Desktop.
Attempt to implement `Init` per shapeless-guide book
package net
import shapeless.{::, DepFn1, HList}
trait Init[L <: HList] extends DepFn1[L]
object Init {
type Aux[L <: HList, Out1] = Init[L] { type Out = Out1}
def apply[L <: HList](implicit ev: Init[L]): Aux[L, ev.Out] = ev
implicit def atLeast1[H, L <: HList]: Aux[H :: L, L] =
new Init[H :: L] {
type Out = L
def apply(in: H :: L): Out = in.tail
}
}
@kevinmeredith

kevinmeredith commented Jan 19, 2017 •

Copy link
Copy Markdown
Author

testing

scala> Init[Int :: HNil]
res0: net.Init[shapeless.::[Int,shapeless.HNil]]{type Out = shapeless.HNil} = net.Init$$anon$1@9d5bd06

scala> res0(4 :: HNil)
res1: res0.Out = HNil

scala> Init[Char :: String :: HNil]
res2: net.Init[shapeless.::[Char,shapeless.::[String,shapeless.HNil]]]{type Out = shapeless.::[String,shapeless.HNil]} = net.Init$$anon$1@7c7fb5a2

scala> res2('a' :: "f" :: HNil)
res3: res2.Out = f :: HNil

scala> Init[HNil]
<console>:16: error: could not find implicit value for parameter ev: net.Init[shapeless.HNil]
       Init[HNil]
           ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment