Scala : zipWith[A,B,C](f: Function2[A,B,C], l1 :Li

2019-09-02 09:20发布

问题:

So i need to implement a function that takes two lists and a function. The function then uses the elements of the two lists and applies the function on the elements and saves them into a list by using map and/or fold and the functions from the list class.

Example:

zipWith((x: Int, y: Int) => x + y, List(1, 2, 3), List(4, 5, 6)) → List(5, 7, 9)

zipWith((x:Int,y:Int) => x, List(1,2,3), List(4,5,6)) → List(1, 2, 3)

I do not know how to use the passed function and apply it on the two lists. My idea was to zip the two lists and then apply the function on every element of the zipped list.

回答1:

The elegant solution is using pattern matching:

def zipWith[A, B, C](f: (A, B) => C, l1: List[A], l2: List[B]): List[C] = {
  l1.zip(l2).map { case (x1, x2) => f(x1, x2) }
}

To solve this without pattern matching, you'll just need to access the tuple's fields directly:

def zipWith[A, B, C](f: (A, B) => C, l1: List[A], l2: List[B]): List[C] = {
  l1.zip(l2).map(tuple => f(tuple._1, tuple._2))
}


回答2:

Since you asked for a solution without pattern matching, I suggest the following one:

def zipWith[A, B, C](a: List[A], b: List[B])(f: (A, B) => C): List[C] = {
  a.zip(b).map { tuple =>
    f(tuple._1, tuple._2)
  }
}

By moving f to a separate arguments list, your invocations could be shortened significantly:

val a = List(1, 2, 3)
val b = List(4, 5, 6)

zipWith(a, b)(_ + _) // List(5, 7, 9)
zipWith(a, b)(_ * _) // List(4, 10, 18)