Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. def zipWith[A, B](lists: List[List[A]])(f: List[A] => B): List[B] = {
  2. @tailrec
  3. def loop(acc: List[B], input: List[List[A]]): List[B] = {
  4. val init = (Nil: List[List[A]], Nil: List[A])
  5. val (left, zipped) = input.foldLeft(init)((z, a) => {
  6. val (tails, heads) = z
  7. (a.tail :: tails, a.head :: heads)
  8. })
  9. if (left.foldLeft(false)(_ || _.isEmpty))
  10. f(zipped.reverse) :: acc
  11. else
  12. loop(f(zipped.reverse) :: acc, left.reverse)
  13. }
  14.  
  15. loop(Nil, lists).reverse
  16. }
  17.  
  18. test("zipWith"){
  19. assert(zipWith(List(List(1,2,3), List(4,5,6), List(7,8,9)))(_.mkString("::")) === List("1::4::7", "2::5::8", "3::6::9"))
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement