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§
sourcefn flat_map<F>(self, f: F) -> Self::Target<B>where
F: FnMut(Self::Param) -> Self::Target<B>,
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§
sourcefn flatten(self) -> Self::Target<B>where
Self: FlatMap<B, Param = <Self as Higher>::Target<B>> + Sized,
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);
sourcefn 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 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);
sourcefn 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 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);
sourcefn 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>,
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);