Trait naan::functor::Functor

source ·
pub trait Functor<F, A>where
    F: HKT1<T<A> = Self>,{
    // Required method
    fn fmap<AB, B>(self, f: AB) -> F::T<B>
       where AB: F1<A, Ret = B>;
}
Expand description

Functor adds a mapping operation to generic types.

In essence, map allows one to lift a function of type fn(A) -> B to some new Functor context, e.g. fn(Option<A>) -> Option<B>.

Laws

  • Invoking map with an identity function (e.g. |a| a) should do absolutely nothing.
use naan::prelude::*;

#[derive(Debug, PartialEq, Eq)]
struct Container<T>(T);
struct ContainerHKT;
impl HKT1 for ContainerHKT {
  type T<A> = Container<A>;
}

impl<A> Functor<ContainerHKT, A> for Container<A> {
  fn fmap<AB, B>(self, f: AB) -> Container<B>
    where AB: F1<A, Ret = B>
  {
    Container(f.call(self.0))
  }
}

assert_eq!(Container(0u8).fmap(|n| n + 1).fmap(|n: u8| n.to_string()),
           Container("1".to_string()))

Required Methods§

source

fn fmap<AB, B>(self, f: AB) -> F::T<B>where AB: F1<A, Ret = B>,

Use a function from A -> B to transform an F<A> to an F<B>.

Implementations on Foreign Types§

source§

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

source§

fn fmap<AB, B>(self, f: AB) -> <ResultOk<E> as HKT1>::T<B>where AB: F1<A, Ret = B>,

source§

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

source§

fn fmap<AB, B>(self, f: AB) -> Vec<B>where AB: F1<A, Ret = B>,

source§

impl<K, A> Functor<BTreeMapValues<K>, A> for BTreeMap<K, A>where K: Ord,

source§

fn fmap<AB, B>(self, f: AB) -> BTreeMap<K, B>where AB: F1<A, Ret = B>,

source§

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

source§

fn fmap<AB, B>(self, f: AB) -> <Option as HKT1>::T<B>where AB: F1<A, Ret = B>,

source§

impl<const N: usize, A> Functor<ArrayVec<N>, A> for ArrayVec<[Option<A>; N]>

source§

fn fmap<AB, B>(self, f: AB) -> ArrayVec<[Option<B>; N]>where AB: F1<A, Ret = B>,

source§

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

source§

fn fmap<AB, B>(self, f: AB) -> HashMap<K, B>where AB: F1<A, Ret = B>,

Implementors§

source§

impl<A> Functor<Id, A> for Id<A>

source§

impl<M, T, A> Functor<JoinHKT<M>, A> for Join<M, T, A>where JoinHKT<M>: HKT1<T<A> = Self>, M: HKT2 + HKT2<T<A, A> = T>, T: Bifunctor<M, A, A>,