斯卡拉API 2.10 *:Function2.andThen发生了什么事? [英] Scala API 2.10.*: Function2.andThen what happened to?

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

问题描述

我在读«斯卡拉详解»由Joshua Suereth,本书,我已经买了作者的明确规定能力。我第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)

我把这个例子谨慎只要它显然是一个蓝印(没有具体的方法定义...)...«赶上»显然是另外一个错字只要它是一个保留字... 禽流不是实例化...

...但是,尽管例如质量不好,我不认为«故事»VAL函数组成( andThen 来定义是«反的-associative»撰写)是另一个意外的错误提供了它的的核心的例子

… 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.

有效的例子就不能编译我的斯卡拉(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)) }
} 

在该 andThen 仅由FUNCTION1支持和功能2理应消失了Function22我找到了API的简短推敲所以,问题是:

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:

什么是当前的习惯用法, andThen 撰写与元数大于1的功能* ?

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

推荐答案

我不明白的地方的例子将在所有,但这里的一些code,在斯卡拉编译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 {})

我不得不引用,因为它是一个保留字,而元组中的功能2

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

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

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