Scala强调最小功能 [英] Scala underscore minimal function
本文介绍了Scala强调最小功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了这个问题,让我们创建一个值:
Let's create a value for the sake of this question:
val a = 1 :: Nil
现在,我可以证明匿名函数可以这样写成速记形式:
now, I can demonstrate that the anonymous functions can be written in shorthand form like this:
a.map(_*2)
是否可以编写此函数的缩写?:
is it possible to write a shorthand of this function?:
a.map((x) => x)
我的解决方案不起作用:
my solution doesn't work:
a.map(_)
推荐答案
您的第一个速记形式也可以无点书写
Your first shorthand form can also be written point-free
a map (2*)
由于乘法是可交换的.
对于(x) => x
,您需要identity
功能.这是在Predef中定义的并且是通用的,因此可以确保它是类型安全的.
As for (x) => x
, you want the identity
function. This is defined in Predef and is generic, so you can be sure that it's type-safe.
这篇关于Scala强调最小功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文