Skip to content

Instantly share code, notes, and snippets.

@a-eid
Last active November 14, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-eid/433caced2b3cd5df57a87fd10a674a38 to your computer and use it in GitHub Desktop.
Save a-eid/433caced2b3cd5df57a87fd10a674a38 to your computer and use it in GitHub Desktop.
pure reducer with map && filter (not complete)
let array = [1,2,3,4]
function reducer(state , action){
switch(action.type){
case "remove":
return state.filter(function(elem , index){
return index != action.id
})
case 'inc':
return state.map(function(elem , index){
return (index == action.id) ? elem + 1 : elem
})
}
}
action0 = {
type : 'remove',
id : 0
}
action1 = {
type : 'remove',
id : 1
}
action2 = {
type : 'inc',
id : 2
}
action3 = {
type : 'inc',
id : 3
}
action0 = {
type : 'inc',
id : 0
}
action1 = {
type : 'inc',
id : 1
}
action2 = {
type : 'inc',
id : 2
}
action3 = {
type : 'remove',
id : 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment