Вы находитесь на странице: 1из 3

What is Functional Reactive Programming?

quora.com/What-is-Functional-Reactive-Programming

Tikhon Jelvis, I really love FRP!


Updated 128w ago · Author has 1.7k answers and 10.8m answer views
Fundamentally, functional reactive programming (FRP) is programming declaratively with
time-varying values. The idea is to model things like user input and animations in a more
direct, declarative way by making their behavior over time more explicit.

In a normal imperative program for something like a GUI, you model change over time
implicitly in two ways: using mutable state and using callbacks. FRP models the same
notions by introducing two data types representing values that change over time, events
and behaviors:

events represent a value at a particular time. For example, a keypress would be


represented as an Event Char. Pressing the a key at time t would be represented as
the pair (t, 'a'). In practice, we never program directly with events; instead, we deal
with potentially infinite streams of events ordered by time. So a keyboard input would
be a stream of Char events. You can think of streams as representing a value which
only exists sometimes: the typed character only exists when the key is struck.
behaviors represent values that vary continually over time. These are values that
always exist but can be constantly changing. The mouse position is a perfect
example: it would be a Behavior Point. The mouse always has a position, but when
it's moving that position is constantly changing.

Conceptually, you can think of event streams as infinite lists of time value pairs and
behaviors as functions from a time to a value. However, time is not usually explicit; instead,
it is managed by the FRP runtime system. This means that your entire program is made by
combining behaviors and events in different ways--you never ask for the value of
something now but instead define how it behaves based on other time-varying values.

The advantage of this sort of programming is that it abstracts over managing values that
change with time. It makes dependencies between different values more explicit and
allows you for some very nice combinators. This makes it fairly easy to express your logic
at a higher level instead of dealing with the implementation details of mutable state and
callbacks.

My favorite combinator for illustrating the declarative nature of FRP is when . The idea is
simple: given a Behavior Bool--a continuouisly varying boolean--we can use it to filter a
stream of events. Here's an example adapted from a very simple game of life applet I
wrote:

when (not <$> paused) (step <$ time)

1/3
The idea here is very simple. paused is a behavior that tells you whether the game is
paused or not. time is a stream of events from a timer with one event every n seconds.
step <$ time just creates a stream of functions that advance the simulation by one turn
every n seconds. The entire expression creates a stream of step functions every n
seconds as long as the game isn't paused.

I like this particular example because it's simple and easy to relate to. Most of the other
combinators are in a similar vein.

Your entire program in FRP ends up being a combination of events and behaviors like this--
you start with some primitive ones like mouse actions and combine them to create your
own. Finally, you just plug the events/behaviors into outpus as appropriate. For example, if
you have a Behavior String, you can just plug that into a label and have the label update to
the correct value automatically.

While FRP is probably most often used for GUIs, it can also be used for things like games,
synthesizing music or even controllers for robots. It's a very nice technique for specifying
reactive systems in general, which are systems that can continually respond to inputs.

Related QuestionsMore Answers Below


Phil Jones, Still trying to figure it out
Answered 260w ago · Author has 6.6k answers and 5m answer views
Socrates : Hey! You know how, like, since 1979, in my spreadsheet, I could say cell B1 is the
sum of the values in cells A1 and A2? And then when I change the value in A1, the sum in
B1 automatically updates itself without me having to do anything like explicitly say
"recalculate" or anything?
Hermogenes : Yeah?

Socrates : Isn't it weird that none of our sophisticated modern programming languages do
that? Like, I could say
monthCash = monthSalary - monthExpenses
one time, and then every time I update my monthSalary my monthCash automatically
updates itself too.

Hermogenes : Dude. That's FRP.

Alyssa Ann, Software Engineer at Holberton School (2017-present)


Answered 26w ago · Author has 198 answers and 54.9k answer views
Functional Reactive Programming (FRP) offers a fresh perspective on solving modern
programming problems. Once understood, it can greatly simplify your project, especially
when it comes to code dealing with asynchronous events with nested callbacks, complex
list filtering/transformation or timing concerns.

Functional reactive programming is when you combine functional programming to reactive


programming. Your events can carry not only data but also functions with their context
(lambda closures).

Holberton School

2/3
Dima Korolev, https://dimakorolev.quora.com/Against-Justificationism
Answered 182w ago · Author has 4.2k answers and 12.1m answer views
Functional Reactive is a paradigm in programming where instead of imperative actions the
programmer defines desired end results, in a data-driven way, designed for continuous
data processing.

Rax Wunter, Haskell enthusiast


Answered 137w ago
FRP deals with events, but it's more useful to understand it on practical examples.

You can take a look at this article: http://yarax.ru/2016/03/06/react...

And functional means, that we can follow some functional styles (map, reduce etc.)
working with streams so, as it were simple data.

There described some practical issues, which can be solved using reactive programming
on the client (browser interactions) as well on the server side (Node.js).

Examples are given using JavaScript Kefir.js, but it's very similar to other libraries as Rx.js
and Bacon.js

Kumar HariShankar, An Aspiring Polyglot


Answered 222w ago
I'm a FRP newbie. I found this while desperately trying to figure out what FRP actually is.
This article explains it with a clear example
staltz/introrx.md
Jason Feingold, 20+ years doing Scala, Java, , Javascript, C, C++, .NET, etc.
Answered 9w ago · Author has 159 answers and 25.4k answer views
Instead of writing an event handler that takes a single event and mutates state you process
streams of events that produce new states. These new states are used by other event
stream processors to produce yet more new states.

3/3

Вам также может понравиться