如何在 Java 8 中使用 Observable zip

How to use Observable zip in Java 8

最近遇到一些使用了以下函数的代码,但是对如何使用这个函数有点费解。

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

对于java doc,它说的是"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."。我读了好几遍,还是不明白它的意思。

而且我还读到 zip 函数本身意味着 "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"。

我本来以为我知道 zip 是做什么的,但是看完这个我就糊涂了。 zip 函数不是只会 return 一个值吗,为什么它是“...对于基于此函数结果的每个组合”?

任何人都可以用简单的术语和示例帮助说明上述功能以及 zip 的一般含义吗?

这是一个例子:

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, γ).

如果你想要一个更具体的例子,

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.

在 JavaDoc 中,"a specified combiner function" 指的是函数 f。您引用的第二件事中的短语 "a specified function" 也指的是这一点。请注意此函数如何组合多个项目,一个来自每个可观察对象(例如 25)到返回的可观察对象中的一个项目(例如 10)。

JavaDoc 和您引用的第二件事中的 "combination" 指的是您正在压缩的每个可观察对象的项目元组,例如 (2, 5)(3, 6).

请注意,zip returns 一个单独的 Observable 对象,它表示值的 序列 。 JavaDoc 描述了这个单一的 Observable 对象,而你引用的第二件事描述了 表示的序列 Observable 对象(因此措辞 "for each combination") .这可能就是为什么您将第二个摘录误解为 zip returns 多个值的原因。两个摘录都说的是实话,但他们描述的是不同的东西。

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