Skip to content

Instantly share code, notes, and snippets.

@charlesetc
Last active April 21, 2016 06:25
Show Gist options
  • Save charlesetc/683c036e550ea833a70be83dda5ff4e5 to your computer and use it in GitHub Desktop.
Save charlesetc/683c036e550ea833a70be83dda5ff4e5 to your computer and use it in GitHub Desktop.
impl<T> Monad<T, Vec<T>> for Vec<T> {
fn returns(inside: T) -> Self {
vec!(inside)
}
fn bind<'a>(outside: Self, op: Box<Fn(T) -> Self + 'a>) -> Self
{
outside
.into_iter()
.fold(vec![], |mut agg, x| {
let mut c = &mut op(x);
agg.append(c);
agg
})
}
}
impl<T> Monad<T, Vec<Vec<T>>> for Vec<T> {
fn returns(inside: T) -> Self {
vec!(inside)
}
fn bind<'a>(outside: Self, op: Box<Fn(T) -> Vec<Vec<T>> + 'a>) -> Vec<Vec<T>>
{
outside
.into_iter()
.fold(vec![], |mut agg, x| {
let mut c = &mut op(x);
agg.append(c);
agg
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment