1 | //! Free functions that create iterator adaptors or call iterator methods. |
2 | //! |
3 | //! The benefit of free functions is that they accept any [`IntoIterator`] as |
4 | //! argument, so the resulting code may be easier to read. |
5 | |
6 | #[cfg (feature = "use_alloc" )] |
7 | use std::fmt::Display; |
8 | use std::iter::{self, Zip}; |
9 | #[cfg (feature = "use_alloc" )] |
10 | type VecIntoIter<T> = alloc::vec::IntoIter<T>; |
11 | |
12 | #[cfg (feature = "use_alloc" )] |
13 | use alloc::string::String; |
14 | |
15 | use crate::intersperse::{Intersperse, IntersperseWith}; |
16 | use crate::Itertools; |
17 | |
18 | pub use crate::adaptors::{interleave, put_back}; |
19 | #[cfg (feature = "use_alloc" )] |
20 | pub use crate::kmerge_impl::kmerge; |
21 | pub use crate::merge_join::{merge, merge_join_by}; |
22 | #[cfg (feature = "use_alloc" )] |
23 | pub use crate::multipeek_impl::multipeek; |
24 | #[cfg (feature = "use_alloc" )] |
25 | pub use crate::peek_nth::peek_nth; |
26 | #[cfg (feature = "use_alloc" )] |
27 | pub use crate::put_back_n_impl::put_back_n; |
28 | #[cfg (feature = "use_alloc" )] |
29 | pub use crate::rciter_impl::rciter; |
30 | pub use crate::zip_eq_impl::zip_eq; |
31 | |
32 | /// Iterate `iterable` with a particular value inserted between each element. |
33 | /// |
34 | /// [`IntoIterator`] enabled version of [`Iterator::intersperse`]. |
35 | /// |
36 | /// ``` |
37 | /// use itertools::intersperse; |
38 | /// |
39 | /// itertools::assert_equal(intersperse((0..3), 8), vec![0, 8, 1, 8, 2]); |
40 | /// ``` |
41 | pub fn intersperse<I>(iterable: I, element: I::Item) -> Intersperse<I::IntoIter> |
42 | where |
43 | I: IntoIterator, |
44 | <I as IntoIterator>::Item: Clone, |
45 | { |
46 | Itertools::intersperse(self:iterable.into_iter(), element) |
47 | } |
48 | |
49 | /// Iterate `iterable` with a particular value created by a function inserted |
50 | /// between each element. |
51 | /// |
52 | /// [`IntoIterator`] enabled version of [`Iterator::intersperse_with`]. |
53 | /// |
54 | /// ``` |
55 | /// use itertools::intersperse_with; |
56 | /// |
57 | /// let mut i = 10; |
58 | /// itertools::assert_equal(intersperse_with((0..3), || { i -= 1; i }), vec![0, 9, 1, 8, 2]); |
59 | /// assert_eq!(i, 8); |
60 | /// ``` |
61 | pub fn intersperse_with<I, F>(iterable: I, element: F) -> IntersperseWith<I::IntoIter, F> |
62 | where |
63 | I: IntoIterator, |
64 | F: FnMut() -> I::Item, |
65 | { |
66 | Itertools::intersperse_with(self:iterable.into_iter(), element) |
67 | } |
68 | |
69 | /// Iterate `iterable` with a running index. |
70 | /// |
71 | /// [`IntoIterator`] enabled version of [`Iterator::enumerate`]. |
72 | /// |
73 | /// ``` |
74 | /// use itertools::enumerate; |
75 | /// |
76 | /// for (i, elt) in enumerate(&[1, 2, 3]) { |
77 | /// /* loop body */ |
78 | /// } |
79 | /// ``` |
80 | pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter> |
81 | where |
82 | I: IntoIterator, |
83 | { |
84 | iterable.into_iter().enumerate() |
85 | } |
86 | |
87 | /// Iterate `iterable` in reverse. |
88 | /// |
89 | /// [`IntoIterator`] enabled version of [`Iterator::rev`]. |
90 | /// |
91 | /// ``` |
92 | /// use itertools::rev; |
93 | /// |
94 | /// for elt in rev(&[1, 2, 3]) { |
95 | /// /* loop body */ |
96 | /// } |
97 | /// ``` |
98 | pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter> |
99 | where |
100 | I: IntoIterator, |
101 | I::IntoIter: DoubleEndedIterator, |
102 | { |
103 | iterable.into_iter().rev() |
104 | } |
105 | |
106 | /// Converts the arguments to iterators and zips them. |
107 | /// |
108 | /// [`IntoIterator`] enabled version of [`Iterator::zip`]. |
109 | /// |
110 | /// ## Example |
111 | /// |
112 | /// ``` |
113 | /// use itertools::zip; |
114 | /// |
115 | /// let mut result: Vec<(i32, char)> = Vec::new(); |
116 | /// |
117 | /// for (a, b) in zip(&[1, 2, 3, 4, 5], &['a' , 'b' , 'c' ]) { |
118 | /// result.push((*a, *b)); |
119 | /// } |
120 | /// assert_eq!(result, vec![(1, 'a' ),(2, 'b' ),(3, 'c' )]); |
121 | /// ``` |
122 | #[deprecated ( |
123 | note = "Use [std::iter::zip](https://doc.rust-lang.org/std/iter/fn.zip.html) instead" , |
124 | since = "0.10.4" |
125 | )] |
126 | pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter> |
127 | where |
128 | I: IntoIterator, |
129 | J: IntoIterator, |
130 | { |
131 | i.into_iter().zip(j) |
132 | } |
133 | |
134 | /// Takes two iterables and creates a new iterator over both in sequence. |
135 | /// |
136 | /// [`IntoIterator`] enabled version of [`Iterator::chain`]. |
137 | /// |
138 | /// ## Example |
139 | /// ``` |
140 | /// use itertools::chain; |
141 | /// |
142 | /// let mut result:Vec<i32> = Vec::new(); |
143 | /// |
144 | /// for element in chain(&[1, 2, 3], &[4]) { |
145 | /// result.push(*element); |
146 | /// } |
147 | /// assert_eq!(result, vec![1, 2, 3, 4]); |
148 | /// ``` |
149 | pub fn chain<I, J>( |
150 | i: I, |
151 | j: J, |
152 | ) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter> |
153 | where |
154 | I: IntoIterator, |
155 | J: IntoIterator<Item = I::Item>, |
156 | { |
157 | i.into_iter().chain(j) |
158 | } |
159 | |
160 | /// Create an iterator that clones each element from &T to T |
161 | /// |
162 | /// [`IntoIterator`] enabled version of [`Iterator::cloned`]. |
163 | /// |
164 | /// ``` |
165 | /// use itertools::cloned; |
166 | /// |
167 | /// assert_eq!(cloned(b"abc" ).next(), Some(b'a' )); |
168 | /// ``` |
169 | pub fn cloned<'a, I, T>(iterable: I) -> iter::Cloned<I::IntoIter> |
170 | where |
171 | I: IntoIterator<Item = &'a T>, |
172 | T: Clone + 'a, |
173 | { |
174 | iterable.into_iter().cloned() |
175 | } |
176 | |
177 | /// Perform a fold operation over the iterable. |
178 | /// |
179 | /// [`IntoIterator`] enabled version of [`Iterator::fold`]. |
180 | /// |
181 | /// ``` |
182 | /// use itertools::fold; |
183 | /// |
184 | /// assert_eq!(fold(&[1., 2., 3.], 0., |a, &b| f32::max(a, b)), 3.); |
185 | /// ``` |
186 | pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B |
187 | where |
188 | I: IntoIterator, |
189 | F: FnMut(B, I::Item) -> B, |
190 | { |
191 | iterable.into_iter().fold(init, f) |
192 | } |
193 | |
194 | /// Test whether the predicate holds for all elements in the iterable. |
195 | /// |
196 | /// [`IntoIterator`] enabled version of [`Iterator::all`]. |
197 | /// |
198 | /// ``` |
199 | /// use itertools::all; |
200 | /// |
201 | /// assert!(all(&[1, 2, 3], |elt| *elt > 0)); |
202 | /// ``` |
203 | pub fn all<I, F>(iterable: I, f: F) -> bool |
204 | where |
205 | I: IntoIterator, |
206 | F: FnMut(I::Item) -> bool, |
207 | { |
208 | iterable.into_iter().all(f) |
209 | } |
210 | |
211 | /// Test whether the predicate holds for any elements in the iterable. |
212 | /// |
213 | /// [`IntoIterator`] enabled version of [`Iterator::any`]. |
214 | /// |
215 | /// ``` |
216 | /// use itertools::any; |
217 | /// |
218 | /// assert!(any(&[0, -1, 2], |elt| *elt > 0)); |
219 | /// ``` |
220 | pub fn any<I, F>(iterable: I, f: F) -> bool |
221 | where |
222 | I: IntoIterator, |
223 | F: FnMut(I::Item) -> bool, |
224 | { |
225 | iterable.into_iter().any(f) |
226 | } |
227 | |
228 | /// Return the maximum value of the iterable. |
229 | /// |
230 | /// [`IntoIterator`] enabled version of [`Iterator::max`]. |
231 | /// |
232 | /// ``` |
233 | /// use itertools::max; |
234 | /// |
235 | /// assert_eq!(max(0..10), Some(9)); |
236 | /// ``` |
237 | pub fn max<I>(iterable: I) -> Option<I::Item> |
238 | where |
239 | I: IntoIterator, |
240 | I::Item: Ord, |
241 | { |
242 | iterable.into_iter().max() |
243 | } |
244 | |
245 | /// Return the minimum value of the iterable. |
246 | /// |
247 | /// [`IntoIterator`] enabled version of [`Iterator::min`]. |
248 | /// |
249 | /// ``` |
250 | /// use itertools::min; |
251 | /// |
252 | /// assert_eq!(min(0..10), Some(0)); |
253 | /// ``` |
254 | pub fn min<I>(iterable: I) -> Option<I::Item> |
255 | where |
256 | I: IntoIterator, |
257 | I::Item: Ord, |
258 | { |
259 | iterable.into_iter().min() |
260 | } |
261 | |
262 | /// Combine all iterator elements into one String, separated by `sep`. |
263 | /// |
264 | /// [`IntoIterator`] enabled version of [`Itertools::join`]. |
265 | /// |
266 | /// ``` |
267 | /// use itertools::join; |
268 | /// |
269 | /// assert_eq!(join(&[1, 2, 3], ", " ), "1, 2, 3" ); |
270 | /// ``` |
271 | #[cfg (feature = "use_alloc" )] |
272 | pub fn join<I>(iterable: I, sep: &str) -> String |
273 | where |
274 | I: IntoIterator, |
275 | I::Item: Display, |
276 | { |
277 | iterable.into_iter().join(sep) |
278 | } |
279 | |
280 | /// Sort all iterator elements into a new iterator in ascending order. |
281 | /// |
282 | /// [`IntoIterator`] enabled version of [`Itertools::sorted`]. |
283 | /// |
284 | /// ``` |
285 | /// use itertools::sorted; |
286 | /// use itertools::assert_equal; |
287 | /// |
288 | /// assert_equal(sorted("rust" .chars()), "rstu" .chars()); |
289 | /// ``` |
290 | #[cfg (feature = "use_alloc" )] |
291 | pub fn sorted<I>(iterable: I) -> VecIntoIter<I::Item> |
292 | where |
293 | I: IntoIterator, |
294 | I::Item: Ord, |
295 | { |
296 | iterable.into_iter().sorted() |
297 | } |
298 | |
299 | /// Sort all iterator elements into a new iterator in ascending order. |
300 | /// This sort is unstable (i.e., may reorder equal elements). |
301 | /// [`IntoIterator`] enabled version of [`Itertools::sorted_unstable`]. |
302 | /// |
303 | /// ``` |
304 | /// use itertools::sorted_unstable; |
305 | /// use itertools::assert_equal; |
306 | /// |
307 | /// assert_equal(sorted_unstable("rust" .chars()), "rstu" .chars()); |
308 | /// ``` |
309 | #[cfg (feature = "use_alloc" )] |
310 | pub fn sorted_unstable<I>(iterable: I) -> VecIntoIter<I::Item> |
311 | where |
312 | I: IntoIterator, |
313 | I::Item: Ord, |
314 | { |
315 | iterable.into_iter().sorted_unstable() |
316 | } |
317 | |