Chapter 1

Is event-driven programming only possible with some programming languages?

Event-driven programming and reactive programming can be used with virtually any programming language and any programming paradigm. However, not all programming languages are equal in their ability to write event-driven code. Usually, the more low-level the programming language, the most difficult it is to write event-driven code in it. The term low-level is used here to describe the functionalities supported by the programming languages, not the fact that it is designed for system programming rather than applicative programming. A language with no support for closures, anonymous functions, and generators will lead to event-driven code that's hard to write, read, and maintain.

What are the differences between reactive programming and reactive systems?

Reactive programming is a way to structure code that reacts to events. A reactive system is not related to the way an application is programmed, but how it behaves. A reactive system is an application or an ensemble of applications that is responsive, resilient, and elastic, based on a message-driven architecture. Reactive programming is one tool that can be used to write a reactive system.

What is an observable?

An observable is an entity that emits zero or more items. An observable has an explicit lifetime, and it can end either successfully or with an error. Once an observable is completed, it cannot send other items.

What is an observer?

An observer is an entity that receives the events emitted by an observable.

Are observables pull-based or push-based?

Observables are push-based. This is different from iterables, which are pull-based.

What makes reactivity diagrams different from activity diagrams?

UML activity diagrams are used to document a code flow, while reactivity diagrams are used to document a data flow. An activity diagram is executed only when some code calls the function or method that implements this code flow. On the other hand, when the function implementing a reactivity diagram is called, only the connection between all operators is established. The execution of each operation occurs each time a new item is emitted on an input observable.

How do you create an observable emitting integers from 0 to 10,000?

Since the from_ operator takes an iterable as an input, we can use it with any function from the Python standard library that returns a list. In this case, we can use the range function to create an observable:

Observable.from_(range(10001)) 
.subscribe(lambda i: print(i))

This example prints numbers from 0 to 10,000.

How do you create an observable from another observable where all items are multiplied by 3?

If we have an observable of integers:

items = Observable.from_([1, 2, 3, 4])

Then, creating another observable emitting these items multiplied by 3 is done with the map operator:

three_times = items.map(lambda i: i*3)
..................Content has been hidden....................

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