A Transducer represents an animation that starts in some initial state and
proceeds through various states until it stops. For example, a square might
move from an x position of 0 to an x position of 100 in increments of 10.
The states then become 0, 10, 20, ..., 100.
More abstractly, a transducer is a finite state machine with an additional
set of output values, so that each state is associated with some output
value. Continuing the example of the moving square, the states are the x
position and the output is the square at the given position (for some fixed
y coordinate).
Transducers should be treated like half open intervals, which means they
should generate the inital state but avoid generating a stopping state when
possible (for example, when combined in sequence with another transducer).
Transducers have several type classes instances:
Traverse
Applicative
Monoid, corresponding to sequential composition (++)
The majority of the API is provided by the Cats methods defined on these
type classes, so import cats.implicits._ to get a richer API (e.g. toList,
filter, etc.)
There is another possible monoid, which corresponds to parallel composition
(and), if there is a monoid on the type A. Taken with ++ this makes
Transducers a Rig or Semiring (depending on how one defines these terms;
they are not always defined in the same way).
A method that returns the output of the current state. If the transducer
has stopped it may not have any output, in which case it can throw a
java.util.NoSuchElementException. As a result, clients should avoid
calling this method when the transducer is in a stopped state. If possible
this method should return some other sensible result, rather than throwing
an exception, if given a stopped state.
A method that returns the output of the current state. If the transducer
has stopped it may not have any output, in which case it can throw a
java.util.NoSuchElementException. As a result, clients should avoid
calling this method when the transducer is in a stopped state. If possible
this method should return some other sensible result, rather than throwing
an exception, if given a stopped state.
Create a transducer that runs this transducer in parallel with that
transducer, stopping when both have stopped. Both transducers must produce
output of the same type, and there must be a monoid instance for the
output type.
Create a transducer that runs this transducer in parallel with that
transducer, stopping when both have stopped. Both transducers must produce
output of the same type, and there must be a monoid instance for the
output type.
If one transducer stops before the other then its last output before
stopping is returned as its output until the other transducer stops. If it
stops before generating output (i.e. its initial state is a stopping
state) than the zero / identity of the monoid is used as its output. This
behaviour is usually what we want for animations, and it makes and a
monoid instance for transducer with empty as the identity. To stop when
either have stopped see product.
When this transducer's next state would be a stopped state, transition to
the tranducer created by calling the given function with the current
output. If this transducer immediately stops, and hence has no output,
there will be no output to pass to the function and therefore the next
transducer will not be created.
When this transducer's next state would be a stopped state, transition to
the tranducer created by calling the given function with the current
output. If this transducer immediately stops, and hence has no output,
there will be no output to pass to the function and therefore the next
transducer will not be created.
This is like append (++) but allows the final output to determine the
transducer that is appended.
Create a transducer that runs this transducer in parallel with that
transducer, stopping when either has stopped. To stop when both have
stopped see and.
Create a transducer that runs this transducer in parallel with that
transducer, stopping when either has stopped. To stop when both have
stopped see and.
Create a transducer that outputs the cumulative results of applying the
function f to the output of the underlying transducer. If the underlying
transducer has stopped the zero value is produced as the only output.
Create a transducer that outputs the cumulative results of applying the
function f to the output of the underlying transducer. If the underlying
transducer has stopped the zero value is produced as the only output.