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

What's hot (20)

PDF
Completable future
Srinivasan Raghvan
 
PPTX
Luis Atencio on RxJS
Luis Atencio
 
PPTX
Avoiding Callback Hell with Async.js
cacois
 
PDF
ECMAScript 6
偉格 高
 
ODP
Concurrent Programming in Java
Ruben Inoto Soto
 
PDF
Callbacks and control flow in Node js
Thomas Roch
 
PDF
LINQ Inside
jeffz
 
PDF
Asynchronous programming done right - Node.js
Piotr Pelczar
 
PDF
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
Islam Sharabash
 
PDF
CLS & asyncListener: asynchronous observability for Node.js
Forrest Norvell
 
PPT
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
Allen Wirfs-Brock
 
PDF
Callbacks, promises, generators - asynchronous javascript
Łukasz Kużyński
 
PDF
Intro to RxJava/RxAndroid - GDG Munich Android
Egor Andreevich
 
PDF
Practical RxJava for Android
Tomáš Kypta
 
PDF
RxJava on Android
Dustin Graham
 
PPTX
Javascript asynchronous
kang taehun
 
PDF
Understanding Asynchronous JavaScript
jnewmanux
 
PDF
Hipster oriented programming (Mobilization Lodz 2015)
Jens Ravens
 
PDF
Non Blocking I/O for Everyone with RxJava
Frank Lyaruu
 
PDF
Advanced functional programing in Swift
Vincent Pradeilles
 
Completable future
Srinivasan Raghvan
 
Luis Atencio on RxJS
Luis Atencio
 
Avoiding Callback Hell with Async.js
cacois
 
ECMAScript 6
偉格 高
 
Concurrent Programming in Java
Ruben Inoto Soto
 
Callbacks and control flow in Node js
Thomas Roch
 
LINQ Inside
jeffz
 
Asynchronous programming done right - Node.js
Piotr Pelczar
 
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
Islam Sharabash
 
CLS & asyncListener: asynchronous observability for Node.js
Forrest Norvell
 
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
Allen Wirfs-Brock
 
Callbacks, promises, generators - asynchronous javascript
Łukasz Kużyński
 
Intro to RxJava/RxAndroid - GDG Munich Android
Egor Andreevich
 
Practical RxJava for Android
Tomáš Kypta
 
RxJava on Android
Dustin Graham
 
Javascript asynchronous
kang taehun
 
Understanding Asynchronous JavaScript
jnewmanux
 
Hipster oriented programming (Mobilization Lodz 2015)
Jens Ravens
 
Non Blocking I/O for Everyone with RxJava
Frank Lyaruu
 
Advanced functional programing in Swift
Vincent Pradeilles
 

Viewers also liked (9)

PDF
Monads in Clojure
Leonardo Borges
 
PDF
JS Lab`16. Роман Лютиков: "ClojureScript, что ты такое?"
GeeksLab Odessa
 
PDF
Clojure: an overview
Larry Diehl
 
PDF
Deep Learning and Text Mining
Will Stanton
 
KEY
Functional programming in clojure
Juan-Manuel Gimeno
 
PDF
Clojure for Java developers
John Stevenson
 
ODP
Getting started with Clojure
John Stevenson
 
PDF
Clojure: The Art of Abstraction
Alex Miller
 
PPTX
Lego Serious Play Introduction
martinsandberg
 
Monads in Clojure
Leonardo Borges
 
JS Lab`16. Роман Лютиков: "ClojureScript, что ты такое?"
GeeksLab Odessa
 
Clojure: an overview
Larry Diehl
 
Deep Learning and Text Mining
Will Stanton
 
Functional programming in clojure
Juan-Manuel Gimeno
 
Clojure for Java developers
John Stevenson
 
Getting started with Clojure
John Stevenson
 
Clojure: The Art of Abstraction
Alex Miller
 
Lego Serious Play Introduction
martinsandberg
 
Ad

Similar to Functional Reactive Programming in Clojurescript (20)

PDF
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
PatrickCrompton
 
PDF
Intro to JavaScript Testing
Ran Mizrahi
 
PDF
Practical pairing of generative programming with functional programming.
Eugene Lazutkin
 
PDF
Functional Reactive Programming in the Netflix API
C4Media
 
PDF
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Pablo Godel
 
PDF
Groovy On Trading Desk (2010)
Jonathan Felch
 
PPTX
Systematic Generation Data and Types in C++
Sumant Tambe
 
PDF
Scalable JavaScript
Ynon Perek
 
PDF
Lone StarPHP 2013 - Building Web Apps from a New Angle
Pablo Godel
 
PDF
To Batch Or Not To Batch
Luca Mearelli
 
PDF
function* - ES6, generators, and all that (JSRomandie meetup, February 2014)
Igalia
 
PDF
Towards a software ecosystem for java prolog interoperabilty
kim.mens
 
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
PDF
JS Responsibilities
Brendan Eich
 
PDF
Variables & Expressions
Rich Price
 
PDF
OSCON - ES6 metaprogramming unleashed
Javier Arias Losada
 
PDF
GR8Conf 2009: Groovy Usage Patterns by Dierk König
GR8Conf
 
PDF
vbench: lightweight performance testing for Python
Wes McKinney
 
PDF
Gon gem. For RDRC 2013, June 7
Alexey Gaziev
 
PDF
OSDC 2013 | 2000 databases later by Kristian Köhntopp
NETWAYS
 
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
PatrickCrompton
 
Intro to JavaScript Testing
Ran Mizrahi
 
Practical pairing of generative programming with functional programming.
Eugene Lazutkin
 
Functional Reactive Programming in the Netflix API
C4Media
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Pablo Godel
 
Groovy On Trading Desk (2010)
Jonathan Felch
 
Systematic Generation Data and Types in C++
Sumant Tambe
 
Scalable JavaScript
Ynon Perek
 
Lone StarPHP 2013 - Building Web Apps from a New Angle
Pablo Godel
 
To Batch Or Not To Batch
Luca Mearelli
 
function* - ES6, generators, and all that (JSRomandie meetup, February 2014)
Igalia
 
Towards a software ecosystem for java prolog interoperabilty
kim.mens
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
JS Responsibilities
Brendan Eich
 
Variables & Expressions
Rich Price
 
OSCON - ES6 metaprogramming unleashed
Javier Arias Losada
 
GR8Conf 2009: Groovy Usage Patterns by Dierk König
GR8Conf
 
vbench: lightweight performance testing for Python
Wes McKinney
 
Gon gem. For RDRC 2013, June 7
Alexey Gaziev
 
OSDC 2013 | 2000 databases later by Kristian Köhntopp
NETWAYS
 
Ad

More from Leonardo Borges (18)

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

Recently uploaded (20)

PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 

Functional Reactive Programming in Clojurescript