frpnow-0.18: Principled practical FRP

Copyright(c) Atze van der Ploeg 2015
LicenseBSD-style
Maintaineratzeus@gmail.org
Stabilityprovisional
Portabilityportable
Safe HaskellNone
LanguageHaskell98

Control.FRPNow.EvStream

Contents

Description

Event streams for FRPNow

Synopsis

Documentation

data EvStream a Source

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

emptyEs :: EvStream a Source

The empty event stream

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

catMaybesEs :: EvStream (Maybe a) -> EvStream a Source

Filter the Just values from an event stream.

filterEs :: (a -> Bool) -> EvStream a -> EvStream a Source

Filter events from an event stream

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.

delay Source

Arguments

:: EvStream x

The event stream that functions as the `clock'

-> 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.

Debug

traceEs :: (Show a, Eq a) => String -> EvStream a -> Now () Source

Debug function, print all values in the event stream to stderr, prepended with the given string.