Scala 中方法、函数和部分应用函数的函数组合 [英] Function composition of methods, functions, and partially applied functions in Scala

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

问题描述

有点类似于堆栈溢出问题组合和然后方法,我一直在研究 Twitter 的 Scala School 教程并很快遇到了评论者遇到的相同问题(这很棒,因为我上床睡觉以为我的问题已解决).

Somewhat similar to Stack Overflow question Compose and andThen methods, I've been working through Twitter's Scala School tutorial and quickly ran into the same problem that a commenter had (which was great, because I went to bed thinking my problem was solved).

在教程中,它定义了两个方法:

In the tutorial, it defines two methods as such:

def addUmm(x: String) = x + " umm"
def addAhem(x: String) = x + " ahem"

而在较新版本的 Scala 中,您不能像这样调用 compose:addUmm(_).compose(addAhem(_)),已接受的答案(以及一些其他答案似乎取决于 addUmmaddAhem 是方法,而不是函数,这在尝试调用 compose 时会产生问题.我满意地睡觉了,成功了运行:

and while in newer versions of Scala, you can't call compose on them as such: addUmm(_).compose(addAhem(_)), the accepted answer (and some of the other answers seem to hinge upon the fact that addUmm and addAhem are methods, not functions, which creates an issue when trying to call compose. I went to bed satisfied, having successfully run:

scala> ((s: String) => s + " umm").compose((s: String) => s + " ahem")
res0: String => java.lang.String = <function1>

酷.问题是,虽然无法组合方法是有道理的,但当我用我知道的值计算相同的东西时,评估为 Function1:

Cool. The issue is that while not being able to compose methods makes some sense, when I the same thing with values I know evaluate to Function1:

val a = (s: String) => s + " umm"
val b = (s: String) => s + " ahem"
val c = a(_).compose(b(_))

好吧,尽管这一次它们是函数的部分应用,而不是方法,但最后一行给出了与原始问题相同的错误.原题中的一个答案(排名靠前,但不是公认的答案)似乎暗示它与部分应用程序的扩展方式有关,解释是什么?

Well, that last line coughs up the same error that the original question did, even though they're partial applications of functions this time, not methods. One of the answers in the original question (highly-ranked, but not the accepted answer) seems to hint that it has to do with how the partial application is expanded, what is the explanation?

对于 Scala 新手,无论您是否明确指定 _: String,推理器都会得到 a(_).compose(b(_)) 错误的事实两个地方,但 a.compose(b) 确实有点令人困惑.

For a Scala newbie, the fact that the inferencer gets a(_).compose(b(_)) wrong no matter if you explicitly specify _: String both places, but a.compose(b) does is somewhat confusing.

推荐答案

a(_).compose(b(_)) 扩展为 x =>{ a(x).compose(y => b(y) }.因此错误.你想要的是 (x => a(x)).compose(y =>b(y)).添加一对括号可以解决这个问题.

a(_).compose(b(_)) expands to x => { a(x).compose(y => b(y) }. Hence the error. What you want is (x => a(x)).compose(y => b(y)). Adding a pair of parentheses fixes this.

scala> (a(_)).compose(b(_: String))
res56: String => java.lang.String = <function1>

scala> res56("hello")
res57: java.lang.String = helloahemumm

但是由于 ab 是函数,您可以避免所有这些繁琐的事情,只需执行 a compose b.

But since a and b are functions, you can avoid all this cruft and simply do a compose b.

这篇关于Scala 中方法、函数和部分应用函数的函数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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