Created
August 22, 2011 20:56
-
-
Save raichoo/1163522 to your computer and use it in GitHub Desktop.
Scala NList
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
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