Transducer

trait Transducer[Output]

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).

Companion
object
class Object
trait Matchable
class Any
Transducer[Output]

Type members

Types

type State

The type of the state used by this transducer. A type parameter as this isn't really needed outside of the transducer.

The type of the state used by this transducer. A type parameter as this isn't really needed outside of the transducer.

Value members

Abstract methods

The initial state for this Transducer.

The initial state for this Transducer.

def next(current: State): State

A method that constructs the next state given the current state. If the current state is a stopped state this method should always return that state.

A method that constructs the next state given the current state. If the current state is a stopped state this method should always return that state.

def output(state: State): Output

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.

def stopped(state: State): Boolean

True if this state is a stopped (or halt) state, meaning the transducer will never transition to a different state.

True if this state is a stopped (or halt) state, meaning the transducer will never transition to a different state.

Concrete methods

def ++(that: Transducer[Output]): Transducer[Output]

Append that transducer to this transducer, so that tranducer runs when this one has finished. Both transducers must produce output of the same type.

Append that transducer to this transducer, so that tranducer runs when this one has finished. Both transducers must produce output of the same type.

def and(that: Transducer[Output])(implicit m: Monoid[Output]): Transducer[Output]

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.

def andThen(f: Output => Transducer[Output]): Transducer[Output]

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.

def animate[Alg <: ([x[_]] =>> Algebra[x]), F[_], Frame, Canvas](frame: Frame)(implicit a: AnimationRenderer[Canvas], e: Renderer[Alg, F, Frame, Canvas], r: Redraw[Canvas], ev: Output <:< Picture[Alg, F, Unit], runtime: IORuntime): Unit

Convenience method to animate a transducer.

Convenience method to animate a transducer.

def foldLeft[B](zero: B)(f: (B, Output) => B): B
def foldRight[B](zero: Eval[B])(f: (Output, Eval[B]) => Eval[B]): Eval[B]
def map[B](f: Output => B): Transducer[B]

Transform the output of this transducer using the given function

Transform the output of this transducer using the given function

def product[B](that: Transducer[B]): Transducer[(Output, B)]

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.

def repeat(count: Int): Transducer[Output]

Construct a transducer by appending this transducer to itself the given number of times.

Construct a transducer by appending this transducer to itself the given number of times.

The count must be 0 or greater.

def scanLeft[B](zero: B)(f: (B, Output) => B): Transducer[B]

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.

def toStream: Stream[Pure, Output]

Convert this transducer to a fs2.Stream

Convert this transducer to a fs2.Stream

def traverse[G[_], B](f: Output => G[B])(implicit G: Applicative[G]): G[Transducer[B]]