如何在Java 8中使用Observable的zip功能

huangapple go评论64阅读模式
英文:

How to use Observable zip in Java 8

问题

最近我遇到了一些使用以下函数的代码,但在理解如何使用该函数方面遇到了困难。

zip(java.lang.Iterable<? extends Observable<?>> ws, FuncN<? extends R> zipFunction)

根据Java文档,它的意思是“返回一个Observable,该Observable发射由应用于其他Observables的Iterable中的项目的组合的指定组合函数的结果,按顺序发射。” 我已经多次阅读了它,但仍然不太明白它的含义。

我还读到zip函数本身的意思是“通过指定的函数将多个Observables的发射合并在一起,并基于此函数的结果为每个组合发射单个项目”。

我最初以为我知道zip函数是做什么的,但在阅读这个之后,我感到困惑。zip函数难道不只会返回一个值吗?为什么会是“...基于此函数的结果为每个组合发射单个项目”?

有人可以用简单的术语和示例来说明上述函数以及zip函数的一般含义吗?

英文:

Recently I have come across some codes which use the following function but have difficultly in understanding how to use this function.

zip(java.lang.Iterable&lt;? extends Observable&lt;?&gt;&gt; ws, FuncN&lt;? extends R&gt; zipFunction)

For java doc, it said it means "Returns an Observable that emits the results of a specified combiner function applied to combinations of items emitted, in sequence, by an Iterable of other Observables.". I have read it several times but still couldn't get what it means.

And I also read that the zip function itself means "combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function".

I originally thought I know what zip does, but after reading this, I get confused. Isn't the zip function will return one value only, why it is "...for each combination based on the results of this function" ?

Anyone could help to illustrate in simple terms and examples what the above function and also the zip in general mean?

答案1

得分: 1

这里有一个示例:

可观察对象 X 表示序列 abc

可观察对象 Y 表示序列 αβγ

使用函数 f 将可观察对象 XY 进行合并,会返回一个新的可观察对象,表示序列 f(a, α)f(b, β)f(c, γ)

如果你想要更具体的示例,

可观察对象 X 表示序列 234

可观察对象 Y 表示序列 567

使用 "multiply" 函数合并可观察对象 XY,会返回一个新的可观察对象,表示序列 101828

在 JavaDoc 中,"指定的合并函数" 是指函数 f。你引用的第二部分中的 "指定的函数" 也指的是这个函数。请注意,这个函数将来自每个可观察对象的多个项(例如 25)合并为返回的可观察对象中的一个单独项(例如 10)。

JavaDoc 和你引用的第二部分中的 "组合" 都指的是来自要合并的每个可观察对象的项目元组,例如 (2, 5)(3, 6)

请注意,zip 返回一个单独的 Observable 对象,表示一个值的序列。JavaDoc 描述了这个单独的 Observable 对象,而你引用的第二部分描述了 Observable 对象所代表的序列(因此使用了 "对于每个组合" 这样的措辞)。这可能是你误解第二部分内容,以为它在说 zip 返回多个值。这两个引用都是正确的,但它们描述的是不同的内容。

我重申一遍:zip 返回一个代表值序列的 单个 Observable 对象。

英文:

Here's a example:

> Observable X represents the sequence a, b, c.
>
> Observable Y represents the sequence α, β, γ.
>
> Zipping the observables X and Y using the function f returns a
> new observable that represents the sequence f(a, α), f(b, β),
> f(c, γ).

If you want an even more concrete example,

> Observable X represents the sequence 2, 3, 4.
>
> Observable Y represents the sequence 5, 6, 7.
>
> Zipping the observables X and Y using the "multiply" function returns a
> new observable that represents the sequence 10, 18,
> 28.

In the JavaDoc, "a specified combiner function" refers to the function f. The phrase "a specified function" in the second thing that you quoted also refers to this. Notice how this function combines multiple items, one from each observable, (e.g. 2 and 5) to one single item in the returned observable (e.g. 10).

A "combination" in both the JavaDoc and the second thing you quoted refers to tuples of items from each observable that you are zipping, such as (2, 5) or (3, 6).

Note that zip returns one single Observable object, which represents a sequence of values. The JavaDoc describes this single Observable object, whereas the second thing you quoted describes the sequence represented by the Observable object (hence the wording "for each combination"). This could be why you have misunderstood the second excerpt as saying that zip returns multiple values. Both excerpts are telling the truth, but they are describing different things.

I reiterate: zip returns a single Observable object that represents a sequence of values.

huangapple
  • 本文由 发表于 2020年5月5日 19:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定