SlideShare a Scribd company logo
Taming asynchronous
workflows with Functional
Reactive Programming
LambdaJam - Brisbane, 2013
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
Friday, 17 May 13
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
• Thoughtworker
• Functional Programming enthusiast
• Clojure Evangelist
• Founder & Organiser of the Sydney
Clojure User Group (clj-syd)
• World traveller
• Fan of Murray’s Beers :)
about:me
Friday, 17 May 13
Functional programmers like
programming with values:
a, b, c...
and pure functions:
f, g, h...
Friday, 17 May 13
We get new values by applying
functions to it
(f a) ;;=> b
Friday, 17 May 13
But that’s hardly useful when
we have multiple values
(def vals [a b c])
Friday, 17 May 13
So we use Higher Order
Functions
(map f vals)
Friday, 17 May 13
And compose them as we see fit
(-> vals
(filter f)
(map g)
(reduce h))
Friday, 17 May 13
But what if the value isn’t
known...yet?
a?
Friday, 17 May 13
We make promises
;; thread#1
(def a (promise))
;; ...later in the program
(f @a) ;;<= blocks thread
;; thread#2
(deliver a 10) ;; now thread#1 continues
Friday, 17 May 13
Not great if we want to ‘react’
to a new value
Friday, 17 May 13
What about a list of - as of yet
unknown - values?
[a,b,c]? ? ?
Friday, 17 May 13
Or better yet, a value that
changes over time?
0
37.5
75
112.5
150
10s 20s 30s 40s 50s 60
Value
Time
Friday, 17 May 13
Does this sound familiar?
Friday, 17 May 13
Spreadsheets: a poor man’s
reactive programming model
Values
Function
Friday, 17 May 13
Spreadsheets: a poor man’s
reactive programming model
As we change
a value
Our function cell
reacts to the
change
Friday, 17 May 13
‘Changing a value’ is an event
Several events over time form an
event stream
Friday, 17 May 13
“Functional Reactive
Programming is about effectively
processing event streams without
explicitly managing state”
- me
Friday, 17 May 13
“FRP is about handling time-
varying values like they were
regular values.”
- Haskell wiki
Friday, 17 May 13
We’ll use Reactive Extensions
(Rx) - but there are many
implementations
Friday, 17 May 13
In Rx, event streams are called
Observable sequences
Friday, 17 May 13
Rx 101
(-> (.returnValue js/Rx.Observable 42)
(.map #(* % 2))
(.subscribe #(.log js/console %)))
;; 84
Friday, 17 May 13
Rx 101
(-> (.fromArray js/Rx.Observable
(clj->js [10 20 30]))
(.map #(* % 2))
(.reduce +)
(.subscribe #(.log js/console %)))
;; 120
Friday, 17 May 13
Rx 101
(defn project-range [n]
(.returnValue js/Rx.Observable (range n)))
(-> (.fromArray js/Rx.Observable
(clj->js [1 2 3]))
(.selectMany project-range)
(.subscribe #(.log js/console (clj->js %))))
;; [0]
;; [0 1]
;; [0 1 2]
Friday, 17 May 13
Observables are Monads
Friday, 17 May 13
The Monad Type Class
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
Friday, 17 May 13
Monad functions: return
return :: a -> m a
returnValue :: a -> Observable a
Friday, 17 May 13
(>>=) :: m a -> (a -> m b) -> m b
selectMany :: Observable a -> (a -> Observable b) -> Observable b
Monad functions: >>= (bind)
Friday, 17 May 13
Demo: Simple polling app
Friday, 17 May 13
Server exposes poll questions
and results
e.g.:
{:id 7
:question "Which is the best music style?"
:results {:a 10
:b 47
:c 17}}
Friday, 17 May 13
What we want
• Render results
• Continuously poll server every 2 secs
• If current question is the same as the previous one
update results;
• Otherwise:
• Stop polling;
• Display countdown message;
• Render new question and results;
• Restart polling;
Friday, 17 May 13
The core idea
Friday, 17 May 13
Turn server results into an
event stream
112334
Friday, 17 May 13
Duplicate stream, skipping one
112334
123345
skip 1
Friday, 17 May 13
Zip them together
112334
1
2
zip
2
3
3
3
3
4
4
5 1
123345
1
Friday, 17 May 13
Now we have access to both
the previous and current
results, with no local variables
Friday, 17 May 13
Show me the code!
https://github.com/leonardoborges/frp-code
Friday, 17 May 13
(def results-connectable
(let [obs (-> js/Rx.Observable
(.interval 2000)
(.selectMany results-observable)
(.publish)
(.refCount))
obs-1 (.skip obs 1)]
(.zip obs obs-1 (fn [prev curr]
{:prev prev
:curr curr}))))
Turn server results into an event stream
{
The core idea
Clone stream, skip one
Zip them together
{
Friday, 17 May 13
“FRP is about handling time-
varying values like they were
regular values.”
- Haskell wiki
Friday, 17 May 13
Questions?
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
Friday, 17 May 13
References
Code - https://github.com/leonardoborges/frp-code
RxJS - https://github.com/Reactive-Extensions/RxJS
RxJava - https://github.com/Netflix/RxJava
Other FRP implementations:
Reactive-banana - http://www.haskell.org/haskellwiki/Reactive-banana
Javelin (Clojurescript) - https://github.com/tailrecursion/javelin
Bacon.js - https://github.com/raimohanska/bacon.js
Friday, 17 May 13

More Related Content

PDF
The algebra of library design
PDF
Functional Reactive Programming / Compositional Event Systems
PPTX
Real world functional reactive programming
PPTX
RxJava Applied
PDF
RxJava applied [JavaDay Kyiv 2016]
PDF
The Future of Futures - A Talk About Java 8 CompletableFutures
PPTX
The Road To Reactive with RxJava JEEConf 2016
ODP
CompletableFuture
The algebra of library design
Functional Reactive Programming / Compositional Event Systems
Real world functional reactive programming
RxJava Applied
RxJava applied [JavaDay Kyiv 2016]
The Future of Futures - A Talk About Java 8 CompletableFutures
The Road To Reactive with RxJava JEEConf 2016
CompletableFuture

What's hot (20)

PDF
Completable future
PPTX
Luis Atencio on RxJS
PPTX
Avoiding Callback Hell with Async.js
PDF
ECMAScript 6
ODP
Concurrent Programming in Java
PDF
Callbacks and control flow in Node js
PDF
LINQ Inside
PDF
Asynchronous programming done right - Node.js
PDF
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
PDF
CLS & asyncListener: asynchronous observability for Node.js
PPT
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
PDF
Callbacks, promises, generators - asynchronous javascript
PDF
Intro to RxJava/RxAndroid - GDG Munich Android
PDF
Practical RxJava for Android
PDF
RxJava on Android
PPTX
Javascript asynchronous
PDF
Understanding Asynchronous JavaScript
PDF
Hipster oriented programming (Mobilization Lodz 2015)
PDF
Non Blocking I/O for Everyone with RxJava
PDF
Advanced functional programing in Swift
Completable future
Luis Atencio on RxJS
Avoiding Callback Hell with Async.js
ECMAScript 6
Concurrent Programming in Java
Callbacks and control flow in Node js
LINQ Inside
Asynchronous programming done right - Node.js
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
CLS & asyncListener: asynchronous observability for Node.js
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
Callbacks, promises, generators - asynchronous javascript
Intro to RxJava/RxAndroid - GDG Munich Android
Practical RxJava for Android
RxJava on Android
Javascript asynchronous
Understanding Asynchronous JavaScript
Hipster oriented programming (Mobilization Lodz 2015)
Non Blocking I/O for Everyone with RxJava
Advanced functional programing in Swift
Ad

Viewers also liked (9)

PDF
Monads in Clojure
PDF
JS Lab`16. Роман Лютиков: "ClojureScript, что ты такое?"
PDF
Clojure: an overview
PDF
Deep Learning and Text Mining
KEY
Functional programming in clojure
PDF
Clojure for Java developers
ODP
Getting started with Clojure
PDF
Clojure: The Art of Abstraction
PPTX
Lego Serious Play Introduction
Monads in Clojure
JS Lab`16. Роман Лютиков: "ClojureScript, что ты такое?"
Clojure: an overview
Deep Learning and Text Mining
Functional programming in clojure
Clojure for Java developers
Getting started with Clojure
Clojure: The Art of Abstraction
Lego Serious Play Introduction
Ad

Similar to Functional Reactive Programming in Clojurescript (20)

PDF
(Functional) reactive programming (@pavlobaron)
PDF
I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...
PPTX
Functional Reactive Programming with RxJS
PDF
Functional Reactive Programming in JavaScript
PPTX
RxJS vs RxJava: Intro
PDF
Reactive programming with RxJS - Taiwan
PPTX
Reactive programming
PDF
FRP with Ractive and RxJS
PPTX
Functional Reactive Endpoints using Spring 5
PDF
Funtional Reactive Programming with Examples in Scala + GWT
PPTX
Functional Reactive Programming
PDF
How do you do that in FRP
PDF
Intro to Functional Reactive Programming In Scala
PDF
RxJS - The Reactive Extensions for JavaScript
PDF
Moving towards Reactive Programming
PDF
Reactive systems
PPTX
Functional Reactive Programming (FRP): Working with RxJS
PDF
[DevDay 2019] Reactive Programming with JavaScript - By Pham Nguyen Duc, Web ...
(Functional) reactive programming (@pavlobaron)
I have a stream - Insights in Reactive Programming - Jan Carsten Lohmuller - ...
Functional Reactive Programming with RxJS
Functional Reactive Programming in JavaScript
RxJS vs RxJava: Intro
Reactive programming with RxJS - Taiwan
Reactive programming
FRP with Ractive and RxJS
Functional Reactive Endpoints using Spring 5
Funtional Reactive Programming with Examples in Scala + GWT
Functional Reactive Programming
How do you do that in FRP
Intro to Functional Reactive Programming In Scala
RxJS - The Reactive Extensions for JavaScript
Moving towards Reactive Programming
Reactive systems
Functional Reactive Programming (FRP): Working with RxJS
[DevDay 2019] Reactive Programming with JavaScript - By Pham Nguyen Duc, Web ...

More from Leonardo Borges (18)

PDF
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
PDF
Parametricity - #cljsyd - May, 2015
PDF
From Java to Parellel Clojure - Clojure South 2019
PDF
Futures e abstração - QCon São Paulo 2015
PDF
High Performance web apps in Om, React and ClojureScript
PDF
Programação functional reativa: lidando com código assíncrono
PDF
Clojure Macros Workshop: LambdaJam 2013 / CUFP 2013
PDF
Intro to Clojure's core.async
PDF
Clojure/West 2013 in 30 mins
PDF
Clojure Reducers / clj-syd Aug 2012
PDF
The many facets of code reuse in JavaScript
PDF
Continuation Passing Style and Macros in Clojure - Jan 2012
PDF
Heroku addons development - Nov 2011
PDF
Clouds against the Floods (RubyConfBR2011)
KEY
Clouds Against the Floods
KEY
Arel in Rails 3
PDF
Testing with Spring
PDF
JRuby in The Enterprise
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
Parametricity - #cljsyd - May, 2015
From Java to Parellel Clojure - Clojure South 2019
Futures e abstração - QCon São Paulo 2015
High Performance web apps in Om, React and ClojureScript
Programação functional reativa: lidando com código assíncrono
Clojure Macros Workshop: LambdaJam 2013 / CUFP 2013
Intro to Clojure's core.async
Clojure/West 2013 in 30 mins
Clojure Reducers / clj-syd Aug 2012
The many facets of code reuse in JavaScript
Continuation Passing Style and Macros in Clojure - Jan 2012
Heroku addons development - Nov 2011
Clouds against the Floods (RubyConfBR2011)
Clouds Against the Floods
Arel in Rails 3
Testing with Spring
JRuby in The Enterprise

Recently uploaded (20)

PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
CloudStack 4.21: First Look Webinar slides
PPT
Geologic Time for studying geology for geologist
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
Five Habits of High-Impact Board Members
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Getting Started with Data Integration: FME Form 101
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Chapter 5: Probability Theory and Statistics
PDF
STKI Israel Market Study 2025 version august
PPTX
The various Industrial Revolutions .pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPTX
Benefits of Physical activity for teenagers.pptx
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
CloudStack 4.21: First Look Webinar slides
Geologic Time for studying geology for geologist
observCloud-Native Containerability and monitoring.pptx
Five Habits of High-Impact Board Members
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Final SEM Unit 1 for mit wpu at pune .pptx
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
NewMind AI Weekly Chronicles – August ’25 Week III
Getting Started with Data Integration: FME Form 101
1 - Historical Antecedents, Social Consideration.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Getting started with AI Agents and Multi-Agent Systems
Chapter 5: Probability Theory and Statistics
STKI Israel Market Study 2025 version august
The various Industrial Revolutions .pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Benefits of Physical activity for teenagers.pptx

Functional Reactive Programming in Clojurescript