Last active
January 4, 2017 16:11
-
-
Save xaviervia/42c5823aa5672373260d5e86d4aea091 to your computer and use it in GitHub Desktop.
Flux-like IncrementStore as a Monoid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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