essence-of-live-coding-0.2.5: General purpose live coding framework
Safe HaskellSafe-Inferred
LanguageHaskell2010

LiveCoding.Cell.Util

Synopsis

State accumulation

sumFrom :: Monad m => Integer -> Cell m Integer Integer Source #

Sum all past inputs, starting by the given number

count :: Monad m => Cell m a Integer Source #

Count the number of ticks, starting at 0

foldC :: (Data b, Monad m) => (a -> b -> b) -> b -> Cell m a b Source #

Accumulate all incoming data, using the given fold function and start value. For example, if foldC f b receives inputs a0, a1,... it will output b, f a0 b, f a1 $ f a0 b, and so on.

foldC' :: (Data b, Monad m) => (a -> b -> b) -> b -> Cell m a b Source #

Like foldC, but does not delay the output.

keep :: (Data a, Monad m) => a -> Cell m (Maybe a) a Source #

Initialise with a value a. If the input is Nothing, keep a will output the stored indefinitely. A new value can be stored by inputting Just a.

keepJust :: (Monad m, Data a) => Cell m (Maybe a) (Maybe a) Source #

Like keep, but returns Nothing until it is initialised by a Just a value.

boundedFIFO :: (Data a, Monad m) => Int -> Cell m (Maybe a) (Seq a) Source #

boundedFIFO n keeps the first n present values.

edge :: Monad m => Cell m Bool Bool Source #

Returns True iff the current input value is True and the last input value was False.

Debugging utilities

printTime :: MonadIO m => String -> m () Source #

Print the current UTC time, prepended with the first 8 characters of the given message.

printTimeC :: MonadIO m => String -> Cell m () () Source #

Like printTime, but as a cell.

Buffers

data BufferCommand a Source #

A command to send to buffer.

Constructors

Push a

Add an a to the buffer.

Pop

Remove the oldest element from the buffer.

maybePush :: Maybe a -> [BufferCommand a] Source #

Pushes Just a and does nothing on Nothing.

maybePop :: Maybe a -> [BufferCommand b] Source #

Pops on Just a and does nothing on Nothing.

buffer :: (Monad m, Data a) => Cell m [BufferCommand a] (Maybe a) Source #

Single-consumer, multi-producer buffer.

The output value is the oldest value in the buffer, if it exists.

  • Add elements by inputting Push a.
  • Remove elements by inputting Pop.

buffered :: (Monad m, Data a) => Cell m (Maybe a) (Maybe b) -> Cell m (Maybe a) (Maybe b) Source #

Equip a Cell with a buffer.

  • Whenever Just a value enters buffered cell, it is added to the buffer.
  • Whenever cell emits Just b, the oldest value is dropped from the buffer.
  • cell is always fed with Just the oldest value from the buffer, except when the buffer is empty, then it is fed Nothing.

This construction guarantees that cell produces exactly one output for every input value.