essence-of-live-coding-0.2.6: General purpose live coding framework
Safe HaskellNone
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.

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

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

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

Outputs Just a whenever the the value a changes and Nothing otherwise. The first output is always Nothing. The following holds:

   delay a >>> changes >>> hold a == delay a
 

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

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

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

Hold the first value and output it indefinitely.

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

boundedFIFO n keeps the first n present values.

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

Buffers and returns the elements in First-In-First-Out order, returning Nothing whenever the buffer is empty.

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

Like fifo, but accepts lists as input. Each step is O(n) in the length of the list.

fifoFoldable :: (Monad m, Data a, Foldable f) => Cell m (f a) (Maybe a) Source #

Like fifoList, but generalised to any Foldable.

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.

Detecting change

onChange Source #

Arguments

:: (Monad m, Data p, Eq p) 
=> p

This parameter has to change during live coding to trigger an action

-> (p -> p -> a -> m b)

This action gets passed the old parameter and the new parameter

-> Cell m a (Maybe b) 

Perform an action whenever the parameter p changes, and the code is reloaded.

Note that this does not trigger any actions when adding, or removing an onChange cell. For this functionality, see LiveCoding.Handle. Also, when moving such a cell, the action may not be triggered reliably.

onChange' Source #

Arguments

:: (Monad m, Data p, Eq p) 
=> (p -> p -> a -> m b)

This action gets passed the old parameter and the new parameter

-> Cell m (p, a) (Maybe b) 

Like onChange', but with a dynamic input.