简体   繁体   English

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

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

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. 然后,该函数使用两个列表中的元素并将该函数应用于这些元素,并通过使用map和/或fold以及列表类中的函数将它们保存到列表中。

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 + 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) 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. 我的想法是压缩两个列表,然后将功能应用于压缩列表的每个元素。

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))
}

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: 通过将f移到单独的参数列表,可以大大缩短您的调用:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较 A 和 B 列表并创建一个 C 列表,其中 B 值不在 Powershell 中的 A 列表中 - Compare A and B list and create a C list with B values not in A list in Powershell scala:将List [Tuple3 [A,B,C]]转换为Foo的最短路径(A,B,List [C]) - scala: shortest way to convert List[ Tuple3[A,B,C] ] to Foo(A, B, List[C]) 检查列表 C 是否是列表 A 和列表 B 的差异 - Check if List C is the difference of List A and List B 字符串&#39;[abc]&#39;到列表python中 - string '[a b c]' into a list python 如何将列表[a,b,c]转换为python切片索引[:a,:b:c]? - How to convert list [a, b, c] to python slice index[:a, :b :c]? 转换元组列表,使 [(a,b,c)] 转换为 [(a,b),(a,c)] - Convert list of tuples such that [(a,b,c)] converts to [(a,b),(a,c)] 使用Linq c#将List <A>转换为List <B> - Convert List<A> to List<B> with Linq c# 如何使列表索引可互换,例如[&#39;a&#39;,&#39;b&#39;,&#39;c&#39;] == [&#39;b&#39;,&#39;a&#39;,&#39;c&#39;]? - How to make list indices interchangeable, as in ['a', 'b', 'c'] == ['b', 'a', 'c']? 将 list 的元素从 [['a', 'b'], ['c', 'd']] 转换为 ['a b', 'c d'] - Transform the elements of list from [['a', 'b'], ['c', 'd']] to ['a b', 'c d'] python 2.7-从a到b切成比b到c的列表, - python 2.7 - slice a list from a to b than b to c,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM