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

pub struct Take<L: ListFn> {
    input: L,
    count: usize,
}

impl<L: ListFn> ListFn for Take<L> {
    type Item = L::Item;
    type End = ();
    fn list(self) -> List<Self> {
        if self.count == 0 {
            List::End(())
        } else {
            match self.input.list() {
                List::Some(first, input) => List::Some(
                    first,
                    Take {
                        input,
                        count: self.count - 1,
                    },
                ),
                List::End(_) => List::End(()),
            }
        }
    }
}

pub trait TakeEx: ListFn {
    fn take(self, count: usize) -> Take<Self> {
        Take { input: self, count }
    }
}

impl<L: ListFn> TakeEx for L {}