The distinct operator

The distinct operator returns only unique items from its source observable. The following figure shows its marble diagram:

Figure 9.11: The distinct operator

Its prototype is the following:

Observable.distinct(self, key_selector=None, comparer=None)

Here, key_selector is a function that returns a key from each item. The default value uses the item as a key. The comparer parameter is a function used to compare two keys. Its default value uses the equality operator. Both parameters are optional.

Here is an example of the distinct operator:

numbers = Observable.from_([1, 2, 1, 3, 3, 2, 4, 5])
numbers.distinct().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 example gives the following result:

on_next 1
on_next 2
on_next 3
on_next 4
on_next 5
on_completed

All duplicate items have been removed from the numbers observable.

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

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