@action-land/tarz
Provides a specification and basic type classes for update function.
Essentially there are two kinds of update functions —
ReducerFunction
: takes in action
and state
and returns a new state
.CommandFunction
: takes in action
and state
and return a new action
.
Index
Types
Reducer Function
- Takes two arguments viz.
Value
and State
. - Always return a
State
. - Does not mutate the original objects provided.
export type ReducerFunction<Value, State> = {
(value: Value, state: State): State
}
Command Function
- Takes two arguments only viz.
Value
and State
. - Always returns an
Action
. It can consider returning Nil if no meaningful value is intended. - Does not mutate the original objects provided.
export type CommandFunction<Value, State, Output> = {
(value: Value, state: State): Action<Output>
}
Library Functions
concatR
Takes in multiple ReducerFunction
(s) as arguments and returns a new ReducerFunction
as a result.
- Consider the
ReducerFunction
(s) R0
R1
then two statements will be logically equivalent —
concatR(R0, R1)(action, state)
R1(action, R0(action, state))
- Its associative —
concatR(concatR(R0, R1), R2) === concatR(R0, concatR(R1, R2))
- Its additive —
concatR(R0, zeroR) === R0
.
Usage
import {concatR} from 'update-function-type'
const R0 = (a, b) => a + b
const R1 = (a, b) => a * b
concatR(R0, R1)(10, 20)
concatC
Takes in multiple CommandFunction
(s) as arguments and returns a new CommandFunction
as a result.
- Consider the
CommandFunction
(s) C0
C1
then two statements will be logically equivalent —
concatC(C0, C1)(action, state)
List(C0(action, state), C1(action, state))
- Its associative —
concatC(concatC(C0, C1), C2) === concatC(C0, concatC(C1, C2))
- Its additive —
concatC(C0, zeroC) === C0
Usage
import {concatC} from 'update-function-type'
const C0 = (a, b) => action('+', a + b)
const C1 = (a, b) => action('*', a * b)
concatC(C0, C1)(10, 500)
matchR
- Takes in specification object with keys as action types and values as
ReducerFunction
(s) and returns another ReducerFunction
. - The returned function when called with an
Action
and State
internally calls the function matching the action's type. - The matching reducer function is called with value of the original action and the state is passed as is.
- In case no action matches the given specification the original state is returned.
Usage
import {matchR} from 'update-function-type'
const reducer = matchR({
add: (a, b) => a + b,
mul: (a, b) => a * b
})
reducer(action('add', 10), 1000)
matchC
- Takes in specification object with keys as action types and values as
CommandFunction
(s) and returns another CommandFunction
. - The returned function when called with an
Action
and State
internally calls the function matching the action's type. - The matching reducer function is called with value of the original action and the state is passed as is.
- In case no action matches the given specification Nil is returned.
Usage
import {matchC} from 'update-function-type'
const reducer = matchC({
add: (a, b) => action('added', a + b),
mul: (a, b) => action('multiplied', a * b)
})
reducer(action('add', 10), 1000)
zeroR
- Is a
ReducerFunction
. - Takes in an
action
and a state
and returns the same state.
zeroC
- Is a
CommandFunction
. - Takes in an
action
and a state
and returns the same action
.