The combine_latest operator

The combine_latest operator allows you to emit items that are composed from the latest value of the items emitted by each source observable. Here is the marble diagram of this operator:

Figure 8.6: The combine_latest operator

This operator can be used as a class method or as a member method:

Observable.combine_latest(self, *args)
Observable.combine_latest(cls, *args)

The args argument contains all the source observables that must be combined. A list can also be provided instead of several arguments. The last argument must be a function that implements the combination logic. This function takes as many input arguments as the source observable provided before.

Here is an example of how to use this operator:

numbers = Observable.from_([1, 2, 3, 4])
characters = Observable.from_(["a", "b", "c"])

characters.combine_latest(numbers, lambda c, n: c + str(n)).subscribe(
on_next = lambda i: print("on_next {}".format(i)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)

This gives the following result:

on_next a1
on_next b1
on_next b2
on_next c2
on_next c3
on_next c4
on_completed

For each combination of numbers and characters items, they are concatenated together to form a new sequence of all these combinations.

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

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