Functional Reactive Programming (FRP)
Functional Reactive Programming (FRP)
Functional Reactive Programming (FRP)
Programming (FRP)
with reactive-banana-0.6.0.0
Heinrich Apfelmus
Functional Reactive
Programming (FRP)
with reactive-banana-0.6.0.0
Heinrich Apfelmus
Why?
Functional reactive programming is an
elegant method for implementing
interactive programs
Time
bf <*> bx = at each
\time -> bf time $ bx time point in time
0 Time Time
(*) <$> a <*> b
Time
Example task: attenuate an oscillation.
Event
type Event a = [(Time,a)]
Value
• mous clicks – GUI
• notes – music
• collision – physics
Time
You can also see that event occurrences may happen simultaneously, at least in reactive-banana-0.6.
Event API
instance Functor Event Functor
List
never :: Event a []
unionWith :: (a -> a -> a)
-> Event a -> Event a -> Event a zipWith
Int
10
0 Time Time
Example: filterE
Event & Behavior API
stepper :: a -> Event a -> Behavior a
x stepper 2 x
Time 2 Time
Of course, the most interesting part about the API concerns the interaction between Behavior and Event.
The `stepper` function turns an Event into a Behavior by remembering the value. The result is a step function,
hence the name.
Event & Behavior API
(<@>) :: Behavior (a -> b)
-> Event a -> Event b
„apply“
(<@) :: Behavior b
-> Event a -> Event b
The <@> operator is called „apply“ and applies a time-varying function to event occurrences.
Its little brother <@ tags an Event with values from the Behavior. It is analogous to the <$ operator from
Data.Functor.
Event & Behavior API
()
b e
Time Time
b <@ e
Time
Visualization of the <@ operator.
Frameworks (GUI, ...)
data NetworkDescription t a