Trait rust2fun::functor::Functor

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

    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<()> = 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§

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§

Alias for [map] if the implementing type already had a built-in .map method.

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);

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);

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);

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

Examples
use rust2fun::prelude::*;

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

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"));

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"));

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));

if lifted into Functor.

Examples
use rust2fun::constant;
use rust2fun::prelude::*;

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

Implementations on Foreign Types§

Implementors§