Trait naan::bifunctor::Bifunctor

source ·
pub trait Bifunctor<F, A, B>where
    F: HKT2<T<A, B> = Self>,{
    // Required method
    fn bimap<A2, B2, FA, FB>(self, fa: FA, fb: FB) -> F::T<A2, B2>
       where FA: F1<A, Ret = A2>,
             FB: F1<B, Ret = B2>;

    // Provided methods
    fn lmap<A2, FA>(self, fa: FA) -> F::T<A2, B>
       where Self: Sized,
             FA: F1<A, Ret = A2> { ... }
    fn rmap<B2, FB>(self, fb: FB) -> F::T<A, B2>
       where Self: Sized,
             FB: F1<B, Ret = B2> { ... }
    fn join(self) -> Join<F, Self, A>
       where Self: Sized,
             F: HKT2<T<A, A> = Self> { ... }
}
Expand description

A Bifunctor provides a map function for types with 2 parameters, allowing you to act on both types at once.

Additionally, Bifunctor provides lmap and rmap which allow you to unambiguously map only one of the types.

use std::io;
use std::path::PathBuf;

use naan::prelude::*;

fn get_some_meaningful_filepath() -> io::Result<String> {
}

// in one shot we turn `Result<String, io::Error>` into `Result<PathBuf, String>`.
get_some_meaningful_filepath().bimap(|ok| PathBuf::from(ok), |err| format!("{err:?}"));

Required Methods§

source

fn bimap<A2, B2, FA, FB>(self, fa: FA, fb: FB) -> F::T<A2, B2>where FA: F1<A, Ret = A2>, FB: F1<B, Ret = B2>,

Provided Methods§

source

fn lmap<A2, FA>(self, fa: FA) -> F::T<A2, B>where Self: Sized, FA: F1<A, Ret = A2>,

Map the left type in the Bifunctor

In Result, this maps the “Ok” type and is equivalent to map.

source

fn rmap<B2, FB>(self, fb: FB) -> F::T<A, B2>where Self: Sized, FB: F1<B, Ret = B2>,

Map the right type in the Bifunctor

In Result, this maps the “Error” type and is equivalent to map_err.

source

fn join(self) -> Join<F, Self, A>where Self: Sized, F: HKT2<T<A, A> = Self>,

Wrap this type in Join

Implementations on Foreign Types§

source§

impl<A, E> Bifunctor<Result, A, E> for Result<A, E>

source§

fn bimap<AB, BB, FA, FB>(self, fa: FA, fb: FB) -> <Result as HKT2>::T<AB, BB>where FA: F1<A, Ret = AB>, FB: F1<E, Ret = BB>,

Implementors§