Safe Haskell | None |
---|
Oversimplified FRP library, using no concurrency primitives. As it's primarily intended to be used with haste-compiler, compiled to Javascript, this library is NOT thread-safe, though it could be made so by simply slamming a global lock around the write and newSinkID operations.
- data Signal a
- sink :: (a -> IO ()) -> Signal a -> IO ()
- new :: IO (Signal a) -> Signal a
- union :: Signal a -> Signal a -> Signal a
- accumS :: a -> Signal (a -> a) -> Signal a
- filterS :: (a -> Bool) -> Signal a -> Signal a
- whenS :: Signal Bool -> Signal a -> Signal a
- zipS :: Signal a -> Signal b -> Signal (a, b)
- untilS :: (a -> Bool) -> Signal a -> Signal b -> Signal a
- fromS :: (a -> Bool) -> Signal a -> Signal b -> Signal a
- stateful :: (st, a) -> Signal (st -> (st, a)) -> Signal a
- unions :: [Signal a] -> Signal a
- filterMapS :: (a -> Maybe b) -> Signal a -> Signal b
Documentation
new :: IO (Signal a) -> Signal aSource
Execute the specified IO action to obtain a new signal when registering
signals. This is handy when you're creating a signal from an external
event for use with a single sink:
clicked <- buttonSig my_button
sink (_ -> putStrLn Button clicked!) clicked
-- ...can be rewritten as:
sink (_ -> putStrLn Button clicked!) (new $ buttonSig my_button)
union :: Signal a -> Signal a -> Signal aSource
Create a signal that has the value of whichever parent signal fired last. The union is left biased.
accumS :: a -> Signal (a -> a) -> Signal aSource
Behaves pretty much like scanl on signals. Initialize the accumulator with a default value; every time the function signal triggers, apply the function to the accumulator and pass on the result.
filterS :: (a -> Bool) -> Signal a -> Signal aSource
Filter out events. filterS pred sig only lets the signal sig through if
it fulfills the predicate pred. For example:
(pa, a) <- pipe (0 :: Int)
(pb, b) <- pipe (0 :: Int)
let plus = (+) $ filterS (a <* b
sink (putStrLn . show) plus
write pa 20
write pb 20
write pa 5
The above code will print 20 and 25; writing 20 to pa gets filtered out,
as 20 does not fulfull (< 10) so no signal is fired. b isn't so filtered
however, so the 20 goes through just fine, and is added to the last good
value of a (which is 0 - its initial value). The final 5 does fulfill
(< 10), so the signal goes through and we get 25.
whenS :: Signal Bool -> Signal a -> Signal aSource
Only allow a signal to pass through when the time varying value is true.
zipS :: Signal a -> Signal b -> Signal (a, b)Source
Signal equivalent of the list function by the same name.
untilS :: (a -> Bool) -> Signal a -> Signal b -> Signal aSource
Pass through a signal as long as it does not fulfill a predicate. From the point when it does fulfill that predicate, the signal never propagates again unless the reset signal fired.
fromS :: (a -> Bool) -> Signal a -> Signal b -> Signal aSource
Don't pass the signal through until it fulfills a predicate. After the predicate has been fulfilled at least once, always propagate the signal.
stateful :: (st, a) -> Signal (st -> (st, a)) -> Signal aSource
Create a signal that keeps local state but returns another value.
filterMapS :: (a -> Maybe b) -> Signal a -> Signal bSource
Combined fmap and filter.