Safe Haskell | None |
---|---|

Language | Haskell98 |

This module is about discrete signals.

For a general introduction to signals, see the documentation of FRP.Grapefruit.Signal.

- data DSignal era val
- empty :: DSignal era val
- union :: DSignal era val -> DSignal era val -> DSignal era val
- unionWith :: (val -> val -> val) -> DSignal era val -> DSignal era val -> DSignal era val
- transUnion :: (val1 -> val') -> (val2 -> val') -> (val1 -> val2 -> val') -> DSignal era val1 -> DSignal era val2 -> DSignal era val'
- unions :: [DSignal era val] -> DSignal era val
- unionsWith :: (val -> val -> val) -> [DSignal era val] -> DSignal era val
- difference :: DSignal era val1 -> DSignal era val2 -> DSignal era val1
- differenceWith :: (val1 -> val2 -> Maybe val1) -> DSignal era val1 -> DSignal era val2 -> DSignal era val1
- intersection :: DSignal era val1 -> DSignal era val2 -> DSignal era val1
- intersectionWith :: (val1 -> val2 -> val') -> DSignal era val1 -> DSignal era val2 -> DSignal era val'
- map :: (val -> val') -> DSignal era val -> DSignal era val'
- filter :: (val -> Bool) -> DSignal era val -> DSignal era val
- catMaybes :: DSignal era (Maybe val) -> DSignal era val
- mapMaybe :: (val -> Maybe val') -> DSignal era val -> DSignal era val'
- scan :: accu -> (accu -> val -> accu) -> DSignal era val -> DSignal era accu
- scan1 :: (val -> val -> val) -> DSignal era val -> DSignal era val
- stateful :: state -> DSignal era (state -> (val, state)) -> DSignal era val
- consumer :: (val -> IO ()) -> Consumer DSignal val
- producer :: ((val -> IO ()) -> Setup) -> Producer DSignal val

# Discrete signal type

The type of discrete signals.

A discrete signal is a sequence of values assigned to discrete times. A pair of a time and a
corresponding value is called an occurrence. You can think of `DSignal `

as being
equivalent to *era* *val*

where `Map`

(Time *era*) *val*`Time `

is the type of all times of
the given era. However, an occurence at the starting time of the era is not possible. In
contrast to *era*`Map`

, a discrete signal may cover infinitely many values.

Discrete signals can describe sequences of events. For example, the sequence of all key presses could be described by a discrete signal of characters. Discrete signals are also used in conjunction with sampling.

The discrete signal instances of `Functor`

and `Monoid`

provide the following method
definitions:

`fmap`

=`map`

`mempty`

=`empty`

`mappend`

=`union`

`mconcat`

=`unions`

# Empty signal

# Combination

## Union

union :: DSignal era val -> DSignal era val -> DSignal era val Source #

Constructs the left-biased union of two discrete signals.

`union`

is equivalent to

.`unionWith`

const

unionWith :: (val -> val -> val) -> DSignal era val -> DSignal era val -> DSignal era val Source #

Constructs the union of two discrete signals, combining simultaneously occuring values via a combining function.

`unionWith`

is equivalent to

.`transUnion`

id id

transUnion :: (val1 -> val') -> (val2 -> val') -> (val1 -> val2 -> val') -> DSignal era val1 -> DSignal era val2 -> DSignal era val' Source #

Union with conversion and combination.

At each time, a signal

or a signal *dSignal1*

has an occurence, the signal*dSignal2*

transUnionconv1conv2combdSignal1dSignal2

has an occurence, too. The value of this occurence is formed as follows:

*conv1**val1*- if

has an occurence of value*dSignal1*

and*val1*

has no occurence*dSignal2* *conv2**val2*- if

has an occurence of value*dSignal2*

and*val2*

has no occurence*dSignal1* *comb**val1**val2*- if

has an occurence of value*dSignal1*

and*val1*

has an occurence of value*dSignal2**val2*

unions :: [DSignal era val] -> DSignal era val Source #

Repeated left-biased union.

`unions`

is equivalent to `foldl `

and `union`

`empty`

.`unionsWith`

const

unionsWith :: (val -> val -> val) -> [DSignal era val] -> DSignal era val Source #

## Difference

difference :: DSignal era val1 -> DSignal era val2 -> DSignal era val1 Source #

Constructs the difference of two discrete signals.

`difference`

is equivalent to

.`differenceWith`

(\_ _ -> Nothing)

differenceWith :: (val1 -> val2 -> Maybe val1) -> DSignal era val1 -> DSignal era val2 -> DSignal era val1 Source #

Constructs a kind of difference of two discrete signals where occurences may be modified instead of being dropped.

At each time, a signal

has an occurence of a value *dSignal1*

, the signal
*val1*`differenceWith `

has*comb* *dSignal1* *dSignal*

- an occurence of
*val1* - if

has no occurence*dSignal2* - an occurence of
*val'* - if

has an occurence of a value*dSignal2*

and*val2**comb**val1**val2*= Just*val'* - no occurence
- if

has an occurence of a value*dSignal2*

and*val2**comb**val1**val2*= Nothing

## Intersection

intersection :: DSignal era val1 -> DSignal era val2 -> DSignal era val1 Source #

Constructs the left-biased intersection of two discrete signals.

`intersection`

is equivalent to

.`intersectionWith`

const

intersectionWith :: (val1 -> val2 -> val') -> DSignal era val1 -> DSignal era val2 -> DSignal era val' Source #

Constructs the intersection of two discrete signals, combining values via a combining function.

# Mapping and filtering

map :: (val -> val') -> DSignal era val -> DSignal era val' Source #

Converts each value occuring in a discrete signal by applying a function to it.

filter :: (val -> Bool) -> DSignal era val -> DSignal era val Source #

Drops all occurence of a discrete signal whose values do not fulfill a given predicate.

catMaybes :: DSignal era (Maybe val) -> DSignal era val Source #

Converts all occurences with values of the form `Just `

into occurences with value
*val*

and drops all occurences with value *val*`Nothing`

.

# Stateful signals

scan :: accu -> (accu -> val -> accu) -> DSignal era val -> DSignal era accu Source #

Accumulates the values of a discrete signal, starting with a given initial value.

Applying `scan `

to a discrete signal replaces its occurence values *init* *fun*

,
*val_1*

and so on by the values *val_2*

, *init* `*fun*` *val_1*`(`

and so on.*init*
`*fun*` *val_1*) `*fun*` *val_2*

scan1 :: (val -> val -> val) -> DSignal era val -> DSignal era val Source #

Accumulates the values of a discrete signal, starting with the first occuring value.

Applying `scan1 `

to a discrete signal replaces its occurence values *init* *fun*

,
*val_1*

, *val_2*

and so on by the values *val_3*

, *val_1*

,
*val_1* `*fun*` *val_2*`(`

and so on.*val_1* `*fun*` *val_2*) `*fun*` *val_3*

stateful :: state -> DSignal era (state -> (val, state)) -> DSignal era val Source #

Constructs a discrete signal by repeatedly applying state transformers.

Applying `stateful `

to a discrete signal replaces its occurence values *init*

,
*trans_1*

, *trans_2*

and so on by the values *trans_3*`fst . `

, *trans_1* $ *init*```
fst .
```

, *trans_2* $ snd . *trans_1* $ *init*`fst . `

and so on.*trans_3* $ snd . *trans_2* $ snd . *trans_1*
$ *init*

# Connectors

consumer :: (val -> IO ()) -> Consumer DSignal val Source #

Converts an event handler into a discrete signal consumer.

If a discrete signal is consumed with such a consumer, the handler is called at each occurence with the occuring value as its argument.

producer :: ((val -> IO ()) -> Setup) -> Producer DSignal val Source #

Converts an event handler registration into a discrete signal producer.

Applying the argument of `producer`

to an event handler has to yield a setup which makes the
handler be called with a certain value everytime the produced signal shall have an
occurence of this value.