Created
August 24, 2011 15:42
-
-
Save debasishg/1168340 to your computer and use it in GitHub Desktop.
Encoding of List using rank-3 types (Runar's code from http://paste.pocoo.org/show/463632/)
This file contains hidden or 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 scalaz._ | |
import Scalaz._ | |
type List[A] = Forall[({type f[B] = ((A, B) => B, B) => B})#f] | |
val nil: Forall[List] = new Forall[List] { | |
def apply[A] = new List[A] { | |
def apply[B] = (c, n) => n | |
} | |
} | |
val cons: Forall[({type f[x] = (x, List[x]) => List[x]})#f] = | |
new Forall[({type f[x] = (x, List[x]) => List[x]})#f] { | |
def apply[A] = (h, t) => new List[A] { | |
def apply[B] = (c, n) => c(h, t.apply(c, n)) | |
} | |
} | |
def map[A, B](f: A => B, as: List[A]): List[B] = | |
as.apply[List[B]]((h, t) => cons.apply(f(h), t), nil.apply[B]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment