Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Scala NList
package nlist
sealed trait Nat
sealed trait Z extends Nat
sealed trait S[A <: Nat] extends Nat
sealed trait NList[A <: Nat, +B] {
type Length = A
def zap[C](l: NList[Length, B => C]): NList[Length, C]
}
case object NNil extends NList[Z, Nothing] {
def zap[A](l: NList[Length, Nothing => A]): NNil.type =
NNil
}
sealed case class NCons[A <: Nat, B](head: B, tail: NList[A, B])
extends NList[S[A], B] {
def zap[C](l: NList[Length, B => C]): NList[Length, C] =
l match {
case NCons(f, fs) =>
NCons(f(head), tail zap fs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment