Scala : zipWith[A,B,C](f: Function2[A,B,C], l1 :List[A], l2 :List[B]) : List[C] Method

佐手、 提交于 2020-01-25 21:31:23

问题


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)


来源:https://stackoverflow.com/questions/37333957/scala-zipwitha-b-cf-function2a-b-c-l1-lista-l2-listb-listc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!