Last active
February 23, 2016 18:10
-
-
Save myzone/6e81d84a48235ae1a981 to your computer and use it in GitHub Desktop.
This file contains 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 applier = (rules, reactions) => { | |
const apply = (state, events) => { | |
return R.reduce((state, event) => { | |
const reaction = reactions(state, event); | |
const next = rules(state, event); | |
return apply(next, reaction); | |
}, state, events); | |
}; | |
return apply; | |
}; |
This file contains 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
fn apply<T, S, A, R, I: Iterator<Item=&S>> (applier: &A, reactor: &R, initial_state: T, signals: I) -> T where | |
A: FnMut(&T, &S) -> T, | |
R: FnMut(&T, &S) -> Iterator<Item=S> { | |
return signals | |
.iter() | |
.fold(initial_state, |state, signal| { | |
let reaction = reactor(&state, &signal); | |
let next_state = applier(&state, &signal); | |
return apply(applier, reactor, next_state, reaction); | |
}); | |
} | |
fn main() { | |
let app = |st, sig| { | |
return st + sig; | |
}; | |
let reatc = |st, sig| { | |
return 0..st; | |
}; | |
let r = apply(&app, &reatc, 0, 1..5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment