polars_utils/itertools/
mod.rs1use std::cmp::Ordering;
2
3use crate::IdxSize;
4
5pub mod enumerate_idx;
6
7pub trait Itertools: Iterator {
9 fn collect_vec(self) -> Vec<Self::Item>
11 where
12 Self: Sized,
13 {
14 self.collect()
15 }
16
17 fn try_collect<T, U, E>(self) -> Result<U, E>
19 where
20 Self: Sized + Iterator<Item = Result<T, E>>,
21 Result<U, E>: FromIterator<Result<T, E>>,
22 {
23 self.collect()
24 }
25
26 fn try_collect_vec<T, U, E>(self) -> Result<Vec<U>, E>
28 where
29 Self: Sized + Iterator<Item = Result<T, E>>,
30 Result<Vec<U>, E>: FromIterator<Result<T, E>>,
31 {
32 self.collect()
33 }
34
35 fn enumerate_idx(self) -> enumerate_idx::EnumerateIdx<Self, IdxSize>
36 where
37 Self: Sized,
38 {
39 enumerate_idx::EnumerateIdx::new(self)
40 }
41
42 fn enumerate_u32(self) -> enumerate_idx::EnumerateIdx<Self, u32>
43 where
44 Self: Sized,
45 {
46 enumerate_idx::EnumerateIdx::new(self)
47 }
48
49 fn all_equal(mut self) -> bool
50 where
51 Self: Sized,
52 Self::Item: PartialEq,
53 {
54 match self.next() {
55 None => true,
56 Some(a) => self.all(|x| a == x),
57 }
58 }
59
60 fn eq_by_<I, F>(mut self, other: I, mut eq: F) -> bool
62 where
63 Self: Sized,
64 I: IntoIterator,
65 F: FnMut(Self::Item, I::Item) -> bool,
66 {
67 let mut other = other.into_iter();
68 loop {
69 match (self.next(), other.next()) {
70 (None, None) => return true,
71 (None, Some(_)) => return false,
72 (Some(_), None) => return false,
73 (Some(l), Some(r)) => {
74 if eq(l, r) {
75 continue;
76 } else {
77 return false;
78 }
79 },
80 }
81 }
82 }
83
84 fn partial_cmp_by_<I, F>(mut self, other: I, mut partial_cmp: F) -> Option<Ordering>
86 where
87 Self: Sized,
88 I: IntoIterator,
89 F: FnMut(Self::Item, I::Item) -> Option<Ordering>,
90 {
91 let mut other = other.into_iter();
92 loop {
93 match (self.next(), other.next()) {
94 (None, None) => return Some(Ordering::Equal),
95 (None, Some(_)) => return Some(Ordering::Less),
96 (Some(_), None) => return Some(Ordering::Greater),
97 (Some(l), Some(r)) => match partial_cmp(l, r) {
98 Some(Ordering::Equal) => continue,
99 ord => return ord,
100 },
101 }
102 }
103 }
104}
105
106impl<T: Iterator + ?Sized> Itertools for T {}