3 #![allow(non_camel_case_types)]
8 fn bind<B, F>(&self, f: F ) -> Vec<B> where F: FnMut(&A) -> Vec<B> ;
11 impl<A> vec_monad<A> for Vec<A> {
12 fn bind<B, F>(&self, mut f: F) -> Vec<B> where F: FnMut(&A) -> Vec<B> {
13 let mut r = Vec::new();
21 trait option_monad<A> {
22 fn bind<B, F>(&self, f: F) -> Option<B> where F: FnOnce(&A) -> Option<B>;
25 impl<A> option_monad<A> for Option<A> {
26 fn bind<B, F>(&self, f: F) -> Option<B> where F: FnOnce(&A) -> Option<B> {
28 Some(ref a) => { f(a) }
34 fn transform(x: Option<isize>) -> Option<String> {
35 x.bind(|n| Some(*n + 1) ).bind(|n| Some(n.to_string()) )
39 assert_eq!(transform(Some(10)), Some("11".to_string()));
40 assert_eq!(transform(None), None);
41 assert_eq!((vec!["hi".to_string()])
42 .bind(|x| vec![x.clone(), format!("{}!", x)] )
43 .bind(|x| vec![x.clone(), format!("{}?", x)] ),