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
use core::marker::PhantomData;

use super::*;

struct CollectState<C, E>(C, PhantomData<E>);

impl<C, E> CollectState<C, E> {
    const fn new(c: C) -> Self {
        CollectState(c, PhantomData {})
    }
}

pub struct CollectResult<C, R> {
    pub collection: C,
    pub result: R,
}

impl<C: Collection, E> ScanFn for CollectState<C, E> {
    type InputItem = C::Item;
    type InputResult = E;
    type OutputItem = ();
    type OutputResult = CollectResult<C, Self::InputResult>;

    fn map_input(self, input: Self::InputItem) -> ScanState<Self> {
        ScanState {
            first: (),
            next: CollectState::new(self.0.add(input)),
        }
    }

    fn map_result(self, result: Self::InputResult) -> Self::OutputResult {
        CollectResult {
            collection: self.0,
            result,
        }
    }
}

pub trait Collect
where
    Self: ListFn,
    Self::End: ResultFn,
{
    fn collect<C: Collection<Item = Self::Item>>(
        self,
        c: C,
    ) -> CollectResult<C, <Self::End as ResultFn>::Result> {
        self.scan(CollectState::new(c)).fold().result()
    }
}

impl<L> Collect for L
where
    Self: ListFn,
    Self::End: ResultFn,
{
}