fold

fun <A, B> fold(initial: A, events: Event<B>, reducer: SampleScope.(A, B) -> A): Signal<A>(source)

Construct a Signal by suppling an initial value, a set of events driving the updates of the Signal, together with a reducer describing how new events update the existing state.

Example:

enum class CounterEvent {
Increment,
Decrement;
}

val events: Event<CounterEvent> = ...

val counter: State<Int> = State.fold(0, incrementEvents) { state, event ->
when (event) {
is CounterEvent.Increment -> state + 1
is CounterEvent.Decrement -> state - 1
}
}

fun <A> fold(initial: A, vararg actions: Event<(A) -> A>): Signal<A>(source)