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
use super::*;

pub struct FilterScanWrap<S: FilterScanFn>(S);

pub type FilterScanState<S> = OptionList<<S as FilterScanFn>::OutputItem, FilterScanWrap<S>>;

pub trait FilterScanFn: Sized {
    type InputItem;
    type InputResult;
    type OutputItem;
    type OutputResult;
    fn map_input(self, input: Self::InputItem) -> FilterScanState<Self>;
    fn map_result(self, result: Self::InputResult) -> Self::OutputResult;
    fn some(self, first: Self::OutputItem) -> FilterScanState<Self> {
        OptionList::Some {
            first,
            end: FilterScanWrap(self),
        }
    }
    fn end(self) -> FilterScanState<Self> {
        OptionList::End(FilterScanWrap(self))
    }
}

impl<S: FilterScanFn> FlatScanFn for FilterScanWrap<S> {
    type InputItem = S::InputItem;
    type InputResult = S::InputResult;
    type OutputList = OptionList<S::OutputItem, Self>;
    type EndList = OptionList<S::OutputItem, Id<S::OutputResult>>;
    fn map_item(self, input: Self::InputItem) -> Self::OutputList {
        self.0.map_input(input)
    }
    fn map_result(self, result: Self::InputResult) -> Self::EndList {
        OptionList::End(Id::new(self.0.map_result(result)))
    }
}

pub trait FilterScan
where
    Self: ListFn,
    Self::End: ResultFn,
{
    fn filter_scan<
        S: FilterScanFn<InputItem = Self::Item, InputResult = <Self::End as ResultFn>::Result>,
    >(
        self,
        scan: S,
    ) -> FlatScanState<Self, FilterScanWrap<S>> {
        self.flat_scan(FilterScanWrap(scan))
    }
}

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