小编g85*_*588的帖子

压缩集合上的Scala函数文字和占位符语法

我目前正在学习Scala,并且一直在努力在zipped集合上使用占位符语法.例如,我想从l2 [i]> = l1 [i]的项目中过滤压缩数组.如何使用显式函数文字或占位符语法执行此操作?我试过了:

scala> val l = List(3, 0, 5) zip List(1, 2, 3)
l: List[(Int, Int)] = List((3,1), (4,2), (5,3))

scala> l.filter((x, y) => x > y)
<console>:9: error: missing parameter type
Note: The expected type requires a one-argument function accepting a 2-Tuple.
      Consider a pattern matching anonymous function, `{ case (x, y) =>  ... }`
              l.filter((x, y) => x > y)
                        ^
<console>:9: error: missing parameter type
              l.filter((x, y) => x > y)

scala> …
Run Code Online (Sandbox Code Playgroud)

functional-programming scala function-literal pattern-matching

2
推荐指数
1
解决办法
447
查看次数