Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created December 5, 2011 14:38
Show Gist options
  • Save einblicker/1433783 to your computer and use it in GitHub Desktop.
Save einblicker/1433783 to your computer and use it in GitHub Desktop.
sameLength
trait Forall3[X[_, _, _]] {
def apply[U, V, R]: X[U, V, R]
}
type ZipWith[A[_]] = {
type Apply[U, V, R] = ((U, V) => R) => (A[U], A[V]) => A[R]
}
type Result[T] = (A[T], A[T], Forall3[ZipWith[A]#Apply]) forSome { type A[_] }
def sameLength[T](xs: List[T], ys: List[T]): Option[Result[T]] = {
if (xs.length == ys.length)
Some((
xs, ys,
new Forall3[ZipWith[List]#Apply] {
def apply[U, V, R] = f => _.zip(_).map{case (a, b) => f(a, b)}
}
))
else
None
}
sameLength(List(1, 2), List(3, 4)).get match {
case (a, b, zipWith) =>
val x = zipWith[Int, Int, (Int,Int)]((_, _))(a, b)
val y = zipWith[(Int, Int), Int, ((Int, Int), Int)]((_, _))(x, b)
println(y)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment