加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在scala中创建curried匿名函数?

发布时间:2020-12-16 19:08:12 所属栏目:安全 来源:网络整理
导读:如何在 Scala中创建一个匿名和咖喱功能?以下两个失败: scala (x:Int)(y:Int) = x*yconsole:1: error: not a legal formal parameter (x:Int)(y:Int) = x*y ^scala ((x:Int)(y:Int)) = x*yconsole:1: error: not a legal formal parameter ((x:Int)(y:Int))
如何在 Scala中创建一个匿名和咖喱功能?以下两个失败:

scala> (x:Int)(y:Int) => x*y
<console>:1: error: not a legal formal parameter
       (x:Int)(y:Int) => x*y
              ^

scala> ((x:Int)(y:Int)) => x*y
<console>:1: error: not a legal formal parameter
       ((x:Int)(y:Int)) => x*y
               ^

解决方法

要创建一个curried函数,就好像它是多个函数一样(实际上是这样的情况;-)).

scala> (x: Int) => (y: Int) => x*y
res2: Int => Int => Int = <function1>

这意味着你有一个Int从Int到Int的函数.

scala> res2(3)
res3: Int => Int = <function1>

或者你可以这样写:

scala> val f: Int => Int => Int = x => y => x*y
f: Int => Int => Int = <function1>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读