Skip to content

Instantly share code, notes, and snippets.

@lupuszr
Created February 11, 2016 18:30
Show Gist options
  • Save lupuszr/8321b1e6adaaa0e1a248 to your computer and use it in GitHub Desktop.
Save lupuszr/8321b1e6adaaa0e1a248 to your computer and use it in GitHub Desktop.
Higher Order Reducer WIP
const horeducer = (callback,...reducers) => (state, action) => {
var fnamed ={}
reducers.map( r => fnamed[r.name]=s=> r(s,action) )
return callback(state,action,fnamed)
}
const red1 = (state=0,action) =>{
console.log("r1state");
console.log(state)
switch(action){
case 'INC':
return state+1
case 'DEC':
return state+1
default:
return state
}
}
var state1;
console.log(red1(state1,'INC'))
const red2 = (state={a:0},action) =>{
switch(action){
case 'ADD2':
state.a+=2
return state
default:
state
}
}
var state2;
console.log(red2(state2,'ADD2'))
const hored = (state, action, reducers) => {
console.log(reducers[0](state))
var s = {a:reducers[0](state)}
return reducers[1](s)
}
const hored2= (state, action, {red1,red2}) => {
console.log(red1(state))
var s = {a:red1(state)}
return red2(s)
}
var x = horeducer(hored2,red1,red2)
console.log(x(1,'ADD2'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment