Created
November 25, 2013 11:50
-
-
Save psttf/7640184 to your computer and use it in GitHub Desktop.
ConstZipper zips an HList with a constant value
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._ | |
object cz { | |
trait ConstZipper[L <: HList, C] extends DepFn2[L, C] { type Out <: HList } | |
object ConstZipper { | |
type Aux[L <: HList, C, Out0 <: HList] = | |
ConstZipper[L, C] { type Out = Out0 } | |
implicit def hnilConstZipper[C]: Aux[HNil, C, HNil] = | |
new ConstZipper[HNil, C] { | |
type Out = HNil | |
def apply(l : HNil, c : C): Out = l | |
} | |
implicit def hlistConstZipper[H, T <: HList, C](implicit | |
mct : ConstZipper[T, C] | |
) = new ConstZipper[H :: T, C] { | |
type Out = (H, C) :: mct.Out | |
def apply(l : H :: T, c : C): Out = (l.head,c) :: mct(l.tail, c) | |
} | |
} | |
def constZip[L <: HList, C, Out0 <: HList](l: L, c: C)(implicit | |
icz : ConstZipper.Aux[L, C, Out0] | |
): Out0 = icz(l, c) | |
} | |
import cz._ | |
val l = 1 :: "a" :: true :: HNil | |
constZip(l, "const") |
Thank you! That really looks easier to understand. And also explains the purpose of Aux
classes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can gather the implicit evidence for a
zip
andmapConst
like this:For the record,
def constZip[L <: HList, A](l: L, a: A) = l zip (l mapConst a)
only compiled because of this bug, which was fixed last month.