Skip to content

Instantly share code, notes, and snippets.

@myzone
Last active February 23, 2016 18:10
Show Gist options
  • Save myzone/6e81d84a48235ae1a981 to your computer and use it in GitHub Desktop.
Save myzone/6e81d84a48235ae1a981 to your computer and use it in GitHub Desktop.
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;
};
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