Monad

Trait Monad 

Source
pub trait Monad<B>: Applicative<B> {
    // Required method
    fn bind<F>(&self, f: F) -> Self::M
       where F: Fn(&Self::A) -> Self::M;
}
Expand description

Monad type class

Required Methods§

Source

fn bind<F>(&self, f: F) -> Self::M
where F: Fn(&Self::A) -> Self::M,

Bind works like map but it flattens nested structures

§Examples
use funlib::Applicative;
use funlib::Monad;
fn over5(i: &i32) -> Option<i32> { if *i > 5 { Some(*i) } else { None }}
let a = Some(4).bind(over5);
let b = Some(6).bind(over5);
assert_eq!(None, a);
assert_eq!(Some(6), b);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<A, B> Monad<B> for Option<A>

Source§

fn bind<F>(&self, f: F) -> Option<B>
where F: FnMut(&A) -> Option<B>,

Source§

impl<A, B> Monad<B> for Box<A>

Source§

fn bind<F>(&self, f: F) -> Box<B>
where F: FnMut(&A) -> Box<B>,

Source§

impl<A, B> Monad<B> for Rc<A>

Source§

fn bind<F>(&self, f: F) -> Rc<B>
where F: FnMut(&A) -> Rc<B>,

Source§

impl<A, B> Monad<B> for Vec<A>

Source§

fn bind<F>(&self, f: F) -> Vec<B>
where F: FnMut(&A) -> Vec<B>,

Implementors§