Trait rust2fun::functor::Functor

source ·
pub trait Functor<B>: Invariant<B> {
    // Required method
    fn map(self, f: impl FnMut(Self::Param) -> B) -> Self::Target<B>;

    // Provided methods
    fn fmap<F>(self, f: F) -> Self::Target<B>
       where F: FnMut(Self::Param) -> B,
             Self: Sized { ... }
    fn fproduct<F>(self, f: F) -> Self::Target<(Self::Param, B)>
       where F: FnMut(&Self::Param) -> B,
             Self: Functor<(<Self as Higher>::Param, B)> + Sized { ... }
    fn fproduct_left<F>(self, f: F) -> Self::Target<(B, Self::Param)>
       where F: FnMut(&Self::Param) -> B,
             Self: Functor<(B, <Self as Higher>::Param)> + Sized { ... }
    fn map_const(self, b: B) -> Self::Target<B>
       where B: Copy,
             Self: Functor<B> + Sized { ... }
    fn void(self) -> Self::Target<()>
       where Self: Functor<(), Target<()> = <Self as Higher>::Target<B>> + Sized { ... }
    fn tuple_left(self, b: B) -> Self::Target<(B, Self::Param)>
       where B: Copy,
             Self: Functor<(B, <Self as Higher>::Param)> + Sized { ... }
    fn tuple_right(self, b: B) -> Self::Target<(Self::Param, B)>
       where B: Copy,
             Self: Functor<(<Self as Higher>::Param, B)> + Sized { ... }
    fn unzip<A>(self) -> (Self::Target<A>, Self::Target<B>)
       where Self: Higher<Param = (A, B)> + Functor<A> + Functor<B> + Copy + Sized { ... }
    fn if_f<T, F>(self, if_true: T, if_false: F) -> Self::Target<B>
       where T: FnMut() -> B,
             F: FnMut() -> B,
             Self: Functor<B, Param = bool> + Sized { ... }
}
Expand description

Covariant functor. See the module level documentation for more.

Required Methods§

source

fn map(self, f: impl FnMut(Self::Param) -> B) -> Self::Target<B>

Transform a Self<A> into a Self<B> by providing a transformation from A to B.

Examples
use rust2fun::prelude::*;

let x = Some("1".to_string());
let actual = x.map(|s| s.parse::<i32>().unwrap());
assert_eq!(Some(1), actual);

Provided Methods§

source

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

Alias for Functor::map if the implementing type already had a built-in .map method.

source

fn fproduct<F>(self, f: F) -> Self::Target<(Self::Param, B)>where F: FnMut(&Self::Param) -> B, Self: Functor<(<Self as Higher>::Param, B)> + Sized,

Tuple the values in Self<A> with the result of applying a function with the value.

Examples
use rust2fun::prelude::*;

let x = Some(1);
let actual = x.fproduct(|x: &i32| x.to_string());
assert_eq!(Some((1, "1".to_string())), actual);
source

fn fproduct_left<F>(self, f: F) -> Self::Target<(B, Self::Param)>where F: FnMut(&Self::Param) -> B, Self: Functor<(B, <Self as Higher>::Param)> + Sized,

Pair the result of function application with the values in Self<A>.

Examples
use rust2fun::prelude::*;

let x = Some(1);
let actual = x.fproduct_left(|x: &i32| x.to_string());
assert_eq!(Some(("1".to_string(), 1)), actual);
source

fn map_const(self, b: B) -> Self::Target<B>where B: Copy, Self: Functor<B> + Sized,

Replaces the A value in Self<A> with the supplied value.

Examples
use rust2fun::prelude::*;

let actual = Some(1).map_const("foo");
assert_eq!(Some("foo"), actual);
source

fn void(self) -> Self::Target<()>where Self: Functor<(), Target<()> = <Self as Higher>::Target<B>> + Sized,

Empty the Self<A> of the values, preserving the structure.

Examples
use rust2fun::prelude::*;

assert_eq!(Some(()), Some(1).void());
source

fn tuple_left(self, b: B) -> Self::Target<(B, Self::Param)>where B: Copy, Self: Functor<(B, <Self as Higher>::Param)> + Sized,

Tuples the A value in Self<A> with the supplied B value, with the B value on the left.

Examples
use rust2fun::prelude::*;

assert_eq!(Some(("foo", 1)), Some(1).tuple_left("foo"));
source

fn tuple_right(self, b: B) -> Self::Target<(Self::Param, B)>where B: Copy, Self: Functor<(<Self as Higher>::Param, B)> + Sized,

Tuples the A value in Self<A> with the supplied B value, with the B value on the right.

Examples
use rust2fun::prelude::*;

assert_eq!(Some((1, "foo")), Some(1).tuple_right("foo"));
source

fn unzip<A>(self) -> (Self::Target<A>, Self::Target<B>)where Self: Higher<Param = (A, B)> + Functor<A> + Functor<B> + Copy + Sized,

Un-zips an Self<(A, B)> consisting of element pairs into two separate Self’s tupled.

Examples
use rust2fun::prelude::*;

let x = Some((1, "foo"));
assert_eq!((Some(1), Some("foo")), Functor::unzip(x));
source

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

if lifted into Functor.

Examples
use rust2fun::prelude::*;

let x = Some(true);
assert_eq!(Some(1), x.if_f(constant!(1), constant!(0)));

Implementations on Foreign Types§

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Result<B, E>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Self::Target<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Self::Target<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Self::Target<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Self::Target<B>

source§

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

source§

fn map(self, _f: impl FnMut(A) -> B) -> PhantomData<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Box<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Self::Target<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Self::Target<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> Option<B>

source§

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

source§

fn map(self, f: impl FnMut(A) -> B) -> HashMap<K, B>

Implementors§

source§

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

source§

impl<A, B, E> Functor<B> for Validated<A, E>