real world functional reactive programming
Post on 14-Jul-2015
345 Views
Preview:
TRANSCRIPT
Real World
Functional
Reactive Programming
Hello!I am Eric Polerecky
You can find me at:
@detroitpro
Functional
Reactive
Programming
FRP is a HUGE topic; we are only going to scratch the surface. I’m not an expert in FRP
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
Lets look a trivial example
DEM
O
OVERVIEWDEMO TIME!
Define FRP
Quick overview of functional programming
Dive into reactive programming
Why?!?!?
DEMO TIME!
REACTIVEReactive programming is an emerging
discipline which combines concurrency
and event-based and asynchronous
systems.
- Wikipedia
REACTIVE
Reactive programming is programming
with asynchronous data streams.
- staltz
TODO: Drink some water
Get example of async data streams from audience
FUNCTIONALLambda Calculus
Category Theory
Mondas
Functor
Parametric Polymorphism
FUNCTIONALLambda Calculus
Category Theory
Mondas
Functor
Parametric Polymorphism
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.
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
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.
REACTIVE
Reactive programming is programming
with asynchronous data streams.
- staltz
TODO: Drink some water
Get example of async data streams from audience
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.
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
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
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
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!
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!
REACTIVEEvents should have been lists
FRP is commonly called: Linq to events
REACTIVEIObservable<T>
Linq to events
+ Time!
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
WHY?event soup
async / await everywhere!
test-ability
control time and space!
Lets look a non-trivial example
DEM
O
DECLARATIVE
PROGRAMMING
Higher abstraction, describe what you
want
Let’s talk about declarative programming
<h2>Hello</h2>
Let’s talk about declarative programming
var even = numberRepository()
.Select(x => x + 1)
.Where(x => x % 2 == 0)
.OrderByDescending(x=>x);
“
Expresses the logic of a
computation without describing its
control flow
=
Declarative 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
“
IObservable
=
Reversed IEnumerable + Time
“
Functional Reactive Programming
=
IObservable + Declarative
Programming
“
Functional Reactive Programming
is to events, lists and properties
what Inversion of Control is to
dependencies
WHENLets understand when to use Functional
Reactive Programming
ALWAYS!Just kidding...
Desktop/Mobile/UIresponsive UI
anywhere that is stateful
even the world wide web
Incoming Dataevents (click events)
streams of data (sensors)
http
file system
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)
Thanks!Any questions?
You can find me at:
@detroitpro
top related