Scala API 2.10.*:Function2.and然后发生了什么? [英] Scala API 2.10.*: Function2.andThen what happened to?

查看:20
本文介绍了Scala API 2.10.*:Function2.and然后发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Joshua Suereth 的 «Scala in Depth»,这本书是我为了作者明确确立的能力而购买的.我在第 3 页,在经历了一堆拼写错误和不连贯的格式(好吧,我已经可以容忍这些错误)之后,我偶然发现了以下示例,该示例是关于解决一个非常简单的场景的函数式方法.

I'm reading «Scala in Depth» by Joshua Suereth, book that I've bought for the clearly established competency of the author. I'm on page 3 and after a bunch of typos and incoherent formatting (ok, I've become tolerant of these errors) I stumbled upon the following example about a functional approach to solve a very simple scenario.

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)

我谨慎地举了这个例子,前提是它显然是一个蓝图(没有定义具体的方法......)...... «catch»显然是另一个错字,只要它是一个保留字...... CatBird 不可实例化......

I took the example with caution provided it's clearly a blue-print (no concrete methods are defined…)… «catch» is clearly another typo provided it's a reserved word… Cat and Bird are not instantiable…

...但是,尽管示例的质量很差,但我不能认为 «story» val 是根据函数组合定义的(andThen 的 «reverse-associative»>compose) 是另一个偶然的错误,前提是它是示例的核心.

… but, despite the poor quality of the example, I can't consider that the «story» val defined in terms of function composition (andThen is the «reverse-associative» of compose) is another accidental mistake provided it's the core of the example.

实际上,该示例无法在我的本地 Scala 版本 (2.10.1) 上编译,并且在可用的最新版本 (2.10.2) 中也没有记录.

Effectively the example won't compile on my local version of Scala (2.10.1) and it's not documented either on the latest version available (2.10.2).

毫无疑问它的用处和它的实现很容易实现(如下):

There is no doubt of its usefulness and that its implementation is easy to accomplish (follow):

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)) }
} 

在对 API 进行简短审查后,我发现 andThen 仅由 Function1 支持,并且据说从 Function2 到 Function22 中消失了,所以问题是:

After a short scrutiny of the API I found that the andThen is supported only by Function1 and supposedly disappeared from Function2 to Function22 so, the question is:

支持andThencompose的函数*的元数大于1的当前习语是什么?

What is the current idiom to support andThen and compose with Function* of arity greater than 1?

推荐答案

我完全不明白那个例子的去向,但这里有一些在 Scala 2.10.2 中编译的代码.

I don't understand where that example is going at all, but here's some code that compiles in 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 {})

我不得不引用 catch 因为它是一个保留字,并将 Function2 元组化.

I had to quote catch because it's a reserved word, and tuple the Function2.

这篇关于Scala API 2.10.*:Function2.and然后发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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