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
use std::collections::HashMap;
use std::hash::BuildHasher;

use crate::{Functor, Inner};

impl<I: Inner> Functor<I> for I {
    type Output<O: Inner> = O;

    fn pure(i: I) -> I {
        i
    }

    fn fmap<O: Inner, F: Fn(I) -> O>(self, func: F) -> Self::Output<O> {
        func(self)
    }
}

impl<I: Inner> Functor<I> for Vec<I> {
    type Output<O: Inner> = Vec<O>;

    fn pure(i: I) -> Self {
        vec![i]
    }

    fn fmap<O: Inner, F: Fn(I) -> O>(self, func: F) -> Self::Output<O> {
        self.into_iter().map(func).collect()
    }
}

impl<I: Inner, S: BuildHasher + Default> Functor<I> for HashMap<I, usize, S> {
    type Output<O: Inner> = HashMap<O, usize, S>;

    fn pure(i: I) -> Self {
        let mut hm = Self::default();
        hm.insert(i, 1);
        hm
    }

    fn fmap<O: Inner, F: Fn(I) -> O>(self, func: F) -> Self::Output<O> {
        // Constructing a new HashMap is necessary, as there may be fewer new
        // keys than old keys, which requires merging some or all counts.
        let mut out = Self::Output::<O>::with_capacity_and_hasher(self.len(), Default::default());
        self.into_iter()
            .map(|(i, count)| (func(i), count))
            .for_each(|(o, count)| {
                *out.entry(o).or_insert(0) += count;
            });
        out
    }
}