gpt4 book ai didi

scala - 为什么 Function2 没有 andThen 方法?

转载 作者:行者123 更新时间:2023-12-03 20:53:28 24 4
gpt4 key购买 nike

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

以下代码有效:

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

但是为什么没有 andThen多参数函数的方法?
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

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

最佳答案

我不知道为什么Function2不供应和andThen ,但 Scalaz 定义了 Functor各种元函数的实例,其中map相当于 andThen ,这意味着你可以写

val multiplyAndDouble = multiply map double

关于scala - 为什么 Function2 没有 andThen 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21680117/

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