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
OVERVIEW
DEMO TIME!
Define FRP
Quick overview of functional programming
Dive into reactive programming
Why?!?!?
DEMO TIME!
REACTIVE
Reactive 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
FUNCTIONAL
Lambda Calculus
Category Theory
Mondas
Functor
Parametric Polymorphism
FUNCTIONAL
Lambda Calculus
Category Theory
Mondas
Functor
Parametric Polymorphism
FUNCTIONAL
‘First Class’ Functions
Pass functions into function
Declarative Programming
Compose 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.
FUNCTIONAL
var 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’ Functions
Pass functions into function
Declarative Programming
Compose methods together to create higher abstraction
Immutable Data
We 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.
REACTIVE
variables
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
REACTIVE
twitter 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
REACTIVE
twitter 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
REACTIVE
IObservable<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!
REACTIVE
IObservable<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!
REACTIVE
Events should have been lists
FRP is commonly called: Linq to events
REACTIVE
IObservable<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: detroitpro
TODO: 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
WHEN
Lets understand when to use Functional
Reactive Programming
ALWAYS!
Just kidding...
Desktop/Mobile/UI
responsive UI
anywhere that is stateful
even the world wide web
Incoming Data
events (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

Real world functional reactive programming

Editor's Notes

  • #4 FRP is not pure functional programming. Functional programming in C# is not pure functional programming.
  • #6 Show a directory searcher with retry and junk. https://gist.github.com/detroitpro/204b9874c07817fd2bea
  • #7 Pass functions into functions Compose/chain functions
  • #10 Pass functions into functions Compose/chain functions
  • #11 Pass functions into functions Compose/chain functions
  • #26 Show a directory searcher with retry and junk. https://gist.github.com/detroitpro/204b9874c07817fd2bea
  • #27 focused on describing how a program operates.
  • #28 HTML (and XAML) is an example of declarative programming, we describe what we want, the rendering engine is responsible for display/layout.
  • #34 We reverse the flow of the caller, decouple the side-effects
  • #38 We build up a linq query, but nothing happens until the console.writeline. If we remove the foreach and console.writeline no items are pulled out of the enumerable.range. This (building up monads) is a concept from functional programming. This (passing lambdas) is also a concept from functional programming. GetEnumerator is called on the list of items until it is complete. What we have here is a pull based system. Our programming is pulling items out of the list, filtering on them (that’s cool) and acting on some of them.