Книга: Learning Concurrency in Python
Назад: Operators and chaining
Дальше: Breaking it down

Filter example

Let's first take a look at how we can chain some very simple operators together in order to only print out the lengths of a string only when it is above five characters in length.

We'll take an array of five strings, map them to their lengths using the .map() operator, and then filter these for all strings less than five using the .filter() operator. We'll then have a subscriber subscribe to the mapped and filtered emissions using a simple lambda function that prints the length:

from rx import Observable

source = Observable.from_(["Alpha", "Beta", "Gamma", "Delta", "Epsilon"])

lengths = source.map(lambda s: len(s))

filtered = lengths.filter(lambda i: i >= 5)

filtered.subscribe(lambda value: print("Received {0}".format(value)))
Назад: Operators and chaining
Дальше: Breaking it down