kind_tree/
telescope.rs

1/// A sequence of arguments that depends on the previous sequence
2/// it's similar to a iterated sigma type.
3#[derive(Debug, Clone, Hash, PartialEq, Eq)]
4pub struct Telescope<T>(Vec<T>);
5
6impl<T> Default for Telescope<T> {
7    fn default() -> Self {
8        Self(Default::default())
9    }
10}
11
12impl<T> Telescope<T> {
13    pub fn new(vec: Vec<T>) -> Telescope<T> {
14        Telescope(vec)
15    }
16
17    pub fn len(&self) -> usize {
18        self.0.len()
19    }
20
21    pub fn push(&mut self, el: T) {
22        self.0.push(el)
23    }
24
25    pub fn as_slice(&self) -> &[T] {
26        self.0.as_slice()
27    }
28
29    pub fn to_vec(self) -> Vec<T> {
30        self.0
31    }
32
33    pub fn get_vec(&mut self) -> &mut Vec<T> {
34        &mut self.0
35    }
36
37    pub fn extend(&self, other: &Telescope<T>) -> Telescope<T>
38    where
39        T: Clone,
40    {
41        Telescope([self.as_slice(), other.as_slice()].concat())
42    }
43
44    pub fn map<B, F>(&self, f: F) -> Telescope<B>
45    where
46        F: FnMut(&T) -> B,
47    {
48        Telescope(self.0.iter().map(f).collect())
49    }
50
51    pub fn is_empty(&self) -> bool {
52        self.0.is_empty()
53    }
54
55    pub fn iter(&self) -> std::slice::Iter<T> {
56        self.0.iter()
57    }
58
59    pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
60        self.0.iter_mut()
61    }
62}
63
64impl<T> Telescope<T>
65where
66    T: Clone,
67{
68    pub fn drop(self, num: usize) -> Telescope<T> {
69        Telescope(self.0[num..].to_vec())
70    }
71}
72
73impl<A> IntoIterator for Telescope<A> {
74    type Item = A;
75
76    type IntoIter = std::vec::IntoIter<A>;
77
78    fn into_iter(self) -> Self::IntoIter {
79        self.0.into_iter()
80    }
81}
82
83impl<A> std::ops::Index<usize> for Telescope<A> {
84    type Output = A;
85
86    fn index(&self, index: usize) -> &Self::Output {
87        &self.0[index]
88    }
89}