Safe Haskell | None |
---|---|
Language | Haskell2010 |
FRP main types and combinators
Synopsis
- data Evt a
- once :: Run a -> Evt a
- never :: Evt a
- data Dyn a
- constDyn :: Run a -> Dyn a
- data DynRef a
- runDyn :: Dyn a -> Run (DynRef a)
- readDyn :: DynRef a -> Run a
- cancelDyn :: DynRef a -> Run ()
- newEvt :: Evt a -> Run (Evt a)
- newDyn :: Dyn a -> Run (Dyn a)
- scan :: (a -> b -> b) -> b -> Evt a -> Evt b
- scanMay :: (a -> b -> Maybe b) -> b -> Evt a -> Evt b
- mapMay :: (a -> Maybe b) -> Evt a -> Evt b
- foldMaps :: Monoid b => (a -> b) -> Evt a -> Evt b
- accum :: (a -> s -> (b, s)) -> s -> Evt a -> Evt b
- accumB :: a -> Evt (a -> a) -> Dyn a
- accumMay :: (a -> s -> Maybe (b, s)) -> s -> Evt a -> Evt b
- filters :: (a -> Bool) -> Evt a -> Evt a
- filterJust :: Evt (Maybe a) -> Evt a
- whens :: Dyn Bool -> Evt a -> Evt a
- splits :: Evt (Either a b) -> (Evt a, Evt b)
- lefts :: Evt (Either a b) -> Evt a
- rights :: Evt (Either a b) -> Evt b
- iterates :: (a -> a) -> a -> Evt b -> Evt a
- withIterates :: (a -> a) -> a -> Evt b -> Evt (a, b)
- fix1 :: (Evt a -> Run (Evt a)) -> Evt a
- fix2 :: (Evt a -> Evt b -> Run (Evt a, Evt b)) -> (Evt a, Evt b)
- fix3 :: (Evt a -> Evt b -> Evt c -> Run (Evt a, Evt b, Evt c)) -> (Evt a, Evt b, Evt c)
- fix4 :: (Evt a -> Evt b -> Evt c -> Evt d -> Run (Evt a, Evt b, Evt c, Evt d)) -> (Evt a, Evt b, Evt c, Evt d)
- switch :: Evt (Evt a) -> Evt a
- joins :: Evt (Evt a) -> Evt a
- delay :: NominalDiffTime -> Evt a -> Evt a
- delayFork :: NominalDiffTime -> Evt a -> Evt a
- sums :: Num a => Evt a -> Evt a
- sumD :: Num a => Float -> Dyn a -> Dyn a
- integrate :: (VectorSpace v, Scalar v ~ Float) => Float -> Dyn v -> Dyn v
- integrate2 :: (VectorSpace v, Scalar v ~ Float) => Float -> Dyn v -> Dyn v
- products :: Num a => Evt a -> Evt a
- count :: Evt a -> Evt Int
- withCount :: Evt a -> Evt (Int, a)
- appends :: Monoid a => Evt a -> Evt a
- takes :: Int -> Evt a -> Evt a
- drops :: Int -> Evt a -> Evt a
- takesWhile :: (a -> Bool) -> Evt a -> Evt a
- dropsWhile :: (a -> Bool) -> Evt a -> Evt a
- cycles :: [a] -> Evt b -> Evt a
- listAt :: [a] -> Evt Int -> Evt a
- toToggle :: Evt a -> Evt Bool
- races :: Evt a -> Evt a -> Evt a
- forks :: Evt a -> Evt a
- forevers :: Evt a -> Evt a
- folds :: Monoid a => Evt a -> Run a
- foldls :: (b -> a -> b) -> b -> Evt a -> Run b
- foldls' :: (b -> a -> Run b) -> b -> Evt a -> Run b
- foldrs :: (a -> b -> b) -> b -> Evt a -> Run b
- foldrs' :: (a -> b -> Run b) -> b -> Evt a -> Run b
- prints :: Show a => Evt a -> Run ()
- putStrLns :: Evt String -> Run ()
- hold :: a -> Evt a -> Dyn a
- unhold :: Dyn a -> Evt a
- scanD :: (a -> b -> b) -> b -> Evt a -> Dyn b
- scanMayD :: (a -> b -> Maybe b) -> b -> Evt a -> Dyn b
- switchD :: Dyn a -> Evt (Dyn a) -> Dyn a
- switchDyn :: Dyn (Evt a) -> Evt a
- apply :: Dyn (a -> b) -> Evt a -> Evt b
- applyMay :: Dyn (a -> Maybe b) -> Evt a -> Evt b
- snap :: Dyn a -> Evt b -> Evt a
- attach :: Dyn a -> Evt b -> Evt (a, b)
- attachWith :: (a -> b -> c) -> Dyn a -> Evt b -> Evt c
- attachWithMay :: (a -> b -> Maybe c) -> Dyn a -> Evt b -> Evt c
- (<@>) :: Dyn (a -> b) -> Evt a -> Evt b
- (<@) :: Dyn a -> Evt b -> Evt a
- class RunFunctor f where
- foreach :: (a -> Run ()) -> Evt a -> Evt a
- posteach :: (a -> Run ()) -> Evt a -> Evt a
- iterates' :: (a -> Run a) -> a -> Evt b -> Evt a
- scan' :: (a -> b -> Run b) -> b -> Evt a -> Evt b
- scanMay' :: (a -> b -> Run (Maybe b)) -> b -> Evt a -> Evt b
- accum' :: (a -> s -> Run (b, s)) -> s -> Evt a -> Evt b
- accumMay' :: (a -> s -> Run (Maybe (b, s))) -> s -> Evt a -> Evt b
- filters' :: (a -> Run Bool) -> Evt a -> Evt a
- mapMay' :: (a -> Run (Maybe b)) -> Evt a -> Evt b
- apply' :: Dyn (a -> Run b) -> Evt a -> Evt b
- applyMay' :: Dyn (a -> Run (Maybe b)) -> Evt a -> Evt b
- mchanEvt :: Chan a -> Evt a
- tchanEvt :: TChan a -> Evt a
- uchanEvt :: InChan a -> Evt a
- clock :: NominalDiffTime -> Evt UTCTime
- pulse :: NominalDiffTime -> Evt ()
- ticks :: Float -> Evt Float
- timer :: Float -> Evt Float
- timerD :: Float -> Dyn Float
- toRandom :: Random b => Evt a -> Evt b
- toRandomR :: Random b => (b, b) -> Evt a -> Evt b
- withRandom :: Random b => Evt a -> Evt (b, a)
- withRandomR :: Random b => (b, b) -> Evt a -> Evt (b, a)
- oneOf :: [a] -> Evt b -> Evt a
- withOneOf :: [a] -> Evt b -> Evt (a, b)
- freqOf :: Dyn [(a, Rational)] -> Evt b -> Evt a
- withFreqOf :: Dyn [(a, Rational)] -> Evt b -> Evt (a, b)
- randSkip :: Dyn Double -> Evt a -> Evt a
- randSkipBy :: Dyn (a -> Double) -> Evt a -> Evt a
- liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
- liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
Events
Event stream
Instances
Monad Evt Source # | |
Functor Evt Source # | |
Applicative Evt Source # | |
RunFunctor Evt Source # | |
Semigroup (Evt a) Source # | |
Monoid (Evt a) Source # | |
Loop (Evt a) Source # | |
Defined in Dyna.Brick.Types | |
Limit (Evt a) Source # | |
Compose (Evt a) Source # | |
Defined in Dyna.Brick.Types | |
Harmony (Evt a) Source # | |
Melody (Evt a) Source # | |
type DurOf (Evt a) Source # | |
Defined in Dyna.Brick.Types |
Dynamics
Dynamic value that continuously changes over time
Instances
Functor Dyn Source # | |
Applicative Dyn Source # | |
RunFunctor Dyn Source # | |
Fractional a => Fractional (Dyn a) Source # | |
Num a => Num (Dyn a) Source # | |
IsString a => IsString (Dyn a) Source # | |
Defined in Dyna.Brick.Types fromString :: String -> Dyn a # | |
Semigroup a => Semigroup (Dyn a) Source # | |
Monoid a => Monoid (Dyn a) Source # | |
Boolean a => Boolean (Dyn a) Source # | |
IfB a => IfB (Dyn a) Source # | |
EqB a => EqB (Dyn a) Source # | |
OrdB a => OrdB (Dyn a) Source # | |
AdditiveGroup a => AdditiveGroup (Dyn a) Source # | |
VectorSpace a => VectorSpace (Dyn a) Source # | |
AffineSpace a => AffineSpace (Dyn a) Source # | |
HasCross3 a => HasCross3 (Dyn a) Source # | |
HasCross2 a => HasCross2 (Dyn a) Source # | |
Defined in Dyna.Brick.Types | |
HasNormal a => HasNormal (Dyn a) Source # | |
Defined in Dyna.Brick.Types | |
(BasisArity a, HasBasis a) => HasBasis (Dyn a) Source # | |
type BooleanOf (Dyn a) Source # | |
Defined in Dyna.Brick.Types | |
type Scalar (Dyn a) Source # | |
type Diff (Dyn a) Source # | |
type Basis (Dyn a) Source # | |
Observing the Dyn
Reference to running dynamic process by which we can query values (readDyn
).
Also note that we no longer need the reference we should release the resources
by calling cancelDyn
.
runDyn :: Dyn a -> Run (DynRef a) Source #
Executes dynamic for observation. The dynamic is step-wise constant function that is driven by some event stream. The function runs the event stream process in background and samples the updated state.
We can observe the value with readDyn
. We need to shut down the stream when
we no longer need it with cancelDyn
function.
cancelDyn :: DynRef a -> Run () Source #
Shuts down the background process for dynamic and releases resulrces for event stream that drives the dynamic.
Control
newEvt :: Evt a -> Run (Evt a) Source #
Runs the argument event stream as background process and produces event stream that is fed with events over channel (unagi-channel package). When result event stream shuts down the background process also shuts down.
newDyn :: Dyn a -> Run (Dyn a) Source #
Runs the dynamic process in background and returns dynamic
that just samples the background proces with readDyn
.
API
Event API
scan :: (a -> b -> b) -> b -> Evt a -> Evt b Source #
scan over event stream. Example:
naturals = scan (+) 0 pulse
scanMay :: (a -> b -> Maybe b) -> b -> Evt a -> Evt b Source #
scan combined with filter. If accumulator function produces Nothing
on event then
that event is ignored and state is kept to previous state.
mapMay :: (a -> Maybe b) -> Evt a -> Evt b Source #
Map with filtering. When Nothing
is produced event is omitted from the stream.
accumB :: a -> Evt (a -> a) -> Dyn a Source #
Accumulates the values with event stream that produce functions.
filters :: (a -> Bool) -> Evt a -> Evt a Source #
Filtering of the event strewams. Only events that produce True remain in the stream.
filterJust :: Evt (Maybe a) -> Evt a Source #
Filters based on Maybe. If Nothing
is produced forthe event it is omitted from the stream.
whens :: Dyn Bool -> Evt a -> Evt a Source #
Filters with dynamic. When dynamic is true events pass through and when it's false events are omitted.
iterates :: (a -> a) -> a -> Evt b -> Evt a Source #
Iterates over event stream. It's like scan but it ignores the values of underying stream and starts with initial value as first element.
withIterates :: (a -> a) -> a -> Evt b -> Evt (a, b) Source #
fix1 :: (Evt a -> Run (Evt a)) -> Evt a Source #
Recursion on event streams. As event streams are functions we can not use
normal recursion that haskell provides. It will stuck the execution.
But we can use fix1
to create event stream that feeds back the events to itself.
Note that any sort of recursion can be implemented with fix1
.
For example if we need 3-recursive event stream:
fix3 :: (Evt a -> Evt b -> Evt c -> (Evt a, Evt b, Evt c)) -> (Evt a, Evt b, Evt c)
we can use sum tpye tags to join it to single stream:
data Tag a b c = TagA a | TagB b | TagC c
fix3 f = unwrap $ fix1 g where g x = wrap (f (unwrapA x) (unwrapB x) (unwrapC x)) wrap a b c = mconcat [TagA <$> a, TagB <$> b, TagC <$> c] unwrap evt = (unwrapA evt, unwrapB evt, unwrapC evt) unwrapA = flip mapMay $ \x -> case x of TagA a -> Just a _ -> Nothing
We can use this trck with any number of streams. There are helper functions: fix2
, fix3
, fix4
fix2 :: (Evt a -> Evt b -> Run (Evt a, Evt b)) -> (Evt a, Evt b) Source #
Recursion for binary functions
fix3 :: (Evt a -> Evt b -> Evt c -> Run (Evt a, Evt b, Evt c)) -> (Evt a, Evt b, Evt c) Source #
Recursion for ternary functions
fix4 :: (Evt a -> Evt b -> Evt c -> Evt d -> Run (Evt a, Evt b, Evt c, Evt d)) -> (Evt a, Evt b, Evt c, Evt d) Source #
Recursion for functions of four arguments
switch :: Evt (Evt a) -> Evt a Source #
Flattens event stream producer by switching between event streams. When next event stream happens it shuts down the previous one.
joins :: Evt (Evt a) -> Evt a Source #
Joins event stream of streams. If stream is started it runs until the end.
delay :: NominalDiffTime -> Evt a -> Evt a Source #
Delays in the thread of execution. Note that it can interfere and screw up functions like clock, timer, pulse, ticks
delayFork :: NominalDiffTime -> Evt a -> Evt a Source #
Delays in background by forking on each event. Note tht if delayed event was put into background prior to stopping of the main event stream it will fire anyway. There is no way to stop it.
integrate2 :: (VectorSpace v, Scalar v ~ Float) => Float -> Dyn v -> Dyn v Source #
listAt :: [a] -> Evt Int -> Evt a Source #
Takes elements from the list by index. If index is out of bounds the event is omitted.
toToggle :: Evt a -> Evt Bool Source #
Turns event stream to toggle stream. It produce cyclic sequence of [True, False]
folds :: Monoid a => Evt a -> Run a Source #
Monoidal fold for event streams, note that stream have to be finite for the function to complete
foldls :: (b -> a -> b) -> b -> Evt a -> Run b Source #
Left fold for event streams, note that stream have to be finite for the function to complete
foldrs :: (a -> b -> b) -> b -> Evt a -> Run b Source #
Right fold for event streams, note that stream have to be finite for the function to complete
prints :: Show a => Evt a -> Run () Source #
Starts event stream process and as callback prints it values.
putStrLns :: Evt String -> Run () Source #
Starts event stream process and as callback prints it values.
Event/Dynamic interaction
hold :: a -> Evt a -> Dyn a Source #
Turns event stream to dynamic. It holds the values of events until the next event happen. It starts with initial value.
hold initVal events = ...
unhold :: Dyn a -> Evt a Source #
Turns dynamic into event stream of underlying events that trigger dynamic updates.
scanD :: (a -> b -> b) -> b -> Evt a -> Dyn b Source #
scans over event stream and converts it to dynamic.
scanMayD :: (a -> b -> Maybe b) -> b -> Evt a -> Dyn b Source #
Dynamic scan that can also filter out events. If Nothing is produced then the event is skipped.
switchDyn :: Dyn (Evt a) -> Evt a Source #
Queries the event stream form dynamic and runs it all next event streams are ignored.
apply :: Dyn (a -> b) -> Evt a -> Evt b Source #
Applies a function to event stream value. The function is sampled from dynamic process.
snap :: Dyn a -> Evt b -> Evt a Source #
Snapshot of dynamic process with event stream. All values in the event stream are substituted with current value of dynamic.
attach :: Dyn a -> Evt b -> Evt (a, b) Source #
Kind of zipWith
function for dynamics and event streams.
attachWith :: (a -> b -> c) -> Dyn a -> Evt b -> Evt c Source #
Kind of zipWith
function for dynamics and event streams.
attachWithMay :: (a -> b -> Maybe c) -> Dyn a -> Evt b -> Evt c Source #
Attach with filtering. When Nothing
is produced event is omitted from the stream.
Effectful API
class RunFunctor f where Source #
foreach :: (a -> Run ()) -> Evt a -> Evt a Source #
Adds some procedure to callback. Procedure is called prior to callback execution.
posteach :: (a -> Run ()) -> Evt a -> Evt a Source #
Adds some procedure to callback. Procedure is called after callback execution.
scan' :: (a -> b -> Run b) -> b -> Evt a -> Evt b Source #
scan over event stream with effectful function.
scanMay' :: (a -> b -> Run (Maybe b)) -> b -> Evt a -> Evt b Source #
scan combined with filter for effectful function. See scanMay
for details.
accumMay' :: (a -> s -> Run (Maybe (b, s))) -> s -> Evt a -> Evt b Source #
Accumulate over event stream.
mapMay' :: (a -> Run (Maybe b)) -> Evt a -> Evt b Source #
Map with filtering. When Nothing
is produced event is omitted from the stream.
Utilities
Channels (interaction with the world)
mchanEvt :: Chan a -> Evt a Source #
Creates the event stream that listens to MVar based channel. If any value is put chan the event stream fires the callback.
tchanEvt :: TChan a -> Evt a Source #
Creates the event stream that listens to MVar based channel. If any value is put chan the event stream fires the callback.
uchanEvt :: InChan a -> Evt a Source #
Creates the event stream that listens to unagi channel (package unagi-chan
).
If any value is put chan the event stream fires the callback.
Clock
clock :: NominalDiffTime -> Evt UTCTime Source #
Queries current time periodically with given period in seconds.
pulse :: NominalDiffTime -> Evt () Source #
Produces pulse events with given period in seconds.
ticks :: Float -> Evt Float Source #
Produces pulse events with given period in seconds and also tells how many seconds exactly has passed. It can be useful for simulations of models that are based on differential equations. As event streams carries how much time has passed between simulation steps.
timer :: Float -> Evt Float Source #
Timer behaves like tocks only it produces accumulated time since beginning of the process. It calculates them by querying current time and suntracting start time from it.
It can be though of as:
sumE ticks
Random
toRandom :: Random b => Evt a -> Evt b Source #
Substitutes values in event stream with random values.
toRandomR :: Random b => (b, b) -> Evt a -> Evt b Source #
Substitutes values in event stream with random values from the given range.
withRandom :: Random b => Evt a -> Evt (b, a) Source #
Substitutes values in event stream with random values.
withRandomR :: Random b => (b, b) -> Evt a -> Evt (b, a) Source #
Substitutes values in event stream with random values from the given range.
freqOf :: Dyn [(a, Rational)] -> Evt b -> Evt a Source #
Picks at random one element from the list. We also provide distribution of events. Probability to pick up the element. Sum of probabilities should equal to 1.
withFreqOf :: Dyn [(a, Rational)] -> Evt b -> Evt (a, b) Source #
Picks at random one element from the list. We also provide distribution of events. Probability to pick up the element. Sum of probabilities should equal to 1.
randSkip :: Dyn Double -> Evt a -> Evt a Source #
Skips at random elements from the list. We provide frequency to skip events with dynamic first argument.
randSkipBy :: Dyn (a -> Double) -> Evt a -> Evt a Source #
Skips elements at random. The probability to skip element depends on the element itself.
Re-exports
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c #
Lift a binary function to actions.
Some functors support an implementation of liftA2
that is more
efficient than the default one. In particular, if fmap
is an
expensive operation, it is likely better to use liftA2
than to
fmap
over the structure and then use <*>
.
This became a typeclass method in 4.10.0.0. Prior to that, it was
a function defined in terms of <*>
and fmap
.
Using ApplicativeDo
: '
' can be understood
as the liftA2
f as bsdo
expression
do a <- as b <- bs pure (f a b)
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d #
Lift a ternary function to actions.
Using ApplicativeDo
: '
' can be understood
as the liftA3
f as bs csdo
expression
do a <- as b <- bs c <- cs pure (f a b c)