Functional reactive programming (FRP) is a merge of two different programming paradigms; functional and reactive. In this article we explore the history and theoretical foundation of FRP before diving into FRP in practice with an example in JavaScript using Bacon.js. After reading this article, you will have a basic understanding of what functional reactive programming is, which benefits it gives and how to apply it to your daily work. By Mikael Brevik and Stian Møllersen
BACKGROUND AND HISTORY Functional Reactive Programming is not a new concept in computer science. It can be traced to 1997, when Conal Elliot, from the Microsoft Research Group, and Paul Hudak, from Yale University released, the research paper "Functional Reactive Animation" in the proceedings of International Conference on Functional Programming1. The academic paper introduces a collection of data types and functions in a system called FRAN (Functional Reactive ANimations). FRAN in turn is preceeded by work done by Conal Elliot and the ActiveVRML modelling language. ActiveVRML was a declarative modelling language for defining simple, sophisticated and interactive animations2. The implementation of FRAN was based on the programming language Haskell, which is an easy extendable functional programming language. As Haskell may have some problems with space leaks, FRAN did as well. To try to fix this, Paul Hudak (et. al.) introduced Real Time FRP (RT-FRP) in a paper from 20013. RT-FRP made some simplifications to the model, but in turn lost some of the expressiveness of the FRAN system. A short year after introducing RT-FRP, the Yale team also released Event-Driven FRP, focusing
on value changes triggered by events and propagating values only on these events4. Some years later, Conal Elliot published the paper "Push-Pull Functional Reactive Programming" (2009)5. In this paper he described a way to implement FRP with a more modernized interface, and where the reactive values had a more simplified semantic. All reactive values were described with push- and pull-based evaluation. In later years, several different implementations and libraries have arisen. Some examples are the compile-tojavascript languages Elm6 and Flapjax7, or Bacon.js8 – which is written completely in Javascript. There are also implementations in languages like C#, Java, Scala, Objective-C, Ruby, and more. THEORETICAL FOUNDATION The primary goal of functional reactive programming is to enable functional programming of user interfaces. Why does one want to program functionally? Functional programming gives the programmer tools to reason about code, both informal and rigorous, through the use of immutability and pure functions.
39