Skip to content

Instantly share code, notes, and snippets.

@Geal
Last active August 29, 2015 14:15
Show Gist options
  • Save Geal/20d25213e519c07b696b to your computer and use it in GitHub Desktop.
Save Geal/20d25213e519c07b696b to your computer and use it in GitHub Desktop.
pub type Fun<'a, I, O> = Box<FnMut(I) -> Res<'a, I, O> +'a>;
pub enum Res<'a, I, O> {
Done(I, O),
Incomplete(Fun<'a, I, O>)
}
pub trait FMap<I, O, N> {
fn fmap<'y,F: Fn(O) -> N>(&'y self, f: F) -> Res<'y, I, N>;
}
impl<'z, R, S, T> FMap<R, S, T> for Res<'z, R, S> {
fn fmap<'y,F: Fn(S) -> T>(&'y self, f: F) -> Res<'y, R, T> {
match self {
&Res::Incomplete(ref g) => {
Res::Incomplete(Box::new(move |input| {
let tmp = g(input);
let res = tmp.fmap(f);
res
}))
},
&Res::Done(i, ref o) => Res::Done(i,f(*o))
}
}
}
fn main() {
}
pub type Fun<'a> = Box<FnMut(u8) -> Res<'a> +'a>;
pub enum Res<'a> {
Done(u8, u8),
Incomplete(Fun<'a>)
}
pub trait FMap {
fn fmap<'y,F: Fn(u8) -> u8>(&'y self, f: F) -> Res<'y>;
}
impl<'z> FMap for Res<'z> {
fn fmap<'y,F: Fn(u8) -> u8>(&'y self, f: F) -> Res<'y> {
match self {
&Res::Incomplete(ref g) => {
Res::Incomplete(Box::new(move |input| {
let tmp = g(input);
let res = tmp.fmap(f);
res
}))
},
&Res::Done(i, o) => Res::Done(i,f(o))
}
}
}
fn main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment