garfield/
functor.rs

1/// Functor
2pub trait Functor: Sized {
3    /// Alias for the type that we are generic over.
4    type Inner;
5    /// The type we are generic over and it's container.
6    type Wrapped<T>: Functor;
7
8    /// Lifts a function into the Functor context.
9    ///
10    /// In Haskell speak this translates to,
11    /// fmap :: a -> b -> f a -> f b
12    fn rmap<B, F>(self, f: F) -> Self::Wrapped<B>
13    where
14        F: FnOnce(Self::Inner) -> B;
15}
16
17impl<A> Functor for Option<A> {
18    type Inner = A;
19    type Wrapped<T> = Option<T>;
20
21    fn rmap<B, F: FnOnce(Self::Inner) -> B>(self, f: F) -> Self::Wrapped<B> {
22        match self {
23            None => None,
24            Some(a) => Some(f(a)),
25        }
26    }
27}
28
29impl<T, E> Functor for Result<T, E> {
30    type Inner = T;
31    type Wrapped<U> = Result<U, E>;
32
33    fn rmap<B, F: FnOnce(Self::Inner) -> B>(self, f: F) -> Self::Wrapped<B> {
34        match self {
35            Err(e) => Err(e),
36            Ok(a) => Ok(f(a)),
37        }
38    }
39}
40
41pub fn fmap<F: Functor, B>(f: impl FnOnce(F::Inner) -> B, fa: F) -> F::Wrapped<B>
42where
43{
44    fa.rmap(f)
45}
46
47#[cfg(test)]
48mod test {
49    use super::fmap;
50    use crate::id;
51
52    #[test]
53    fn functor_option_identity() {
54        let i: Option<i32> = Some(2);
55        let j: Option<i32> = Some(2);
56        assert_eq!(fmap(id, i), j);
57        assert_eq!(fmap(id, Some(2)), Some(2));
58    }
59
60    #[test]
61    fn functor_option_none() {
62        assert_eq!(fmap::<Option<i32>, _>(|x| x * 2, None), None);
63    }
64
65    #[test]
66    fn functor_option_some() {
67        assert_eq!(fmap(|val| val * 2, Some(2)), Some(4));
68    }
69}