Scala 中的咖喱函数 [英] Curried function in scala

查看:50
本文介绍了Scala 中的咖喱函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个方法的定义:

I have a definition of next methods:

def add1(x: Int, y: Int) = x + y

def add2(x: Int)(y: Int) = x + y

第二个是第一个的咖喱版本.然后,如果我想部分应用第二个函数,我必须编写 val res2 = add2(2) _.一切安好.接下来我想要 add1 函数被柯里化.我写

the second one is curried version of first one. Then if I want to partially apply second function I have to write val res2 = add2(2) _. Everything is fine. Next I want add1 function to be curried. I write

val curriedAdd = (add1 _).curried

我认为 curriedAdd 与 add2 相似吗?但是当我尝试以这种方式部分应用 curriedAddval resCurried = curriedAdd(4) _ 我得到一个编译错误.然后我将其修复为

Am I right that curriedAdd is similiar to add2? But when I try to partially apply curriedAdd in a such way val resCurried = curriedAdd(4) _ I get a compilation error. Then I fix it to

val resCurried = curriedAdd(4)

为什么 Functions.curried 的结果与加函数的柯里化版本不同(来自 add2)?

Why the result of a Functions.curried differs from curried version of add function(from add2)?

推荐答案

首先 curriedAddadd2 _ 相同,而不是 add2.add2 只是一种方法.

Firstly curriedAdd is same as add2 _ and not add2. add2 is just a method.

scala> curriedAdd
res52: Int => (Int => Int) = <function1>

scala> add2 _
res53: Int => (Int => Int) = <function1>

关于第二个问题.我认为原因如下.做

About the second question. I think the below is the reason. Doing

scala> val i = curriedAdd(23)
i: Int => Int = <function1>

scala> i _
res54: () => Int => Int = <function0>

scala> curriedAdd(23) _
<console>:10: error: _ must follow method; cannot follow Int => Int
              curriedAdd(23) _

curredAdd(23) _ 不起作用.让我们看看 Scala 手册(第 6.7 节)-

curriedAdd(23) _ does not work. Lets look at scala manual (§6.7)-

如果 e 是方法类型或者 e 是 a按名称调用参数.如果 e 是带参数的方法,则 e _表示通过 eta 扩展(第 6.26.5 节)转换为函数类型的 e.如果 e 是类型 =>T 的无参数方法或按名称调用参数,e _ 表示 () => T 类型的函数,它在计算 e 时它应用于空参数列表().

The expression e _ is well-formed if e is of method type or if e is a call-by-name parameter. If e is a method with parameters, e _ represents e converted to a function type by eta expansion (§6.26.5). If e is a parameterless method or call-by-name parameter of type =>T , e _ represents the function of type () => T , which evaluates e when it is applied to the empty parameterlist ().

请记住,它仅评估 methodcall-by-name 参数.在 curriedAdd(23) _ 中,它不会评估 curriedAdd(23) 而是检查它是方法还是按名称调用.它既不是方法也不是按名称调用 参数.

Remember it only evaluates if it is a method or call-by-name parameter. In curriedAdd(23) _, it does not evaluate curriedAdd(23) but checks if it is a method or call-by-name. It is not a method nor a call-by-name parameter.

它不是 by-name 因为 by-name 是变量的属性.上面你在评估 curriedAdd(23) 后得到一个 by-name 参数,但 curriedAdd(23) 本身不是一个 by-名称 变量.因此出现错误(理想情况下编译器应该已经覆盖了它).请注意,以下内容有效:

It is not by-name because by-name is the property of variable. Above you get a by-name parameter after evaluating curriedAdd(23) but curriedAdd(23) in itself is not a by-name variable. Hence the error (Ideally the compiler should have coverted it). Note that the below works:

scala> curriedAdd(23)
res80: Int => Int = <function1>

scala> res80 _
res81: () => Int => Int = <function0>

上述工作是因为 res80 _,这里您将 _ 应用于 call-by-name 参数,从而进行转换.

The above works because res80 _, here you are applying _ to a call-by-name parameter and hence does the conversion.

这篇关于Scala 中的咖喱函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