38
Real World Functional Reactive Programming

Real world functional reactive programming

Embed Size (px)

Citation preview

Page 1: Real world functional reactive programming

Real World

Functional

Reactive Programming

Page 2: Real world functional reactive programming

Hello!I am Eric Polerecky

You can find me at:

@detroitpro

Page 3: Real world functional reactive programming

Functional

Reactive

Programming

FRP is a HUGE topic; we are only going to scratch the surface. I’m not an expert in FRP

Page 4: Real world functional reactive programming

Is Rx (Reactive Extensions) right for me?

Rx (Rx.NET)

RxJs

Bacon.JS

Ruby

RxJava

RxCpp

Python

ReactiveCocoa

reactivex.io

rxmarbles.com

introtorx.com

The introduction to Reactive

Programming you've been

missing

Paul Betts - Introduction to

Rx

reactiveui.readthedocs.org

GitHub

NetFlix

Microsoft

Page 5: Real world functional reactive programming

Lets look a trivial example

DEM

O

Page 6: Real world functional reactive programming

OVERVIEWDEMO TIME!

Define FRP

Quick overview of functional programming

Dive into reactive programming

Why?!?!?

DEMO TIME!

Page 7: Real world functional reactive programming

REACTIVEReactive programming is an emerging

discipline which combines concurrency

and event-based and asynchronous

systems.

- Wikipedia

Page 8: Real world functional reactive programming

REACTIVE

Reactive programming is programming

with asynchronous data streams.

- staltz

TODO: Drink some water

Get example of async data streams from audience

Page 9: Real world functional reactive programming

FUNCTIONALLambda Calculus

Category Theory

Mondas

Functor

Parametric Polymorphism

Page 10: Real world functional reactive programming

FUNCTIONALLambda Calculus

Category Theory

Mondas

Functor

Parametric Polymorphism

Page 11: Real world functional reactive programming

FUNCTIONAL‘First Class’ FunctionsPass functions into function

Declarative ProgrammingCompose methods together to create higher abstraction

FRP uses come functional concepts but not all. FRP can be an introduction into functional

programming.

C# has functional concepts but is not a functional language.

Page 12: Real world functional reactive programming

FUNCTIONALvar even = numberRepository()

.Select(x => x + 1)

.Where(x => x % 2 == 0)

.OrderByDescending(x=>x);

linq is functional in nature

pass functions into ‘functions’ operators

methods can be composed

Page 13: Real world functional reactive programming

FUNCTIONAL‘First Class’ FunctionsPass functions into function

Declarative ProgrammingCompose methods together to create higher abstraction

Immutable DataWe don’t change data, we project (select) it into new formats

FRP only really uses a small subset of functional concepts. The reactive part of FRP is more more

complex and mind bending.

Page 14: Real world functional reactive programming

REACTIVE

Reactive programming is programming

with asynchronous data streams.

- staltz

TODO: Drink some water

Get example of async data streams from audience

Page 15: Real world functional reactive programming

REACTIVE

Click

Click: async - if it happens, it will happen in the future.

Examples of async data streams: click, message queue, mouse move.

FRP is CONSTRUCTS for programming with async data streams.

Page 16: Real world functional reactive programming

REACTIVEvariables

user input

properties

caches

lists

Things that are not async data streams; but should have been and can be made into async data

streams.

Anything that could change (mutate) it’s value

Page 17: Real world functional reactive programming

REACTIVEtwitter feed

directory

searching

web service calls

keyboard input

file changed

Twitter feed as data stream

When you start thinking about async data streams.

EVERYTHING BECOMES AN ASYNC DATA STREAM!

list scroll

video processing

mouse move

queues

messaging

Page 18: Real world functional reactive programming

REACTIVEtwitter feed

directory

searching

web service calls

keyboard input

file changed

This mental shift is the hardest part of FRP.

list scroll

video processing

mouse move

queues

messaging

Page 19: Real world functional reactive programming

REACTIVEIObservable<T> and IObserver<T>Sometime, in the future, someone is going to give me data, how do I reason about it

IEnumerable<T> and IEnumerable<T>I want all the data now! or I’ll stand here and wait, no one can do anything until I get my

data!

Page 20: Real world functional reactive programming

REACTIVEIObservable<T> and IObserver<T>Sometime, in the future, someone is going to give me data, how do I reason about it

IEnumerable<T> and IEnumerable<T>I want all the data now! or I’ll stand here and wait, no one can do anything until I get my

data!

Page 21: Real world functional reactive programming

REACTIVEEvents should have been lists

FRP is commonly called: Linq to events

Page 22: Real world functional reactive programming

REACTIVEIObservable<T>

Linq to events

+ Time!

Page 23: Real world functional reactive programming

REACTIVE

Reactive programming is turning

everything into an asynchronous data

stream and programming with

asynchronous data streams.

- Eric Polerecky aka: detroitproTODO: Drink some water

Get example of async data streams from audience

Page 24: Real world functional reactive programming

WHY?event soup

async / await everywhere!

test-ability

control time and space!

Page 25: Real world functional reactive programming

Lets look a non-trivial example

DEM

O

Page 26: Real world functional reactive programming

DECLARATIVE

PROGRAMMING

Higher abstraction, describe what you

want

Page 27: Real world functional reactive programming

Let’s talk about declarative programming

<h2>Hello</h2>

Page 28: Real world functional reactive programming

Let’s talk about declarative programming

var even = numberRepository()

.Select(x => x + 1)

.Where(x => x % 2 == 0)

.OrderByDescending(x=>x);

Page 29: Real world functional reactive programming

Expresses the logic of a

computation without describing its

control flow

=

Declarative Programming

Page 30: Real world functional reactive programming

What is a monad?

MyObservable //event args come in, I don’t care from where

.Select(x => x.FullPath) //transform data stream

.Do(ZipFile) //the business logic that needs to happen

.Catch<string,Exception>(...)

.Retry(3) //cross cutting concerns removed from logic

.Buffer(2) //bend time and space

.Subscribe(); //make it so

Page 31: Real world functional reactive programming

IObservable

=

Reversed IEnumerable + Time

Page 32: Real world functional reactive programming

Functional Reactive Programming

=

IObservable + Declarative

Programming

Page 33: Real world functional reactive programming

Functional Reactive Programming

is to events, lists and properties

what Inversion of Control is to

dependencies

Page 34: Real world functional reactive programming

WHENLets understand when to use Functional

Reactive Programming

Page 35: Real world functional reactive programming

ALWAYS!Just kidding...

Page 36: Real world functional reactive programming

Desktop/Mobile/UIresponsive UI

anywhere that is stateful

even the world wide web

Incoming Dataevents (click events)

streams of data (sensors)

http

file system

Page 37: Real world functional reactive programming

Let’s review what we learned about FRP

■ Reverse the flow of data

■ Declarative (higher level abstraction)

■ Linq to events

■ Good for when you would work with events

■ Good for when you have state (not request-response)

■ Reactive libraries are available in every language

■ Rx is on nuget (Rx-Main)

Page 38: Real world functional reactive programming

Thanks!Any questions?

You can find me at:

@detroitpro