mvflow / net.pedroloureiro.mvflow / Reducer

Reducer

typealias Reducer<State, Mutation> = (State, Mutation) -> State (source)

Reducer applies a Mutation to a State, returning the resulting State.

Reducer invocations can't run in parallel so make sure this method returns quickly. It should only contain very simple logic to apply the mutation.

data class State(val counter: Int)
sealed class Mutation {
    data class Add(val amount: Int)
    data class Multiply(val amount: Int)
}
val reducer: Reducer<State, Mutation> = {
    state, mutation ->
    when (mutation) {
        is Add -> State(state.counter + mutation.value)
        is Multiply -> State(state.counter * mutation.value)
    }
}