ReactiveX

57

Transcript of ReactiveX

ReactiveX

The best of functional programming and design patterns !

Agenda

● Functional Programming (Java 8)○ Getting Started○ Examples

● Observer Pattern○ Introduction○ Real life example ○ UML

● ReactiveX○ Introduction○ Getting Started○ Example○ Operators

Getting Started with Functional Programming (Java 8)

Functional Programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Functional Programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Introduction

● It is a declarative programming paradigm, which means programming is done with expressions.

● Functional code, the output value of a function depends only on the

arguments that are input to the function.

Functional Programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Lambda Expressions & Streams

Check Prime Numbers

Stream From Collection

Functional Programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Filter Stream

Functional Programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Sort Stream

- The ordering of stringCollection is untouched.

Functional programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Map Stream

Functional programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Match Stream

Match Stream

Match Stream

Functional programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Count

Functional programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Optional in Java 8

Functional programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Reduce Stream

Functional programming

● Introduction● Streams and lambda expressions● Stream operations

⚪ Filter Stream⚪ Sort Stream⚪ Map Stream ⚪ Match Stream⚪ Stream count

● Optionals● Reduce Stream● Parallel vs Sequential streams

Parallel vs Sequential StreamsSequential stream

Parallel stream

Agenda

● Functional Programming (java 8)○ Getting Started○ Examples

● Observer Pattern○ Introduction○ Real life example ○ UML

● ReactiveX○ Introduction○ Getting Started○ Example○ Operators

Observer pattern

Introduction

● Defines a “one-to-many” dependency between objects so that when one object changes state, all its dependents are notified and updated.

● Subject: the object which will frequently change its state and upon which other objects depend.

● Observer: the object which depends on a subject and updates according to its subject’s state.

Real Life Example

UML

Agenda

● Functional Programming (java 8)○ Getting Started○ Examples

● Observer Pattern○ Introduction○ Real life example ○ UML

● ReactiveX○ Introduction○ Getting Started○ Example○ Operators

ReactiveX ReactiveX is a combination of the best ideas from

the Observer pattern, the Iterator pattern, and functional programming

Introduction

● Reactive programming is a programming paradigm oriented around data flows and propagation of change.

● Let: a = b + c

what about if b or c has been changed?!

Introduction

● Functional Reactive programming (FRP) is a style of programming based on two keys ideas: continuous time-varying behaviors, and event based reactively.

● Popularized by Erik Meijer when he created the Rx library for .Net while at Microsoft.

Reactive Programming

Reactive programming is programming with asynchronous data streams.

What Are Reactive Extensions?

● It’s a library that allows you to use FRP at many programming languages.

● Allows you to represent asynchronous data streams

with “Observables”. ● And Parametrize the concurrency in those

asynchronous data streams using “Schedulers”

Rx Family

● C#: Rx.NET● Javascript: RxJS● RxJava (Java, Scala, Groovy, Clojure, Kotlin). ● Ruby: Rx.rb● Python: RxPY● More at http://reactivex.io/

Why Functional Reactive Programming?

● Writing concurrent programs correctly is difficult.

● You can transform & compose asynchronous operations.

● High-level abstraction.

● Standard error handling.

Push vs Pull

Observables and Observer

● In ReactiveX an Observer subscribes to an Observable.● Then That Observer reacts to whatever item or sequence of items the

Observable emits.● This patterns facilitate concurrent operations because it does not need

to block while waiting for the observable.

Observer onNext, onCompleted, onError

An Observer implements a subset of the following methods:

● onNext: We call this method whenever the Observable emits an item.

● onError: Call this method to indicate the the observable has failed and will not make further calls to onNext or onCompleted.

● onCompleted: Call this method after you have called onNext for the final time and if we have not encountered any errors.

The “Subscribe” method

RxJava

● It’s a JVM implementation of Reactive Extensions.

● Started at Netflix but now Open Sourced.

● Supports Java 6+ & Android 2.3+.

● Java 8 lambda support.

Creating an Observable

Creating an Observable

Creating an Observable

Transforming Observables

Filtering Observables

Filtering Observables

● first()● distinct()● take()● skip()● elementAt()● sample()● more … ● More at

https://github.com/ReactiveX/RxJava/wiki/Filtering-Observables

Aggregate Operators

Combining Observables

Combining Observables

Thanks !