Trait rust2fun::flatmap::FlatMap

source ·
pub trait FlatMap<B>: Higher {
    // Required method
    fn flat_map<F>(self, f: F) -> Self::Target<B>
       where F: FnMut(Self::Param) -> Self::Target<B>;

    // Provided methods
    fn flatten(self) -> Self::Target<B>
       where Self: FlatMap<B, Param = <Self as Higher>::Target<B>> + Sized { ... }
    fn m_product<F>(self, f: F) -> Self::Target<(Self::Param, B)>
       where F: FnMut(Self::Param) -> Self::Target<B>,
             Self: FlatMap<(<Self as Higher>::Param, B)> + Sized,
             Self::Param: Copy,
             Self::Target<B>: Functor<(Self::Param, B), Target<(Self::Param, B)> = Self::Target<(Self::Param, B)>> { ... }
    fn if_m<T, F>(self, if_true: T, if_false: F) -> Self::Target<B>
       where T: FnMut() -> Self::Target<B>,
             F: FnMut() -> Self::Target<B>,
             Self: FlatMap<B, Param = bool> + Sized { ... }
    fn flat_tap<F>(self, f: F) -> Self
       where F: FnMut(Self::Param) -> Self::Target<B>,
             Self: FlatMap<<Self as Higher>::Param, Target<<Self as Higher>::Param> = Self> + Sized,
             Self::Param: Copy,
             Self::Target<B>: Functor<Self::Param, Target<Self::Param> = Self> { ... }
}
Expand description

Gives access to the flat_map method. The motivation for separating this out of Monad is that there are situations where flat_map can be implemented but not pure.

Required Methods§

source

fn flat_map<F>(self, f: F) -> Self::Target<B>where F: FnMut(Self::Param) -> Self::Target<B>,

Maps a function over a value in the context and flattens the resulting nested context. This is the same as self.map(f).flatten(). This is also known as bind or >>= in other languages.

Examples
use rust2fun::prelude::*;

let x = Some(1);
let actual = x.flat_map(|x| Some(x.to_string()));
assert_eq!(Some("1".to_string()), actual);

Provided Methods§

source

fn flatten(self) -> Self::Target<B>where Self: FlatMap<B, Param = <Self as Higher>::Target<B>> + Sized,

Flattens a nested structure. This is a convenience method for flat_map(id).

Examples
use rust2fun::prelude::*;

let actual = Some(Some(1)).flatten();
assert_eq!(Some(1), actual);
source

fn m_product<F>(self, f: F) -> Self::Target<(Self::Param, B)>where F: FnMut(Self::Param) -> Self::Target<B>, Self: FlatMap<(<Self as Higher>::Param, B)> + Sized, Self::Param: Copy, Self::Target<B>: Functor<(Self::Param, B), Target<(Self::Param, B)> = Self::Target<(Self::Param, B)>>,

Pair up the value with the result of applying the function to the value.

Examples
use rust2fun::prelude::*;

let x = Some(1);
let actual = x.m_product(|x| Some(x.to_string()));
assert_eq!(Some((1, "1".to_string())), actual);
source

fn if_m<T, F>(self, if_true: T, if_false: F) -> Self::Target<B>where T: FnMut() -> Self::Target<B>, F: FnMut() -> Self::Target<B>, Self: FlatMap<B, Param = bool> + Sized,

if lifted into monad.

Examples
use rust2fun::prelude::*;

let x = Some(true);
let actual = x.if_m(constant!(Some(1)), constant!(Some(0)));
assert_eq!(Some(1), actual);
source

fn flat_tap<F>(self, f: F) -> Selfwhere F: FnMut(Self::Param) -> Self::Target<B>, Self: FlatMap<<Self as Higher>::Param, Target<<Self as Higher>::Param> = Self> + Sized, Self::Param: Copy, Self::Target<B>: Functor<Self::Param, Target<Self::Param> = Self>,

Apply a monadic function and discard the result while keeping the effect.

Examples
use rust2fun::prelude::*;

let x = Some(1);
let actual = x.flat_tap(|x| Some(x.to_string()));
assert_eq!(Some(1), actual);

Implementations on Foreign Types§

source§

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

source§

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

source§

impl<A, B> FlatMap<B> for PhantomData<A>

source§

fn flat_map<F>(self, _f: F) -> PhantomData<B>where F: FnMut(A) -> PhantomData<B>,

source§

impl<A, B: Ord> FlatMap<B> for BTreeSet<A>

source§

fn flat_map<F>(self, f: F) -> Self::Target<B>where F: FnMut(A) -> Self::Target<B>,

source§

impl<A, B> FlatMap<B> for LinkedList<A>

source§

fn flat_map<F>(self, f: F) -> Self::Target<B>where F: FnMut(A) -> Self::Target<B>,

source§

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

source§

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

source§

impl<A, B: Ord> FlatMap<B> for BinaryHeap<A>

source§

fn flat_map<F>(self, f: F) -> Self::Target<B>where F: FnMut(A) -> Self::Target<B>,

source§

impl<A, B, E> FlatMap<B> for Result<A, E>

source§

fn flat_map<F>(self, f: F) -> Result<B, E>where F: FnMut(A) -> Result<B, E>,

source§

impl<A, B: Eq + Hash> FlatMap<B> for HashSet<A>

source§

fn flat_map<F>(self, f: F) -> Self::Target<B>where F: FnMut(A) -> Self::Target<B>,

source§

impl<A, B, K: Eq + Hash> FlatMap<B> for HashMap<K, A>

source§

fn flat_map<F>(self, f: F) -> HashMap<K, B>where F: FnMut(A) -> HashMap<K, B>,

source§

impl<A, B> FlatMap<B> for VecDeque<A>

source§

fn flat_map<F>(self, f: F) -> Self::Target<B>where F: FnMut(A) -> Self::Target<B>,

source§

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

source§

fn flat_map<F>(self, f: F) -> Self::Target<B>where F: FnMut(A) -> Self::Target<B>,

Implementors§

source§

impl<A, B> FlatMap<B> for NEVec<A>