This crate provides functors in Rust, including
implementations for all collections in
std::collections
.
Functor
impl<'a, A, B> Functor<'a, B> for Option<A>
where
A: 'a,
B: 'a,
{
type Inner = A;
type Mapped<'b> = Option<B>
where
'a: 'b;
fn fmap<'b, F>(self, f: F) -> Self::Mapped<'b, B>
where
'a: 'b,
F: 'b + Fn(A) -> B,
{
self.map(f)
}
}
Functor::fmap
``` use fmap::Functor;
let ok: Result
let err: Result
Note that the compiler requires a FunctorSelf
bound to infer that
mapping an inner type to itself doesn't change the wrapped type:
fn double_inner_i32<'a, T>(x: T) -> T
where
//T: Functor<'a, i32, Inner = i32>, // doesn't work
T: FunctorSelf<'a, i32>, // use this instead
{
x.fmap(|x| 2 * x)
}