Saturday, July 14, 2018

Reactive Programming

Reactive Programming


Reactive programming is a programming paradigm that deals with data flows and the propagation of change. It means that when a data flow is emitted by one component, the change will be propagated to other components by reactive programming library. The propagation of change will continue until it reaches the final receiver. The difference between event-driven and reactive programming is that event-driven programming revolves around events and reactive programming revolves around data.

ReactiveX or RX for reactive programming

ReactiveX or Raective Extension is the most famous implementation of reactive programming. The working of ReactiveX depends upon the following two classes −

Observable class

This class is the source of data stream or events and it packs the incoming data so that the data can be passed from one thread to another. It will not give data until some observer subscribe to it.

Observer class

This class consumes the data stream emitted by observable. There can be multiple observers with observable and each observer will receive each data item that is emitted. The observer can receive three type of events by subscribing to observable −
  • on_next() event − It implies there is an element in the data stream.
  • on_completed() event − It implies end of emission and no more items are coming.
  • on_error() event − It also implies end of emission but in case when an error is thrown by observable.

RxPY – Python Module for Reactive Programming

RxPY is a Python module which can be used for reactive programming. We need to ensure that the module is installed. The following command can be used to install the RxPY module −
pip install RxPY

Example

Following is a Python script, which uses RxPY module and its classes Observable and Observe for reactive programming. There are basically two classes −
  • get_strings() − for getting the strings from observer.
  • PrintObserver() − for printing the strings from observer. It uses all three events of observer class. It also uses subscribe() class.
from rx import Observable, Observer
def get_strings(observer):
   observer.on_next("Ram")
   observer.on_next("Mohan")
   observer.on_next("Shyam")
      observer.on_completed()
class PrintObserver(Observer):
   def on_next(self, value):
      print("Received {0}".format(value))
   def on_completed(self):
   print("Finished")
   def on_error(self, error):
      print("Error: {0}".format(error))
source = Observable.create(get_strings)
source.subscribe(PrintObserver())

Output

Received Ram
Received Mohan
Received Shyam
Finished

PyFunctional library for reactive programming

PyFunctionalis another Python library that can be used for reactive programming. It enables us to create functional programs using the Python programming language. It is useful because it allows us to create data pipelines by using chained functional operators.

Difference between RxPY and PyFunctional

Both the libraries are used for reactive programming and handle the stream in similar fashion but the main difference between both of them depends upon the handling of data. RxPY handles data and events in the system while PyFunctional is focused on transformation of data using functional programming paradigms.

Installing PyFunctional Module

We need to install this module before using it. It can be installed with the help of pip command as follows −
pip install pyfunctional

Example

Following example uses the PyFunctional module and its seq class which act as the stream object with which we can iterate and manipulate. In this program, it maps the sequence by using the lamda function that doubles every value, then filters the value where x is greater than 4 and finally it reduces the sequence into a sum of all the remaining values.
from functional import seq

result = seq(1,2,3).map(lambda x: x*2).filter(lambda x: x > 4).reduce(lambda x, y: x + y)

print ("Result: {}".format(result))

Output

Result: 6

No comments:

Post a Comment