1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// Functor
pub trait Functor: Sized {
    /// Alias for the type that we are generic over.
    type Inner;
    /// The type we are generic over and it's container.
    type Wrapped<T>: Functor;

    /// Lifts a function into the Functor context.
    ///
    /// In Haskell speak this translates to,
    /// fmap :: a -> b -> f a -> f b
    fn rmap<B, F>(self, f: F) -> Self::Wrapped<B>
    where
        F: FnOnce(Self::Inner) -> B;
}

impl<A> Functor for Option<A> {
    type Inner = A;
    type Wrapped<T> = Option<T>;

    fn rmap<B, F: FnOnce(Self::Inner) -> B>(self, f: F) -> Self::Wrapped<B> {
        match self {
            None => None,
            Some(a) => Some(f(a)),
        }
    }
}

impl<T, E> Functor for Result<T, E> {
    type Inner = T;
    type Wrapped<U> = Result<U, E>;

    fn rmap<B, F: FnOnce(Self::Inner) -> B>(self, f: F) -> Self::Wrapped<B> {
        match self {
            Err(e) => Err(e),
            Ok(a) => Ok(f(a)),
        }
    }
}

pub fn fmap<F: Functor, B>(f: impl FnOnce(F::Inner) -> B, fa: F) -> F::Wrapped<B>
where
{
    fa.rmap(f)
}

#[cfg(test)]
mod test {
    use super::fmap;
    use crate::id;

    #[test]
    fn functor_option_identity() {
        let i: Option<i32> = Some(2);
        let j: Option<i32> = Some(2);
        assert_eq!(fmap(id, i), j);
        assert_eq!(fmap(id, Some(2)), Some(2));
    }

    #[test]
    fn functor_option_none() {
        assert_eq!(fmap::<Option<i32>, _>(|x| x * 2, None), None);
    }

    #[test]
    fn functor_option_some() {
        assert_eq!(fmap(|val| val * 2, Some(2)), Some(4));
    }
}