Trait fmap::Monad

source ·
pub trait Monad<'a, B>where
    Self: Pure<'a, B>,
    B: 'a,{
    // Required method
    fn bind<F>(self, f: F) -> Self::Mapped
       where F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped;
}
Expand description

A Functor that is also a monad

Examples

use fmap::Monad;

let a = vec![5, 6, 7];
let b = a.bind(|x| vec![2*x, 10*x]);
assert_eq!(b, vec![10, 50, 12, 60, 14, 70]);

let a: Box<dyn Iterator<Item = i32>> = Box::new(vec![5, 6, 7].into_iter());
let b = a.bind(|x| Box::new(vec![2*x, 10*x].into_iter()));
assert_eq!(b.collect::<Vec<_>>(), vec![10, 50, 12, 60, 14, 70]);

let nested = vec![vec![1, 3], vec![2, 9, 9]];
assert_eq!(nested.bind(|x| x), vec![1, 3, 2, 9, 9]);

Note: .bind(|x| x) is also available as NestedMonad::mjoin

Required Methods§

source

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

Call function with inner values, returning mapped version of Self

Implementations on Foreign Types§

source§

impl<'a, A, B> Monad<'a, B> for Box<dyn Iterator<Item = A> + Send + 'a>where A: 'a, B: 'a + Send,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for Vec<A>where A: 'a, B: 'a,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for Option<A>where A: 'a, B: 'a,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for BTreeSet<A>where A: 'a + Ord, B: 'a + Ord,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for VecDeque<A>where A: 'a, B: 'a,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for Box<dyn Iterator<Item = A> + 'a>where A: 'a, B: 'a,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for Pin<Box<dyn Future<Output = A> + 'a>>where A: 'a, B: 'a,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for LinkedList<A>where A: 'a, B: 'a,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for HashSet<A>where A: 'a + Eq + Hash, B: 'a + Eq + Hash,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for BinaryHeap<A>where A: 'a + Ord, B: 'a + Ord,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B, E> Monad<'a, B> for Result<A, E>where A: 'a, B: 'a,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

source§

impl<'a, A, B> Monad<'a, B> for Pin<Box<dyn Future<Output = A> + Send + 'a>>where A: 'a + Send, B: 'a + Send,

source§

fn bind<F>(self, f: F) -> Self::Mappedwhere F: 'a + Send + FnMut(Self::Inner) -> Self::Mapped,

Implementors§