为什么 Function2 没有 andThen 方法? [英] Why doesn't Function2 have an andThen method?

查看:46
本文介绍了为什么 Function2 没有 andThen 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 andThen 只存在于 Scala 中的单参数函数?

Why does andThen only exist for single argument functions in Scala?

以下代码有效:

val double = (x: Int) => x * 2
val timesFour = double andThen double

但是为什么没有用于多参数函数的 andThen 方法?

But why is there no andThen method for multi argument functions?

val multiply = (x: Int, y: Int) => x * y
val multiplyAndDouble = multiply andThen double

<console>:10: error: value andThen is not a member of (Int, Int) => Int

添加这个方法当然是微不足道的.是否有从标准库中省略的原因?

Surely it is trivial to add this method. Is there a reason it been omitted from the standard library?

推荐答案

我刚刚注意到使用以下方法很容易解决:

I have just noticed it is easy to work around with the following:

val multiplyAndDouble = multiply.tupled andThen double
val res = multiplyAndDouble(1, 3) // res = 6

这篇关于为什么 Function2 没有 andThen 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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