pub trait Functor<'a, A: 'a> {
type Target<T: 'a>: Functor<'a, T, Target<A> = Self>;
fn fmap<B: 'a, F>(self, f: F) -> Self::Target<B>
where
F: Fn(A) -> B + 'a;
fn fconst<B>(self, b: B) -> Self::Target<B>
where
Self: Sized,
B: Clone,
{ ... }
fn void(self) -> Self::Target<()>
where
Self: Sized,
{ ... }
fn f_into_iter(self) -> Box<dyn Iterator<Item = A>>
where
Self: Sized,
A: 'static,
{ ... }
}
Expand description
A Functor
lets you change the type parameter of a generic type.
A Functor
defines a method fmap
on a type F<_>: Functor
which converts
an F<A>
to F<B>
using a function Fn(A) -> B
applied to the A
s inside
it.
You can also use this just to modify the values inside your container value
without changing their type, if the mapping function returns a value of the
same type. This is called an “endofunctor.” In an ideal Rust, we would be
able to implement this as a special case of fmap
modifying the data in place, but in the Rust we have, beware that using
fmap
in this manner is considerably less efficient than
using a mutable reference iterator.
Required Associated Types§
Required Methods§
Provided Methods§
sourcefn fconst<B>(self, b: B) -> Self::Target<B>where
Self: Sized,
B: Clone,
fn fconst<B>(self, b: B) -> Self::Target<B>where
Self: Sized,
B: Clone,
Map the functor to the provided constant value.
sourcefn f_into_iter(self) -> Box<dyn Iterator<Item = A>>where
Self: Sized,
A: 'static,
fn f_into_iter(self) -> Box<dyn Iterator<Item = A>>where
Self: Sized,
A: 'static,
Turn the functor into an iterator.
let my_functor = vec![1, 2, 3];
let iter = my_functor.f_into_iter();
let my_vec: Vec<i32> = iter.collect();
assert_eq!(my_vec, vec![1, 2, 3]);