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屋!
查看全文