-
-
Save tabrez/b86bb73baf2dde24b7c5 to your computer and use it in GitHub Desktop.
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
// zip3 Lists | |
object ZipUtils { | |
def zip3[A, B, C] (xs: List[A], ys: List[B], zs: List[C]) : List[(A, B, C)] = { | |
def zip3_aux(xs: List[A], ys: List[B], zs: List[C], | |
acc: List[(A, B, C)]) : List[(A, B, C)] = (xs, ys, zs) match { | |
case (xh :: xt, yh :: yt, zh :: zt) => zip3_aux (xt, yt, zt, (xh, yh, zh) :: acc ) | |
case _ => acc.reverse | |
} | |
zip3_aux(xs, ys, zs, Nil) | |
} | |
def zipWith [A,B,C] (f: (A, B) => C) (xs: List[A], ys: List[B]) : List[C] = { | |
xs zip ys map { case (x, y) => f(x, y) } | |
} | |
// zip3 Vectors | |
def zip3[A, B, C] (xs: Vector[A], ys: Vector[B], zs: Vector[C]) : Vector[(A, B, C)] = { | |
val l = xs.length min ys.length min zs.length | |
def zipaux (v: Int, acc: Vector[(A, B, C)]) : Vector[(A, B, C)] = | |
if (v == 0) acc | |
else zipaux(v - 1, acc :+ ( xs(l - v), ys(l - v), zs(l - v) )) | |
zipaux (l, Vector.empty) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment