1 #![allow(non_camel_case_types)]
6 fn bind<B, F>(&self, f: F ) -> Vec<B> where F: FnMut(&A) -> Vec<B> ;
9 impl<A> vec_monad<A> for Vec<A> {
10 fn bind<B, F>(&self, mut f: F) -> Vec<B> where F: FnMut(&A) -> Vec<B> {
11 let mut r = Vec::new();
19 trait option_monad<A> {
20 fn bind<B, F>(&self, f: F) -> Option<B> where F: FnOnce(&A) -> Option<B>;
23 impl<A> option_monad<A> for Option<A> {
24 fn bind<B, F>(&self, f: F) -> Option<B> where F: FnOnce(&A) -> Option<B> {
26 Some(ref a) => { f(a) }
32 fn transform(x: Option<isize>) -> Option<String> {
33 x.bind(|n| Some(*n + 1) ).bind(|n| Some(n.to_string()) )
37 assert_eq!(transform(Some(10)), Some("11".to_string()));
38 assert_eq!(transform(None), None);
39 assert_eq!((vec!["hi".to_string()])
40 .bind(|x| vec![x.clone(), format!("{}!", x)] )
41 .bind(|x| vec![x.clone(), format!("{}?", x)] ),