Functional reactive programming
Functional reactive programming (FRP) is a programming paradigm for reactive programming (asynchronous dataflow programming) using the building blocks of functional programming (e.g. map, reduce, filter). FRP has been used for programming graphical user interfaces (GUIs), robotics, and music, aiming to simplify these problems by explicitly modeling time.
Contents
Formulations of FRP[edit]
FRP has taken many forms since its introduction in 1997.[1] One axis of diversity is discrete vs. continuous semantics. Another axis is how FRP systems can be changed dynamically.[2]
Discrete[edit]
Formulations such as Event-Driven FRP and Elm require that updates are discrete and event-driven.[3] These formulations have pushed for practical FRP, focusing on a semantics that have a simple API that can be implemented efficiently in a setting such as robotics or in a web-browser.[4]
In these formulations, it is common that the ideas of behaviors and events are combined into signals that always have a current value, but change discretely.[5]
Continuous[edit]
The earliest formulation of FRP used a continuous semantics, aiming to abstract over many operational details that are not important to the meaning of a program.[6] The key properties of this formulation are:
- Modeling values that vary over continuous time, called "behaviors" and later "signals".
- Modeling "events" which have occurrences at finitely many points in time.
- The system can be changed in response to events, generally termed "switching."
- The separation of evaluation details such as sampling rate from the reactive model.
This semantic model of FRP in side-effect free languages is typically in terms of continuous functions, and typically over time.[7]
Implementation issues[edit]
There are two types of FRP systems, push-based and pull-based. Push-based systems take events and push them through a signal network to achieve a result. Pull-based systems wait until the result is demanded, and work backwards through the network to retrieve the value demanded.
Some FRP systems such as Yampa use sampling. On a regular interval, samples are pushed through a signal network. This approach has two drawbacks: it is very computation intensive to process samples on a regular interval, and the network has to wait up to the duration of the sampling interval to find out about changes to the input. Sampling is an example of push-based FRP.
The Reactive and Etage libraries on Hackage introduced an approach called push-pull FRP, which combines the best of push-based and pull-based FRP. In this approach, only when the next event on a purely defined stream (such as a list of fixed events with times) is demanded, that event is constructed. These purely defined streams act like lazy lists in Haskell. That is the pull-based half. The push-based half is used when events external to the system are brought in. The external events are pushed to consumers, so that they can find out about an event the instant it is issued.
See also[edit]
- Elm, FRP language that compiles to HTML, CSS, and JavaScript
- Reactive Extensions for JavaScript (RxJS), a set of libraries to compose asynchronous and event-based programs using observable collections and Array#extras style composition in JavaScript
- Flapjax, behavior/event FRP implementation in JavaScript
- Bacon.js, a small functional reactive programming library for JavaScript
- ProAct.js, a powerful Reactive Programming JavaScript library, integrating both the functional and object-oriented implementation approaches.
- Kefir.js, a small FRP library with focus on high performance
- Tao Presentations, FRP language for 3D interactive presentations[8]
- Incremental computing
- Stream processing
References[edit]
- ^ Czaplicki, Evan (Apr 2012), Elm: Concurrent FRP for Functional GUIs (PDF) (thesis), Harvard: Test blog, please ignore.
- ^ Nilsson, Henrik; Courtney, Antony; Peterson, John (Feb 2011) [2002], "Functional Reactive Programming, Continued", Haskell Workshop (PDF) (2).
- ^ Taha, Walid; Wan, Zhanyong; Hudak, Paul (2002), "Event-Driven FRP", PADL (PDF), Yale.
- ^ Czaplicki, Evan; Chong, Stephen (2013), "Asynchronous Functional Reactive Programming for GUIs", PLDI, Harvard.
- ^ Wan, Zhanyong; Taha, Walid; Hudak, Paul (Feb 2011), "Real-Time FRP", ICFP (PDF) (1).
- ^ Elliott, Conal; Hudak, Paul (1997), "Functional Reactive Animation", ICFP.
- ^ Courtney, Antony; Elliott, Conal (Feb 2011) [2001], "Genuinely Functional User Interfaces", Haskell Workshop, Yale.
- ^ Animation & 3D: the Web is doing it wrong (World Wide Web log), Taodyne.
External links[edit]
- "What is Functional Reactive Programming?" An intro to FRP with live demos.
- Elm Programming language A functional reactive programming language that compiles to html/css/javascript.
- React, functional reactive programming in OCaml
- Haskell-related FRP research
- Bacon.js, a small FRP library for JavaScript.
- Functional reactive programming with Bacon.js (Web log), Flowdock, Jan 22, 2013.
- Kefir.js, FRP library for JavaScript with Bacon.js like API, but with better performance and less memory consumption
- ProAct.js, a powerful Reactive Programming JavaScript library, integrating both the functional and the object-oriented implementation approaches.
- Rx, Microsoft implementation: .NET and JavaScript
- RxJava, Netflix Java port of Rx
- React4J, a KISS implementation of FRP in Java SE, compatible with Java 8 Lambdas.
- Frappuccino, FRP implementation in Ruby.
- ReactiveCocoa, an Objective-C framework for FRP.
- ReactiveUI, a cross-platform C# framework for FRP.
- "Deprecating the Observer Pattern with Scala⋅React," Scala.React, an FRP Scala implementation
- Sodium, reactive programming in Java, Haskell and C++