The zipWith operator

The instance version (that is, the copy of the function, which should be called with an instance rather than static) of the zip operator is zipWith, which can be called from the Observable instance itself. The only problem with this version is that you can pass only another source Observable. If you need to work with three or more Observable instances, you should rather consider using the zip operator instead of zipWith.

Here's an example:

    fun main(args: Array<String>) { 
      val observable1 = Observable.range(1,10) 
      val observable2 = listOf("String 1","String 2","String 3",
"String 4","String 5","String 6","String 7","String 8",
"String 9","String 10").toObservable() observable1.zipWith(observable2,{e1:Int,e2:String ->
"$e2 $e1"})//(1) .subscribe { println("Received $it") } }

The output is as follows:

On comment (1), we used the zipWith operator on the Observable instance, observable1, and passed another Observable instance, observable2, to it with a lambda to apply to the emissions. From the output, we can tell that the zipWith operator accumulates the producer it's subscribed to, with the producer it is provided with.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.198.94