Reactive.Banana.Incremental
Why a third type Discrete?
In an ideal world, users of functional reactive programming would
only need to use the notions of Behavior
and Event
,
the first corresponding to value that vary in time
and the second corresponding to a stream of event ocurrences.
However, there is the problem of incremental updates.
Ideally, users would describe, say, the value of a GUI text field
as a Behavior
and the reactive-banana implementation would figure
out how to map this onto the screen without needless redrawing.
In other words, the screen should only be updated when the behavior changes.
While this would be easy to implement in simple cases,
it may not always suit the user;
there are many different ways of implementing
incremental computations.
But I don't know a unified theory for them, so
I have decided that the reactive-banana will give explicit control over updates to the user
in the form of specialized data types like Discrete
,
and shall not attempt to bake experimental optimizations into the Behavior
type.
To sum it up:
- You get explicit control over updates (the
changes
function), - but you need to learn a third data type
Discrete
, which almost duplicates theBehavior
type. - Even though the type
Behavior
is more fundamental, you will probably useDiscrete
more often.
That said, Discrete
is not a new primitive type, but built from exising types and combinators; you are encouraged to look at the source code.
If you are an FRP implementor, I encourage you to find a better solution. But if you are a user, you may want to accept the trade-off for now.
Discrete time-varying values
changes :: Discrete f a -> Event f aSource
Event that records when the value changes. Simultaneous events may be pruned for efficiency reasons.
value :: Discrete f a -> Behavior f aSource
Behavior corresponding to the value. It is always true that
value x = stepper (initial x) (changes x)
stepperD :: FRP f => a -> Event f a -> Discrete f aSource
Construct a discrete time-varying value from an initial value and a stream of new values.