SlideShare a Scribd company logo
The Essence
of Reactive
Programming
A Theoretical Approach
Eddy Bertoluzzo
What does it mean to be Reactive?
● Reactive Programming - Wikipedia
Let’s ask Google
Reactive Programming - Wikipedia
● Reactive Programming - Wikipedia
● ReactiveX - Rx*
● Functional Reactive Programming
Let’s ask Google
Reactive Programming
???
Functional Reactive Programming
Reactive Programming
!=
Functional Reactive Programming
● Reactive Programming - Wikipedia
● ReactiveX - Rx*
● Functional Reactive Programming
● Reactive Manifesto - Reactive
Streams
Let’s ask Google
The essence of Reactive Programming
● Reactive Programming - Wikipedia
● ReactiveX - Rx*
● Functional Reactive Programming
● Reactive Manifesto - Reactive
Streams
Let’s ask Google
The essence of Reactive Programming
Quoting Leslie
Lamport
“There’s something about the culture
of software that has impeded the use
of specification.
We have a wonderful way of
describing things precisely that’s
been developed over the last couple
of millennia, called mathematics.
I think that’s what we should be
using as a way of thinking about
what we build.”
The Four Fundamental Effects
One Many
Sync a Iterable a
Async Future a ???
The Four Fundamental Effects
One Many
Sync a Iterable a
Async Future a
Reactive
Programming
Can you feel the
Duality tonight?
Interactive vs Reactive
Sync vs Async
Pull vs Push
Iterable vs ???
Iterable
data Iterator a = Iterator
{ moveNext :: () -> IO Bool
, current :: () -> a
}
newtype Iterable a = Iterable
{ getIterator :: () -> IO (Iterator a)
}
Optional Values
data Maybe a = Nothing | Just a
Iterable
data Iterator a = Iterator
{ moveNext :: () -> IO Bool
, current :: () -> a
}
newtype Iterable a = Iterable
{ getIterator :: () -> IO (Iterator a)
}
Iterable
data Iterator a = Iterator
{ moveNext :: () -> IO (Maybe a)
}
newtype Iterable a = Iterable
{ getIterator :: () -> IO (Iterator a)
}
Coproducts
data Either a b = Left a | Right b
Iterable
data Iterator a = Iterator
{ moveNext :: () -> IO (Maybe a)
}
newtype Iterable a = Iterable
{ getIterator :: () -> IO (Iterator a)
}
Iterable
data Iterator a = Iterator
{ moveNext :: () -> IO (Either SomeException (Maybe a))
}
newtype Iterable a = Iterable
{ getIterator :: () -> IO (Iterator a)
}
Iterable
type Iterator a = () -> IO (Either SomeException (Maybe a))
type Iterable a = () -> IO (Iterator a)
Iterable
type Iterator a = () -> IO a
type Iterable a = () -> IO (Iterator a)
Iterable
type Iterator a = () -> IO a
type Iterable a = () -> IO (() -> IO a)
Iterator == Getter
() -> IO a
Covariance
a <: b
Iterator a <: Iterator b
Covariance
a <: b
Iterator a <: Iterator b
Covariance
Coke <: Drink
VCoke
<: VDrink
Functor
fmap :: (a -> b)
-> Iterator a
-> Iterator b
fmap f ia = () -> f (ia ())
fmap f ia = f . ia -- g (f (x)) = g . f
fmap = (.)
Iterable == Getter of Getters
() -> IO (() -> IO a)
Iterable == Getter of Getters
() -> IO (() -> IO a)
Lifting
Transform a function into a corresponding function within another - usually
more general - setting.
lift :: (Iterator a -> Iterator b)
-> Iterable a
-> Iterable b
lift f iia = () -> f (iia ())
lift = (.)
Functor
fmap' :: (a -> b)
-> Iterable a
-> Iterable b
fmap' f iia = lift (fmap f) iia
Applying Duality
==
Reverse all the ->
Observer
type Iterator a = () -> IO a
= () IO <- a
type Observer a = a -> IO ()
Observer
type Iterator a = () -> IO a
= () IO <- a
type Observer a = a -> IO ()
Observer
type Iterator a = () -> IO a
= () IO <- a
type Observer a = a -> IO ()
Observable
type Iterable a = () -> IO (() -> IO a)
= () IO <- (() IO <- a)
type Observable a = (a -> IO ()) -> IO ()
Observable
type Iterable a = () -> IO (() -> IO a)
= () IO <- (() IO <- a)
type Observable a = (a -> IO ()) -> IO ()
Observable
type Iterable a = () -> IO (() -> IO a)
= () IO <- (() IO <- a)
type Observable a = (a -> IO ()) -> IO ()
Observable
type Observer a = a -> IO ()
type Observable a = (a -> IO ()) -> IO ()
Observer == Setter
a -> IO ()
Contravariance
a <: b
Observer b <: Observer a
Contravariance
a <: b
Observer b <: Observer a
Contravariance
Coke <: Drink
RDrink
<: RCoke
Contravariant Functor
contramap :: (a -> b)
-> Observer b
-> Observer a
contramap f ob = a -> ob (f a)
contramap f ob = ob . f
contramap = flip (.)
Observable == Setter of Setters
(a -> IO ()) -> IO ()
Observable == Setter of Setters
(a -> IO ()) -> IO ()
Lifting
lift :: (Observer b -> Observer a)
-> Observable a
-> Observable b
lift f ooa = ob -> ooa (f ob)
lift f ooa = ooa . f
lift = flip (.)
Functor
fmap :: (a -> b)
-> Observable a
-> Observable b
fmap f ooa = lift (contramap f) ooa
Continuation Passing Style (CPS)
Suspended computation taking a continuation as argument. Instead of returning a
result, they will push it to the continuation.
cps :: (a -> r) -> r
add :: Int -> Int -> Int
add x y = x + y
add_cps :: Int -> Int -> ((Int -> r) -> r)
add_cps x y = k -> k (x + y)
Observable == CPS function
cps :: (a -> r ) -> r
observable :: (a -> IO ()) -> IO ()
Observable
type Observer a = a -> IO ()
type Observable a = (a -> IO ()) -> IO ()
Observable
type Observer a =
Either SomeException (Maybe a) -> IO ()
type Observable a = (Observer a) -> IO ()
Observable
newtype Observer a = Observer
{ onNext :: Either SomeException (Maybe a) -> IO ()
}
newtype Observable a =
{ subscribe :: (Observer a) -> IO ()
}
Observable
data Observer a = Observer
{ onNext :: a -> IO ()
, onError :: SomeException -> IO ()
, onCompleted :: IO ()
}
newtype Observable a =
{ subscribe :: (Observer a) -> IO ()
}
The Four Fundamental Effects
One Many
Sync a Iterable a
Async Future a Observable a
What about backpressure?
Quoting
Gérard Berry
Interactive programs interact at
their own speed with users or with
other programs…
Reactive programs also maintain
a continuous interaction with their
environment, but at a speed which
is determined by the environment,
not by the program itself.
Producer Consumer
Reactive
Interactive
Reactive Streams
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
public interface Subscriber<T> {
public void onSubscribe(Subscription s);
public void onNext(T t);
public void onError(Throwable t);
public void onComplete();
}
Reactive Streams
public interface Subscription {
public void request(long n);
public void cancel();
}
AsyncIterable
data AsyncIterator a = AsyncIterator
{ moveNext :: () -> IO (Future Bool)
, current :: () -> a
}
newtype AsyncIterable a = AsyncIterable
{ getAsyncIterable :: () -> AsyncIterable a
}
The Four Fundamental Effects
One Many
Sync a Iterable a
Async Future a Observable a
AsyncIterable a
Use the Right Tool for the Right Job
The essence of Reactive Programming
Thank you
Eddy Bertoluzzo
eddy.bertoluzzo@gmail.com

