Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created October 25, 2011 10:39
Show Gist options
  • Save einblicker/1312238 to your computer and use it in GitHub Desktop.
Save einblicker/1312238 to your computer and use it in GitHub Desktop.
rank-N polymorphism
import scalaz.Forall
case class Record(x : List[Int], y : List[Double])
def lift(
f: Forall[({type X[A] = (List[A], List[A]) => List[A]})#X],
a: Record,
b: Record
): Record =
(a, b) match {
case (Record(x1, y1), Record(x2, y2)) =>
Record(f.apply(x1, x2), f.apply(y1, y2))
}
def append(a: Record, b: Record): Record =
lift(Forall[({type X[A] = (List[A], List[A]) => List[A]})#X](_(_:::_)), a, b)
val a = Record(List(1, 2, 3), List(1.0, 2.0, 3.0))
val b = Record(List(4, 5, 6), List(4.0, 5.0, 6.0))
println(append(a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment