英文:
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<? extends Observable<?>> ws, FuncN<? extends R> 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
表示序列a
、b
、c
。可观察对象
Y
表示序列α
、β
、γ
。使用函数
f
将可观察对象X
和Y
进行合并,会返回一个新的可观察对象,表示序列f(a, α)
、f(b, β)
、f(c, γ)
。
如果你想要更具体的示例,
可观察对象
X
表示序列2
、3
、4
。可观察对象
Y
表示序列5
、6
、7
。使用 "multiply" 函数合并可观察对象
X
和Y
,会返回一个新的可观察对象,表示序列10
、18
、28
。
在 JavaDoc 中,"指定的合并函数" 是指函数 f
。你引用的第二部分中的 "指定的函数" 也指的是这个函数。请注意,这个函数将来自每个可观察对象的多个项(例如 2
和 5
)合并为返回的可观察对象中的一个单独项(例如 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论