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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! The higher-kinded trait of a `Functor`.

use super::*;
use super::types::*;

pub trait Functor {
    fn fmap<A, B, F: Fn(A) -> B>(f: F, a: K1<Self, A>) -> K1<Self, B>;
}

impl Functor for OptionC {
    fn fmap<A, B, F: Fn(A) -> B>(f: F, a: K1<Self, A>) -> K1<Self, B> {
        K1::new(a.into_inner().map(f))
    }
}

impl Functor for VecC {
    fn fmap<A, B, F: Fn(A) -> B>(f: F, a: K1<Self, A>) -> K1<Self, B> {
        K1::new(a.into_inner().into_iter().map(f).collect())
    }
}

impl<T> Functor for ResultLeft<T> {
    fn fmap<A, B, F: Fn(A) -> B>(f: F, a: K1<Self, A>) -> K1<Self, B> {
        Self::new(a.into_inner().map_err(f))
    }
}

impl<E> Functor for ResultRight<E> {
    fn fmap<A, B, F: Fn(A) -> B>(f: F, a: K1<Self, A>) -> K1<Self, B> {
        Self::new(a.into_inner().map(f))
    }
}

impl<T> Functor for PairLeft<T> {
    fn fmap<A, B, F: Fn(A) -> B>(f: F, a: K1<Self, A>) -> K1<Self, B> {
        let a = a.into_inner();
        Self::new((a.0, f(a.1)))
    }
}

impl<T> Functor for PairRight<T> {
    fn fmap<A, B, F: Fn(A) -> B>(f: F, a: K1<Self, A>) -> K1<Self, B> {
        let a = a.into_inner();
        Self::new((f(a.0), a.1))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn check_law_id<T, F: Functor + Kind1<T>>(input: K1<F, T>)
    where
        K1Type<F, T>: PartialEq + core::fmt::Debug + Clone,
    {
        let output = F::fmap(|x| x, input.clone());
        assert_eq!(input, output);
    }

    fn check_law_compose<T, F: Functor + Kind1<T>, G, H>(g: G, h: H, input: K1<F, T>)
    where
        G: Fn(T) -> T,
        H: Fn(T) -> T,
        K1Type<F, T>: PartialEq + core::fmt::Debug + Clone,
    {
        let composed_first = F::fmap(|x| g(h(x)), input.clone());
        let fmapped_first = F::fmap(g, F::fmap(h, input));
        assert_eq!(composed_first, fmapped_first);
    }

    #[test]
    fn option_functor_law_id() {
        let nothing: Option<i32> = None;
        check_law_id(OptionC::new(nothing));
        let something: Option<i32> = Some(42);
        check_law_id(OptionC::new(something));
    }

    #[test]
    fn option_functor_law_compose() {
        let g = |x| x + 1;
        let h = |x| x * 2;
        check_law_compose::<i32, _, _, _>(&g, &h, OptionC::new(None));
        check_law_compose(&g, &h, OptionC::new(Some(42)));
    }

    #[test]
    fn vec_functor_law_id() {
        let nothing: Vec<i32> = vec![];
        check_law_id(VecC::new(nothing));
        let something: Vec<i32> = vec![1, 2, 3];
        check_law_id(VecC::new(something));
    }

    #[test]
    fn vec_functor_law_compose() {
        let g = |x| x + 1;
        let h = |x| x * 2;
        check_law_compose::<i32, _, _, _>(&g, &h, VecC::new(vec![]));
        check_law_compose(&g, &h, VecC::new(vec![1, 2, 3]));
    }

    #[test]
    fn result_left_functor_law_id() {
        let great: Result<i32, String> = Ok(42);
        check_law_id(ResultLeft::new(great));
        let not_great: Result<String, i32> = Err(42);
        check_law_id(ResultLeft::new(not_great));
    }

    #[test]
    fn result_left_functor_law_compose() {
        let g = |x| x + 1;
        let h = |x| x * 2;
        check_law_compose::<i32, _, _, _>(&g, &h, ResultLeft::<i32>::new(Err(42)));
        check_law_compose(&g, &h, ResultLeft::new(Ok(42)));
    }

    #[test]
    fn result_right_functor_law_id() {
        let great: Result<i32, String> = Ok(42);
        check_law_id(ResultRight::new(great));
        let not_great: Result<String, i32> = Err(42);
        check_law_id(ResultRight::new(not_great));
    }

    #[test]
    fn result_right_functor_law_compose() {
        let g = |x| x + 1;
        let h = |x| x * 2;
        check_law_compose::<i32, _, _, _>(&g, &h, ResultRight::new(Err(42)));
        check_law_compose(&g, &h, ResultRight::<i32>::new(Ok(42)));
    }

    #[test]
    fn pair_left_functor_law_id() {
        check_law_id(PairLeft::new(("foobar", 42)));
    }

    #[test]
    fn pair_left_functor_law_compose() {
        let g = |x| x + 1;
        let h = |x| x * 2;
        check_law_compose(&g, &h, PairLeft::new(("foobar", 42)));
    }

    #[test]
    fn pair_right_functor_law_id() {
        check_law_id(PairRight::new((42, "foobar")));
    }

    #[test]
    fn pair_right_functor_law_compose() {
        let g = |x| x + 1;
        let h = |x| x * 2;
        check_law_compose(&g, &h, PairRight::new((42, "foobar")));
    }
}