Skip to content

Instantly share code, notes, and snippets.

@xaviervia
Last active January 4, 2017 16:11
Show Gist options
  • Save xaviervia/42c5823aa5672373260d5e86d4aea091 to your computer and use it in GitHub Desktop.
Save xaviervia/42c5823aa5672373260d5e86d4aea091 to your computer and use it in GitHub Desktop.
Flux-like IncrementStore as a Monoid
const IncrementStore = state => ({
state,
concat: ({state: delta}) => {
switch (delta.type) {
case 'ADD':
return IncrementStore({
value: state.value + 1
})
case 'REMOVE':
return IncrementStore({
value: state.value - 1
})
default:
return IncrementStore(state)
}
},
fold: f => f(state)
})
IncrementStore.empty = () => IncrementStore({})
IncrementStore({value: 1}).concat(IncrementStore({type: 'ADD'}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment