Copyright | (c) Atze van der Ploeg 2015 |
---|---|

License | BSD-style |

Maintainer | atzeus@gmail.org |

Stability | provisional |

Portability | portable |

Safe Haskell | None |

Language | Haskell98 |

Event streams for FRPNow

- data EvStream a
- next :: EvStream a -> Behavior (Event a)
- nextAll :: EvStream a -> Behavior (Event [a])
- emptyEs :: EvStream a
- merge :: EvStream a -> EvStream a -> EvStream a
- collapseSimul :: EvStream a -> EvStream [a]
- dropEv :: Int -> EvStream a -> EvStream a
- toChanges :: Eq a => Behavior a -> EvStream a
- edges :: Behavior Bool -> EvStream ()
- joinEs :: Event (EvStream b) -> EvStream b
- scanlEv :: (a -> b -> a) -> a -> EvStream b -> Behavior (EvStream a)
- foldrEv :: (a -> Event b -> b) -> EvStream a -> Behavior (Event b)
- foldriEv :: a -> (a -> Event b -> b) -> EvStream a -> Behavior b
- fromChanges :: a -> EvStream a -> Behavior (Behavior a)
- foldrSwitch :: Behavior a -> EvStream (Behavior a) -> Behavior (Behavior a)
- foldEs :: (a -> b -> a) -> a -> EvStream b -> Behavior (Behavior a)
- foldBs :: Behavior a -> (Behavior a -> b -> Behavior a) -> EvStream b -> Behavior (Behavior a)
- catMaybesEs :: EvStream (Maybe a) -> EvStream a
- filterEs :: (a -> Bool) -> EvStream a -> EvStream a
- filterMapEs :: (a -> Maybe b) -> EvStream a -> EvStream b
- filterMapEsB :: Behavior (a -> Maybe b) -> EvStream a -> EvStream b
- filterB :: Behavior (a -> Bool) -> EvStream a -> EvStream a
- during :: EvStream a -> Behavior Bool -> EvStream a
- beforeEs :: EvStream a -> Event () -> EvStream a
- (<@@>) :: Behavior (a -> b) -> EvStream a -> EvStream b
- snapshots :: Behavior a -> EvStream () -> EvStream a
- delay :: EvStream x -> a -> Behavior a -> Behavior (Behavior a)
- callbackStream :: forall a. Now (EvStream a, a -> IO ())
- callStream :: ([a] -> Now ()) -> EvStream a -> Now ()
- callIOStream :: (a -> IO ()) -> EvStream a -> Now ()
- traceEs :: (Show a, Eq a) => String -> EvStream a -> Now ()

# Documentation

The (abstract) type of event streams.

Denotationally, one can think of an eventstream a value of type

[(Time,a)]

Where the points in time are non-strictly increasing. There can be multiple simulatinous events in an event stream.

# Observe

next :: EvStream a -> Behavior (Event a) Source

Obtain the next element of the event stream. The obtained event is guaranteed to lie in the future.

nextAll :: EvStream a -> Behavior (Event [a]) Source

Obtain all simultaneous next elements of the event stream. The obtained event is guaranteed to lie in the future.

# Construction

merge :: EvStream a -> EvStream a -> EvStream a Source

Merge two event stream.

In case of simultaneity, the left elements come first

collapseSimul :: EvStream a -> EvStream [a] Source

Collapses each set simultanious events into a single event carrying the list of occurances.

toChanges :: Eq a => Behavior a -> EvStream a Source

Get the event stream of changes to the input behavior.

edges :: Behavior Bool -> EvStream () Source

Get the events that the behavior changes from `False`

to `True`

joinEs :: Event (EvStream b) -> EvStream b Source

Turns an event of an event stream into an event stream.

# Folds and scans

scanlEv :: (a -> b -> a) -> a -> EvStream b -> Behavior (EvStream a) Source

A left scan over an event stream

foldrEv :: (a -> Event b -> b) -> EvStream a -> Behavior (Event b) Source

Right fold over an eventstream

The result of folding over the rest of the event stream is in an event, since it can be only known in the future.

No initial value needs to be given, since the initial value is `never`

foldriEv :: a -> (a -> Event b -> b) -> EvStream a -> Behavior b Source

Right fold over an eventstream with a left initial value

Defined as:

foldriEv i f ev = f i <$> foldrEv f es

fromChanges :: a -> EvStream a -> Behavior (Behavior a) Source

Create a behavior from an initial value and a event stream of updates.

foldrSwitch :: Behavior a -> EvStream (Behavior a) -> Behavior (Behavior a) Source

Start with the argument behavior, and switch to a new behavior each time an event in the event stream occurs.

Defined as:

foldrSwitch b = foldriEv b switch

foldEs :: (a -> b -> a) -> a -> EvStream b -> Behavior (Behavior a) Source

Left fold over an eventstream to create a behavior (behavior depends on when the fold started).

foldBs :: Behavior a -> (Behavior a -> b -> Behavior a) -> EvStream b -> Behavior (Behavior a) Source

Yet another type of fold.

Defined as:

foldBs b f es = scanlEv f b es >>= foldrSwitch b

# Filter and scan

filterMapEs :: (a -> Maybe b) -> EvStream a -> EvStream b Source

Shorthand for

filterMapEs f e = catMaybesEs $ f <$> e

filterMapEsB :: Behavior (a -> Maybe b) -> EvStream a -> EvStream b Source

Shorthand for

filterMapEs b e = catMaybesEs $ b <@@> e

filterB :: Behavior (a -> Bool) -> EvStream a -> EvStream a Source

Filter events from an eventstream based on a function that changes over time

during :: EvStream a -> Behavior Bool -> EvStream a Source

Obtain only the events from input stream that occur while
the input behavior is `True`

beforeEs :: EvStream a -> Event () -> EvStream a Source

An event stream with only elements that occur before the argument event.

# Combine behavior and eventstream

(<@@>) :: Behavior (a -> b) -> EvStream a -> EvStream b Source

Sample the behavior each time an event in the stream occurs, and combine the outcomes.

snapshots :: Behavior a -> EvStream () -> EvStream a Source

Sample the behavior each time an event in the stream occurs.

:: EvStream x | The event stream that functions as the ` |

-> a | The inital value of the output behavior |

-> Behavior a | The input behavior |

-> Behavior (Behavior a) |

Delay a behavior by one tick of the ``clock'`

.

The event stream functions as the ``clock'`

: the input behavior is sampled on each
event, and the current value of the output behavior is always the previously sample.

Occasionally useful to prevent immediate feedback loops.

# IO interface

callbackStream :: forall a. Now (EvStream a, a -> IO ()) Source

Create an event stream that has an event each time the returned function is called. The function can be called from any thread.

callStream :: ([a] -> Now ()) -> EvStream a -> Now () Source

Call the given function each time an event occurs, and execute the resulting Now computation

callIOStream :: (a -> IO ()) -> EvStream a -> Now () Source

Execute the given IO action each time an event occurs. The IO action is executed on the main thread, so it should not take a long time.