More Related Content

PDF
ZIO Queue
PDF
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
PDF
All Aboard The Scala-to-PureScript Express!
PDF
Scalaz Stream: Rebirth
PDF
Scalaz 8 vs Akka Actors
PPTX
ScalaCheck
PDF
Functional Programming in Java 8
PDF
Orthogonal Functional Architecture
ZIO Queue
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
All Aboard The Scala-to-PureScript Express!
Scalaz Stream: Rebirth
Scalaz 8 vs Akka Actors
ScalaCheck
Functional Programming in Java 8
Orthogonal Functional Architecture

Viewers also liked (20)

ODP
1.7 functional programming
ODP
Elm & Elixir: Functional Programming and Web
PDF
The taste of F#
PDF
Designing with Capabilities
PDF
Real-World Functional Programming @ Incubaid
PDF
Doge-driven design
PDF
The Theory of Chains
PDF
Slick 3.0 functional programming and db side effects
PDF
Functional programming
PDF
Dr Frankenfunctor and the Monadster
PDF
Swift vs. Language X
PPTX
Functional Programming with C#
PDF
16 logical programming
PDF
Functional Programming with Immutable Data Structures
PDF
Domain Driven Design with the F# type System -- NDC London 2013
PDF
Designing with capabilities (DDD-EU 2017)
PDF
Thirteen ways of looking at a turtle
PDF
Functional Programming in C# and F#
PDF
[FT-11][ltchen] A Tale of Two Monads
1.7 functional programming
Elm & Elixir: Functional Programming and Web
The taste of F#
Designing with Capabilities
Real-World Functional Programming @ Incubaid
Doge-driven design
The Theory of Chains
Slick 3.0 functional programming and db side effects
Functional programming
Dr Frankenfunctor and the Monadster
Swift vs. Language X
Functional Programming with C#
16 logical programming
Functional Programming with Immutable Data Structures
Domain Driven Design with the F# type System -- NDC London 2013
Designing with capabilities (DDD-EU 2017)
Thirteen ways of looking at a turtle
Functional Programming in C# and F#
[FT-11][ltchen] A Tale of Two Monads
Ad

Recently uploaded (20)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PDF
top salesforce developer skills in 2025.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
history of c programming in notes for students .pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
AI in Product Development-omnex systems
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Design an Analysis of Algorithms I-SECS-1021-03
wealthsignaloriginal-com-DS-text-... (1).pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Softaken Excel to vCard Converter Software.pdf
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
top salesforce developer skills in 2025.pdf
Reimagine Home Health with the Power of Agentic AI​
CHAPTER 2 - PM Management and IT Context
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Operating system designcfffgfgggggggvggggggggg
history of c programming in notes for students .pptx
PTS Company Brochure 2025 (1).pdf.......
AI in Product Development-omnex systems
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Ad

The essence of Reactive Programming