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")]
7use std::fmt::Display;
8use std::iter::{self, Zip};
9#[cfg(feature = "use_alloc")]
10type VecIntoIter<T> = alloc::vec::IntoIter<T>;
11
12#[cfg(feature = "use_alloc")]
13use alloc::string::String;
14
15use crate::intersperse::{Intersperse, IntersperseWith};
16use crate::Itertools;
17
18pub use crate::adaptors::{interleave, put_back};
19#[cfg(feature = "use_alloc")]
20pub use crate::kmerge_impl::kmerge;
21pub use crate::merge_join::{merge, merge_join_by};
22#[cfg(feature = "use_alloc")]
23pub use crate::multipeek_impl::multipeek;
24#[cfg(feature = "use_alloc")]
25pub use crate::peek_nth::peek_nth;
26#[cfg(feature = "use_alloc")]
27pub use crate::put_back_n_impl::put_back_n;
28#[cfg(feature = "use_alloc")]
29pub use crate::rciter_impl::rciter;
30pub 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/// ```
41pub fn intersperse<I>(iterable: I, element: I::Item) -> Intersperse<I::IntoIter>
42where
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/// ```
61pub fn intersperse_with<I, F>(iterable: I, element: F) -> IntersperseWith<I::IntoIter, F>
62where
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/// ```
80pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter>
81where
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/// ```
98pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter>
99where
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)]
126pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter>
127where
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/// ```
149pub fn chain<I, J>(
150 i: I,
151 j: J,
152) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter>
153where
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/// ```
169pub fn cloned<'a, I, T>(iterable: I) -> iter::Cloned<I::IntoIter>
170where
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/// ```
186pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B
187where
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/// ```
203pub fn all<I, F>(iterable: I, f: F) -> bool
204where
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/// ```
220pub fn any<I, F>(iterable: I, f: F) -> bool
221where
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/// ```
237pub fn max<I>(iterable: I) -> Option<I::Item>
238where
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/// ```
254pub fn min<I>(iterable: I) -> Option<I::Item>
255where
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")]
272pub fn join<I>(iterable: I, sep: &str) -> String
273where
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")]
291pub fn sorted<I>(iterable: I) -> VecIntoIter<I::Item>
292where
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")]
310pub fn sorted_unstable<I>(iterable: I) -> VecIntoIter<I::Item>
311where
312 I: IntoIterator,
313 I::Item: Ord,
314{
315 iterable.into_iter().sorted_unstable()
316}
317