Safe Haskell | None |
---|
This module is about segmented signals.
For a general introduction to signals, see the documentation of FRP.Grapefruit.Signal.
- data SSignal era val
- construct :: val -> DSignal era val -> SSignal era val
- fromInitAndUpdate :: val -> DSignal era val -> SSignal era val
- withInit :: Signal signal => SSignal era val -> (val -> signal era val') -> signal era val'
- updates :: SSignal era val -> DSignal era val
- scan :: accu -> (accu -> val -> accu) -> DSignal era val -> SSignal era accu
- consumer :: (val -> IO ()) -> Consumer SSignal val
- producer :: IO val -> (IO () -> Setup) -> Producer SSignal val
Segmented signal type
The type of segmented signals.
A segmented signal maps times to values like a continuous signal. However, it also comprises a set of discrete times, called update points. The signal can only change its value at its update points. As a special case, the starting time of the era is always considered an update point. So a segmented signal is composed of constant segments which are either bounded by adjacent update points or left-bounded by a last update point and right-unbounded. Note that value updates already take effect at the update point so that the segments are left-closed.
It follows that a segmented signal is completely determined by the update points and the values assigned to them. Therefore, a segmented signal can also be seen as a kind of discrete signal with occurences at the update points. The only difference to a discrete signal is that a segmented signal always has an occurence at the starting time of the era whereas a discrete signal never has one.
The dual nature of segmented signals is reflected by the class instances of SSignal
.
SSignal
is an instance of Samplee
as well as of Sampler
. The first means that it can
be sampled and therefore has a continuous aspect. The second means that it can be used to
sample a signal and therefore has a discrete aspect.
Construction
construct :: val -> DSignal era val -> SSignal era valSource
Constructs a segmented signal from an initial value and a series of updates.
A signal construct init upd
has initially the value init
. At each occurence in
upd
, it has an update point and changes its value to the value occuring in upd
. If
the segmented signal is interpreted as a kind of discrete signal, fromInitAndUpdate
just
adds an initial occurence of init
to the signal upd
.
fromInitAndUpdate :: val -> DSignal era val -> SSignal era valSource
Deprecated: fromInitAndUpdate is replaced by construct.
Same as construct
.
Queries
withInit :: Signal signal => SSignal era val -> (val -> signal era val') -> signal era val'Source
Applies the second argument to the initial value of the first argument.
Using withInit
, it is possible to create a signal which is dependent on the initial value
of a segmented signal but it is not possible to extract the initial value itself. The reason
for this restriction is that the initial value may depend on values of continuous signals
and therefore its calculation might involve doing I/O to read external continuous sources.
updates :: SSignal era val -> DSignal era valSource
Yields the sequence of updates of a segmented signal.
If the segmented signal is interpreted as a discrete signal with an additional occurence at
the start then update
just drops this occurence.
Stateful signals
scan :: accu -> (accu -> val -> accu) -> DSignal era val -> SSignal era accuSource
Accumulates the values of a discrete signal.
Applying scan init fun
to a discrete signal replaces its occurence values val_1
,
val_2
and so on by the values init `fun` val_1
, (init
`fun` val_1) `fun` val_2
and so on and adds an occurence of
the value init
at the beginning.
Connectors
consumer :: (val -> IO ()) -> Consumer SSignal valSource
Converts an event handler into a segmented signal consumer.
If a segmented signal is consumed with such a consumer, the handler is called at the
starting time of the era and at each update with the current value of the signal as its
argument. If the segmented signal is seen as a discrete signal with an additional occurence
at the start then consumer
behaves analogous to the consumer
function of
FRP.Grapefruit.Signal.Discrete.
:: IO val | an action reading the current value of the signal |
-> (IO () -> Setup) | an action which registers a given event handler so that it is called everytime the value of the signal has changed |
-> Producer SSignal val |
Converts a value read action and a change event handler registration into a segmented signal producer.