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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use Box;
/// An `IterableObj` is any type which can return a new boxed iterator that yields
/// elements of the associated type [`Item`] every time [`boxed_iter`] method is called.
///
/// It is the object safe counterpart of [`Iterable`] trait which can conveniently be made into a trait object.
///
/// Instead of `iter`, it implements `boxed_iter` which returns the same iterator in a box.
///
/// Note that for collections and cloneable iterators, `IterableObj` is implicitly implemented and readily available.
/// Please refer to [`Iterable`] documentation for details of automatic implementations.
///
/// In order to use object safe iterables and collections please add `--features std` and use
/// `use orx_iterable::{*, obj_safe::*}` to import dependencies rather than `use orx_iterable::*`.
///
/// [`Item`]: crate::obj_safe::IterableObj::Item
/// [`boxed_iter`]: crate::obj_safe::IterableObj::boxed_iter
/// [`Iterable`]: crate::Iterable
///
/// # Examples
///
/// ```
/// use orx_iterable::{*, obj_safe::*};
/// use arrayvec::ArrayVec;
/// use smallvec::{smallvec, SmallVec};
/// use std::collections::{BTreeSet, BinaryHeap, HashSet, LinkedList, VecDeque};
///
/// struct Stats {
/// count: usize,
/// mean: i64,
/// std_dev: i64,
/// }
///
/// /// we need multiple iterations over numbers to compute the stats
/// fn statistics<'a>(numbers: Box<dyn IterableObj<Item = i64> + 'a>) -> Stats {
/// let count = numbers.boxed_iter().count() as i64;
/// let sum = numbers.boxed_iter().sum::<i64>();
/// let mean = sum / count;
/// let sum_sq_errors: i64 = numbers.boxed_iter().map(|x| (x - mean) * (x - mean)).sum();
/// let std_dev = f64::sqrt(sum_sq_errors as f64 / (count - 1) as f64) as i64;
/// Stats {
/// count: count as usize,
/// mean,
/// std_dev,
/// }
/// }
///
/// // collections as IterableObj
///
/// let x = [3, 5, 7];
/// statistics(Box::new(x.copied()));
/// // see IterableObj's transformation methods such as copied, mapped, etc.
///
/// let x = vec![3, 5, 7];
/// statistics(Box::new(x.copied()));
///
/// let x = LinkedList::from_iter([3, 5, 7]);
/// statistics(Box::new(x.copied()));
///
/// let x = VecDeque::from_iter([3, 5, 7]);
/// statistics(Box::new(x.copied()));
///
/// let x = HashSet::<_>::from_iter([3, 5, 7]);
/// statistics(Box::new(x.copied()));
///
/// let x = BTreeSet::from_iter([3, 5, 7]);
/// statistics(Box::new(x.copied()));
///
/// let x = BinaryHeap::from_iter([3, 5, 7]);
/// statistics(Box::new(x.copied()));
///
/// let x: SmallVec<[_; 128]> = smallvec![3, 5, 7];
/// statistics(Box::new(x.copied()));
///
/// let mut x = ArrayVec::<_, 16>::new();
/// x.extend([3, 5, 7]);
/// statistics(Box::new(x.copied()));
///
/// // cloneable iterators as IterableObj
///
/// let x = Box::new((0..10).map(|x| x * 2).into_iterable());
/// statistics(x);
///
/// let x = vec![1, 2, 3];
/// let y = Box::new(x
/// .iter()
/// .copied()
/// .filter(|x| x % 2 == 1)
/// .flat_map(|x| [-x, x])
/// .into_iterable());
/// statistics(y);
///
/// // lazy generators as IterableObj
///
/// statistics(Box::new(7..21i64));
/// ```
///
/// The following example represents an explicit implementation of the Iterable
/// trait for a lazy generator, which generates a sequence of Fibonacci numbers
/// up to a set bound.
///
/// ```
/// use orx_iterable::*;
/// use orx_iterable::obj_safe::*;
///
/// struct FibUntilIter {
/// curr: u32,
/// next: u32,
/// until: u32,
/// }
///
/// impl Iterator for FibUntilIter {
/// type Item = u32;
///
/// fn next(&mut self) -> Option<Self::Item> {
/// let current = self.curr;
/// self.curr = self.next;
/// self.next = current + self.next;
/// match current > self.until {
/// false => Some(current),
/// true => None,
/// }
/// }
/// }
///
/// struct FibUntil(u32);
///
/// impl IterableObj for FibUntil {
/// type Item = u32;
///
/// fn boxed_iter(&self) -> Box<dyn Iterator<Item = Self::Item> + '_> {
/// Box::new(FibUntilIter { curr: 0, next: 1, until: self.0 })
/// }
/// }
///
/// let fib = FibUntil(10); // IterableObj
///
/// assert_eq!(fib.boxed_iter().count(), 7);
/// assert_eq!(fib.boxed_iter().max(), Some(8));
/// assert_eq!(fib.boxed_iter().collect::<Vec<_>>(), [0, 1, 1, 2, 3, 5, 8]);
/// ```
// impl