The filter operator

The filter operator emits only the items of the input observable that match a predicate function. The following figure shows how this operator works:

Figure 6.5: The filter operator

The prototype of this operator is the following one:

Observable.filter(self, predicate)

The predicate argument is a function called for each item emitted on the input observable. If this predicate evaluates to true, then the item is emitted on the output observable. Otherwise, the item is dropped. The prototype of the predicate function is the following one:

predicate(item, index)

Here, item is the item to evaluate and index is the index of the item in the sequence, starting from 0. Here is an example of how to use the filter operator:

numbers = Observable.from_([1, 2, 3, 4, 5])
numbers.filter(lambda i: i > 1 and i < 4)
.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 results:

on_next 2
on_next 3
on_completed
..................Content has been hidden....................

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