gpt4 book ai didi

scala - 为什么 Scala 只为 Function1 实现 andThen 函数

转载 作者:行者123 更新时间:2023-12-01 09:06:27 26 4
gpt4 key购买 nike

为什么 Scala 只为 Function1(它只接受一个参数)实现方法 andThen。我看不出其他函数没有这种方法的任何原因。

下面我们有可以编译的合法代码:

val firstFunction: String => String = ???
val secondFunction: String => String = ???
firstFunction.andThen(secondFunction)

但这不会编译:

val firstFunction: (String,String) => String = ???
val secondFunction: String => String = ???
firstFunction.andThen(secondFunction)

最佳答案

I don't see any reason why rest of functions does not have such method.

因为使用 Function 将 Function2[String, String, String] 转换为 Function1[(String, String), String] 很简单.tupled:

firstFunction.tupled andThen secondFunction

这适用于任何 arity 的函数。

关于scala - 为什么 Scala 只为 Function1 实现 andThen 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45932016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com