update-function-types
v2.1.2
Published
Provides a specification and basic type classes for update function. Essentially there are two kinds of update functions —
Downloads
29
Maintainers
Readme
Update Function Types
Provides a specification and basic type classes for update function. Essentially there are two kinds of update functions —
ReducerFunction
: takes inaction
andstate
and returns a newstate
.CommandFunction
: takes inaction
andstate
and return a newaction
.
Index
Types
Reducer Function
- Takes two arguments viz.
Value
andState
. - 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
andState
. - 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) // 10 * (10 + 20) === 300
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) // List(action('+', 510), action('B', 5000))
matchR
- Takes in specification object with keys as action types and values as
ReducerFunction
(s) and returns anotherReducerFunction
. - The returned function when called with an
Action
andState
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) // returns 1010
matchC
- Takes in specification object with keys as action types and values as
CommandFunction
(s) and returns anotherCommandFunction
. - The returned function when called with an
Action
andState
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 = matchR({
add: (a, b) => action('added', a + b),
mul: (a, b) => action('multiplied', a * b)
})
reducer(action('add', 10), 1000) // returns action('added', 1010)
zeroR
- Is a
ReducerFunction
. - Takes in an
action
and astate
and returns the same state.
zeroC
- Is a
CommandFunction
. - Takes in an
action
and astate
and returns the sameaction
.