Skip to content

Instantly share code, notes, and snippets.

@raichoo
Created August 22, 2011 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raichoo/1163522 to your computer and use it in GitHub Desktop.
Save raichoo/1163522 to your computer and use it in GitHub Desktop.
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