Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def zipWith[A, B](lists: List[List[A]])(f: List[A] => B): List[B] = {
- @tailrec
- def loop(acc: List[B], input: List[List[A]]): List[B] = {
- val init = (Nil: List[List[A]], Nil: List[A])
- val (left, zipped) = input.foldLeft(init)((z, a) => {
- val (tails, heads) = z
- (a.tail :: tails, a.head :: heads)
- })
- if (left.foldLeft(false)(_ || _.isEmpty))
- f(zipped.reverse) :: acc
- else
- loop(f(zipped.reverse) :: acc, left.reverse)
- }
- loop(Nil, lists).reverse
- }
- test("zipWith"){
- 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"))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement