Lor*_*Goo 7 api scala function-composition
我正在阅读Joshua Suereth写的"Scala in Depth",这本书是我为作者明确建立的能力而购买的.我在第3页,经过一堆拼写错误和不连贯的格式化(好吧,我已经容忍了这些错误)我偶然发现了以下关于解决一个非常简单的场景的功能方法的例子.
trait Cat
trait Bird
trait Catch
trait FullTummy
def catch(hunter: Cat, prey: Bird): Cat with Catch
def eat(consumer: Cat with Catch): Cat with FullTummy
val story = (catch _) andThen (eat _)
story(new Cat, new Bird)
Run Code Online (Sandbox Code Playgroud)
我谨慎地举了这个例子,前提是它显然是一个蓝图(没有具体的方法被定义......)......«catch»显然是另一个错字,前提是它是一个保留字...... Cat并且Bird不可实例化......
......但是,尽管这个例子的质量很差,但我不能认为根据功能构成定义的«故事»val(andThen是"反向关联" compose)是另一个意外的错误,前提是它是这个例子的核心.
实际上,该示例不会在我的本地版本的Scala(2.10.1)上编译,并且在最新版本(2.10.2)上也没有记录.
毫无疑问它的用处和实现很容易实现(如下):
trait Function2ex[-T1, -T2, +R] extends Function2[T1, T2, R] {
def andThen[A](g: R => A): (T1, T2) => A = { (x, y) => g(apply(x, y)) }
}
Run Code Online (Sandbox Code Playgroud)
经过对API的简短审查后,我发现andThen只有Function1支持,并且假设从Function2消失到Function22所以,问题是:
什么是当前的习惯用法,andThen并compose与元数大于1的功能*?
我不明白这个例子到底在哪里,但这里有一些代码在scala 2.10.2中编译.
trait Cat
trait Bird
trait Catch
trait FullTummy
def `catch`(hunter: Cat, prey: Bird): Cat with Catch = ???
def eat(consumer: Cat with Catch): Cat with FullTummy = ???
val story = (`catch` _).tupled andThen (eat _)
story(new Cat with Catch, new Bird {})
Run Code Online (Sandbox Code Playgroud)
我不得不引用catch它,因为它是一个保留字,而且是元组Function2.
| 归档时间: |
|
| 查看次数: |
613 次 |
| 最近记录: |