Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Functional Reactive Programming

No description

on 4 September 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Functional Reactive Programming


By Kyle Hunter
Sept. 4, 2013

Referential Transparency
A review of analytic philosophy
Lois Lane believes
Clark Kent
defeated the villain.
Lois Lane believes
defeated the villain.
Clark Kent
is stronger than Batman.
is stronger than Batman.
"The Context"
“No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn’t convenient. You can learn about lambdas, monads, currying, composing lazily evaluated functions on infinite sets, and all the other aspects of explicitly functionally oriented languages later if you choose.”
— John Carmack
“An expression is said to be
referentially transparent
if it can be replaced with its value without changing the behavior of a program.”

This implies that for any RT function, the same arguments should always give the same result.
(Example modified from
f(x) can always be replaced by it's value, x+1
f(1) = 2
f(1) = 3
f(1) = 4
f() = 'h'
f() = 'e'
f() = 'l'
f() = 'l'
f() = 'o'
Assuming we have an user keyboard input function
Let's back up
There is no official definition as to what a functional programming language is.
Referential transparency of expressions (particularly functions)
Immutable data structures
First-class functions
Higher-order functions
First Class Functions
And those of a higher order
Still doesn't make you a functional language though.
So why functional?
Higher level of abstraction
Easier to reason about correctness
More maintainable code
Transparent concurrency
Lazy evaluation
Why not?
Worse performance/memory usage
I/O and events can be painful
Spreadsheets are reactive
Benefits tend to fade when more impure features are added
Functional Reactive Programming tends to help a lot with this issue while keeping it pure
FRP with
Thesis-baby of Evan Czaplicki
Written in Haskell
Purely functional
Compiles to JS/HTML/CSS
Uses concept of Signals for reactive component
Signals are Time-varying values
From Evan Czaplicki's thesis
Classical FRP (1997)
Real-Time FRP (2001)
"Behaviors are continuous, time-varying values. This is represented as a function from a time to a value."
"Events represent a sequence of discrete events as a time-stamped list of values."
Analog, velocity, acceleration, etc.
Mouse clicks, key presses, animations, etc.
Time and space leaks
Not as expressive
(No higher-order)
Event FRP based on RT-FRP introduces discrete signals which only change on event (mouse click, etc)
Arrowized FRP (2002)
Information From Evan Czaplicki's thesis
Using Signals in Elm
map :: (a -> b) -> [a] -> [b]
lift :: (a -> b) -> Signal a -> Signal b
foldr :: (a -> b -> b) -> b -> [a] -> b
foldp : (a -> b -> b) -> b -> Signal a -> Signal b
foldr (+) 5 [1,2,3,4]
map reverse ["abc","cda","1234"]
Examples (if time)
main must be an Element or Signal Element
Industry Usage of FRP
official example from website
Prezi —
Callback Hell
More FRP
“Just like goto, these callbacks force you to jump around your codebase in a way that is really hard to understand. You basically have to read the whole program to understand what any individual function does.”
— Evan Czaplicki (Creator of Elm)
(Examples from http://elm-lang.org/learn/Escape-from-Callback-Hell.elm)
continuation passing style (CPS)
“The cost of writing JavaScript is just too high. Every new and innovative feature costs too many lines of JavaScript code, and every line of JavaScript code costs too much time to maintain. Nevermind making that code performant. That’s a job for a code generator, not an engineer. JavaScript is broken, but that doesn’t mean we have to put up with it. And we don’t intend to.

Starting today, Evan Czaplicki is joining Prezi on a full-time basis. He’ll continue to develop Elm, a statically-typed, functional-reactive programming language that compiles down to JavaScript and runs in the browser. Elm’s development will remain open-source, and all the development that occurs within Prezi will make it into every release. The goal is to provide a viable, open-source language that’s both an alternative and complement to JavaScript for front-end development. Here’s why we think Elm is that language.”
— Prezi
Q: Is Prezi secretly trying to take control and make Elm all about zooming?

No :) Although I am a full-time employee of Prezi, I still have full control of the direction of Elm, and my primary goal is the success of Elm as an independent project.
Netflix —
“RxJava is our implementation of Rx for the JVM and is available in the Netflix repository in Github.

Functional reactive programming with RxJava has enabled Netflix developers to leverage server-side conconcurrency without the typical thread-safety and synchronization concerns. The API service layer implementation has control over concurrency primitives, which enables us to pursue system performance improvements without fear of breaking client code. ”
— Ben Christensen (Netflix developer)
Embrace Concurrency
Futures are Expensive to Compose
Callbacks Have Their Own Problems
Reactive is Good
The Reactive Extensions (Rx)
And this is an emerging technology, so expect more...
Functional Programming is great, but it has some problems.

Adding reactive extensions is a good fix for some of those problems.

(in a non-functional style)
Full transcript