斯卡拉强调说明 [英] Scala underscore explanation
本文介绍了斯卡拉强调说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
列表(List(List( 1,2),List(3,4),List(5))map(x =>(x.size))
我们可以缩短为:
列表(List(1,2),List(3 ,4),List(5))map((_size))
但是,如果我们有这样的事情:
列表(列表(1,2),列表(3,4),列表(5)) map(x =>(x.size,x.size))
为什么我们不能将它缩短为:
列表(列表(1,2),列表(3,4),列表(5))地图((_.size,_.size))
?
解决方案
占位符的数量应该等于函数参数的数量。在你的情况下, map
有1个参数,这就是为什么你不能使用两个占位符。
Have a look at these scala snippets: if we have something like this:
List(List(1, 2), List(3, 4), List(5)) map (x => (x.size))
we can shorten it to:
List(List(1, 2), List(3, 4), List(5)) map ((_.size))
but, if we have something like this:
List(List(1, 2), List(3, 4), List(5)) map (x => (x.size, x.size))
why can't we shorten it to:
List(List(1, 2), List(3, 4), List(5)) map ((_.size, _.size))
?
解决方案
An amount of placeholders should be equals amount of function parameters. In your case map
has 1 parameter that's why you can't use two placeholders.
这篇关于斯卡拉强调说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文