Published
Edited
Aug 14, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function things_1(state = [], action) {
switch (action.type) {
case APPEND: return [...state, action.data]
case PREPEND: return [action.data, ...state]
default: return state
}
}
Insert cell
Insert cell
things_1(existing_state, {type: APPEND, data: 'zzz'})
Insert cell
Insert cell
Insert cell
things_2_reducers = ({
[APPEND]: (state, action) => [...state, action.data],
[PREPEND]: (state, action) => [action.data, ...state]
})
Insert cell
function things_2(state = [], action) {
return things_2_reducers.hasOwnProperty(action.type)
? things_2_reducers[action.type](state, action)
: state
}
Insert cell
Insert cell
things_2(existing_state, {type: PREPEND, data: 'zzz'})
Insert cell
Insert cell
Insert cell
Insert cell
endpoint = reducers => (state, action) => {
return reducers.hasOwnProperty(action.type)
? reducers[action.type](state, action)
: state
}
Insert cell
things_3 = endpoint(things_2_reducers)
Insert cell
Insert cell
things_3(existing_state, {type: APPEND, data: 'zzz'})
Insert cell
Insert cell
things_3(existing_state, {type: PREPEND, data: 'zzz'})
Insert cell
Insert cell
Insert cell
Insert cell
generic_append = (state, action) => [...state, action.data]
Insert cell
generic_prepend = (state, action) => [action.data, ...state]
Insert cell
things_4_reducers = ({
[APPEND]: generic_append,
[PREPEND]: generic_prepend
})
Insert cell
other_things_reducers = ({
'APPEND_OTHER_THING': generic_append,
'PREPEND_OTHER_THING': generic_prepend
})
Insert cell
Insert cell
Insert cell
Insert cell
endpoint_2 = (reducers, default_state = null) => (state, action) => {
const _state = state == null ? default_state : state
return reducers.hasOwnProperty(action.type)
? reducers[action.type](_state, action)
: _state
}
Insert cell
Insert cell
Insert cell
things_5 = endpoint_2(things_4_reducers, DEFAULT_THINGS_5)
Insert cell
things_5(null, {type: APPEND, data: 'zzz'})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more