From 1e15089b564222eccfa859804447613ca511fdf1 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Tue, 30 Jan 2024 18:38:25 +0100 Subject: [PATCH 1/8] Deprecate `group_by` in favor of `chunk_by` --- src/groupbylazy.rs | 4 ++-- src/lib.rs | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs index c318ec7b3..08e9e933b 100644 --- a/src/groupbylazy.rs +++ b/src/groupbylazy.rs @@ -294,7 +294,7 @@ where /// value. It should be stored in a local variable or temporary and /// iterated. /// -/// See [`.group_by()`](crate::Itertools::group_by) for more information. +/// See [`.chunk_by()`](crate::Itertools::chunk_by) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] pub struct GroupBy where @@ -368,7 +368,7 @@ where /// Iterator element type is `(K, Group)`: /// the group's key `K` and the group's iterator. /// -/// See [`.group_by()`](crate::Itertools::group_by) for more information. +/// See [`.chunk_by()`](crate::Itertools::chunk_by) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] pub struct Groups<'a, K, I, F> where diff --git a/src/lib.rs b/src/lib.rs index 1f39379e2..bfe4b95a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ //! - `use_std` //! - Enabled by default. //! - Disable to compile itertools using `#![no_std]`. This disables -//! any items that depend on collections (like `group_by`, `unique`, +//! any items that depend on collections (like `chunk_by`, `unique`, //! `kmerge`, `join` and many more). //! //! ## Rust Version @@ -600,13 +600,13 @@ pub trait Itertools: Iterator { /// // Note: The `&` is significant here, `GroupBy` is iterable /// // only by reference. You can also call `.into_iter()` explicitly. /// let mut data_grouped = Vec::new(); - /// for (key, group) in &data.into_iter().group_by(|elt| *elt >= 0) { + /// for (key, group) in &data.into_iter().chunk_by(|elt| *elt >= 0) { /// data_grouped.push((key, group.collect())); /// } /// assert_eq!(data_grouped, vec![(true, vec![1, 3]), (false, vec![-2, -2]), (true, vec![1, 0, 1, 2])]); /// ``` #[cfg(feature = "use_alloc")] - fn group_by(self, key: F) -> GroupBy + fn chunk_by(self, key: F) -> GroupBy where Self: Sized, F: FnMut(&Self::Item) -> K, @@ -615,6 +615,18 @@ pub trait Itertools: Iterator { groupbylazy::new(self, key) } + /// See [`.chunk_by()`](Itertools::chunk_by). + #[deprecated(note = "Use .chunk_by() instead", since = "0.13.0")] + #[cfg(feature = "use_alloc")] + fn group_by(self, key: F) -> GroupBy + where + Self: Sized, + F: FnMut(&Self::Item) -> K, + K: PartialEq, + { + self.chunk_by(key) + } + /// Return an *iterable* that can chunk the iterator. /// /// Yield subiterators (chunks) that each yield a fixed number elements, From 3471dc0a45dbd1c19abc724ea9ccb9d9c9842eac Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:11:04 +0100 Subject: [PATCH 2/8] Update `group_by` doc example --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bfe4b95a9..d0043b199 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -593,15 +593,15 @@ pub trait Itertools: Iterator { /// ``` /// use itertools::Itertools; /// - /// // group data into runs of larger than zero or not. + /// // chunk data into runs of larger than zero or not. /// let data = vec![1, 3, -2, -2, 1, 0, 1, 2]; - /// // groups: |---->|------>|--------->| + /// // chunks: |---->|------>|--------->| /// /// // Note: The `&` is significant here, `GroupBy` is iterable /// // only by reference. You can also call `.into_iter()` explicitly. /// let mut data_grouped = Vec::new(); - /// for (key, group) in &data.into_iter().chunk_by(|elt| *elt >= 0) { - /// data_grouped.push((key, group.collect())); + /// for (key, chunk) in &data.into_iter().chunk_by(|elt| *elt >= 0) { + /// data_grouped.push((key, chunk.collect())); /// } /// assert_eq!(data_grouped, vec![(true, vec![1, 3]), (false, vec![-2, -2]), (true, vec![1, 0, 1, 2])]); /// ``` From 611eca0dc05964927c552d2f6041b509a31a1bd5 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Tue, 30 Jan 2024 23:03:21 +0100 Subject: [PATCH 3/8] Deprecate `GroupBy` in favor of `ChunkBy` --- src/groupbylazy.rs | 26 +++++++++++++++----------- src/lib.rs | 17 ++++++++++------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs index 08e9e933b..007fea6f6 100644 --- a/src/groupbylazy.rs +++ b/src/groupbylazy.rs @@ -1,7 +1,7 @@ use alloc::vec::{self, Vec}; use std::cell::{Cell, RefCell}; -/// A trait to unify `FnMut` for `GroupBy` with the chunk key in `IntoChunks` +/// A trait to unify `FnMut` for `ChunkBy` with the chunk key in `IntoChunks` trait KeyFunction { type Key; fn call_mut(&mut self, arg: A) -> Self::Key; @@ -282,10 +282,14 @@ where } } -/// `GroupBy` is the storage for the lazy grouping operation. +#[deprecated(note = "Use `ChunkBy` instead", since = "0.13.0")] +/// See [`ChunkBy`](crate::structs::ChunkBy). +pub type GroupBy = ChunkBy; + +/// `ChunkBy` is the storage for the lazy grouping operation. /// /// If the groups are consumed in their original order, or if each -/// group is dropped without keeping it around, then `GroupBy` uses +/// group is dropped without keeping it around, then `ChunkBy` uses /// no allocations. It needs allocations only if several group iterators /// are alive at the same time. /// @@ -296,7 +300,7 @@ where /// /// See [`.chunk_by()`](crate::Itertools::chunk_by) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct GroupBy +pub struct ChunkBy where I: Iterator, { @@ -307,12 +311,12 @@ where } /// Create a new -pub fn new(iter: J, f: F) -> GroupBy +pub fn new(iter: J, f: F) -> ChunkBy where J: IntoIterator, F: FnMut(&J::Item) -> K, { - GroupBy { + ChunkBy { inner: RefCell::new(GroupInner { key: f, iter: iter.into_iter(), @@ -329,7 +333,7 @@ where } } -impl GroupBy +impl ChunkBy where I: Iterator, { @@ -348,7 +352,7 @@ where } } -impl<'a, K, I, F> IntoIterator for &'a GroupBy +impl<'a, K, I, F> IntoIterator for &'a ChunkBy where I: Iterator, I::Item: 'a, @@ -377,7 +381,7 @@ where K: 'a, F: 'a, { - parent: &'a GroupBy, + parent: &'a ChunkBy, } impl<'a, K, I, F> Iterator for Groups<'a, K, I, F> @@ -418,7 +422,7 @@ where K: 'a, F: 'a, { - parent: &'a GroupBy, + parent: &'a ChunkBy, index: usize, first: Option, } @@ -476,7 +480,7 @@ where /// `ChunkLazy` is the storage for a lazy chunking operation. /// -/// `IntoChunks` behaves just like `GroupBy`: it is iterable, and +/// `IntoChunks` behaves just like `ChunkBy`: it is iterable, and /// it only buffers if several chunk iterators are alive at the same time. /// /// This type implements [`IntoIterator`] (it is **not** an iterator diff --git a/src/lib.rs b/src/lib.rs index d0043b199..f12d95a1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,8 +99,11 @@ pub mod structs { pub use crate::exactly_one_err::ExactlyOneError; pub use crate::flatten_ok::FlattenOk; pub use crate::format::{Format, FormatWith}; + #[allow(deprecated)] + #[cfg(feature = "use_alloc")] + pub use crate::groupbylazy::GroupBy; #[cfg(feature = "use_alloc")] - pub use crate::groupbylazy::{Chunk, Chunks, Group, GroupBy, Groups, IntoChunks}; + pub use crate::groupbylazy::{Chunk, ChunkBy, Chunks, Group, Groups, IntoChunks}; #[cfg(feature = "use_std")] pub use crate::grouping_map::{GroupingMap, GroupingMapBy}; pub use crate::intersperse::{Intersperse, IntersperseWith}; @@ -575,10 +578,10 @@ pub trait Itertools: Iterator { /// Consecutive elements that map to the same key (“runs”), are assigned /// to the same group. /// - /// `GroupBy` is the storage for the lazy grouping operation. + /// `ChunkBy` is the storage for the lazy grouping operation. /// /// If the groups are consumed in order, or if each group's iterator is - /// dropped without keeping it around, then `GroupBy` uses no + /// dropped without keeping it around, then `ChunkBy` uses no /// allocations. It needs allocations only if several group iterators /// are alive at the same time. /// @@ -597,7 +600,7 @@ pub trait Itertools: Iterator { /// let data = vec![1, 3, -2, -2, 1, 0, 1, 2]; /// // chunks: |---->|------>|--------->| /// - /// // Note: The `&` is significant here, `GroupBy` is iterable + /// // Note: The `&` is significant here, `ChunkBy` is iterable /// // only by reference. You can also call `.into_iter()` explicitly. /// let mut data_grouped = Vec::new(); /// for (key, chunk) in &data.into_iter().chunk_by(|elt| *elt >= 0) { @@ -606,7 +609,7 @@ pub trait Itertools: Iterator { /// assert_eq!(data_grouped, vec![(true, vec![1, 3]), (false, vec![-2, -2]), (true, vec![1, 0, 1, 2])]); /// ``` #[cfg(feature = "use_alloc")] - fn chunk_by(self, key: F) -> GroupBy + fn chunk_by(self, key: F) -> ChunkBy where Self: Sized, F: FnMut(&Self::Item) -> K, @@ -618,7 +621,7 @@ pub trait Itertools: Iterator { /// See [`.chunk_by()`](Itertools::chunk_by). #[deprecated(note = "Use .chunk_by() instead", since = "0.13.0")] #[cfg(feature = "use_alloc")] - fn group_by(self, key: F) -> GroupBy + fn group_by(self, key: F) -> ChunkBy where Self: Sized, F: FnMut(&Self::Item) -> K, @@ -633,7 +636,7 @@ pub trait Itertools: Iterator { /// determined by `size`. The last chunk will be shorter if there aren't /// enough elements. /// - /// `IntoChunks` is based on `GroupBy`: it is iterable (implements + /// `IntoChunks` is based on `ChunkBy`: it is iterable (implements /// `IntoIterator`, **not** `Iterator`), and it only buffers if several /// chunk iterators are alive at the same time. /// From abcf2a5fce8d013723b2f99a59d48daa63172b5a Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:35:56 +0100 Subject: [PATCH 4/8] Fix `iris.rs` --- examples/iris.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/iris.rs b/examples/iris.rs index 5f5525154..a041b2849 100644 --- a/examples/iris.rs +++ b/examples/iris.rs @@ -77,15 +77,15 @@ fn main() { let mut plot_symbols = "+ox".chars().cycle(); let mut symbolmap = HashMap::new(); - // using Itertools::group_by - for (species, species_group) in &irises.iter().group_by(|iris| &iris.name) { + // using Itertools::chunk_by + for (species, species_chunk) in &irises.iter().chunk_by(|iris| &iris.name) { // assign a plot symbol symbolmap .entry(species) .or_insert_with(|| plot_symbols.next().unwrap()); println!("{} (symbol={})", species, symbolmap[species]); - for iris in species_group { + for iris in species_chunk { // using Itertools::format for lazy formatting println!("{:>3.1}", iris.data.iter().format(", ")); } From 103145213a08ec6283c051b6f822e53ea8a6f75d Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:38:11 +0100 Subject: [PATCH 5/8] Fix `bench1.rs` --- benches/bench1.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/benches/bench1.rs b/benches/bench1.rs index 8d8881676..f718ce2c9 100644 --- a/benches/bench1.rs +++ b/benches/bench1.rs @@ -390,7 +390,7 @@ fn zip_unchecked_counted_loop3(c: &mut Criterion) { }); } -fn group_by_lazy_1(c: &mut Criterion) { +fn chunk_by_lazy_1(c: &mut Criterion) { let mut data = vec![0; 1024]; for (index, elt) in data.iter_mut().enumerate() { *elt = index / 10; @@ -398,10 +398,10 @@ fn group_by_lazy_1(c: &mut Criterion) { let data = black_box(data); - c.bench_function("group by lazy 1", move |b| { + c.bench_function("chunk by lazy 1", move |b| { b.iter(|| { - for (_key, group) in &data.iter().group_by(|elt| **elt) { - for elt in group { + for (_key, chunk) in &data.iter().chunk_by(|elt| **elt) { + for elt in chunk { black_box(elt); } } @@ -409,7 +409,7 @@ fn group_by_lazy_1(c: &mut Criterion) { }); } -fn group_by_lazy_2(c: &mut Criterion) { +fn chunk_by_lazy_2(c: &mut Criterion) { let mut data = vec![0; 1024]; for (index, elt) in data.iter_mut().enumerate() { *elt = index / 2; @@ -417,10 +417,10 @@ fn group_by_lazy_2(c: &mut Criterion) { let data = black_box(data); - c.bench_function("group by lazy 2", move |b| { + c.bench_function("chunk by lazy 2", move |b| { b.iter(|| { - for (_key, group) in &data.iter().group_by(|elt| **elt) { - for elt in group { + for (_key, chunk) in &data.iter().chunk_by(|elt| **elt) { + for elt in chunk { black_box(elt); } } @@ -436,8 +436,8 @@ fn slice_chunks(c: &mut Criterion) { c.bench_function("slice chunks", move |b| { b.iter(|| { - for group in data.chunks(sz) { - for elt in group { + for chunk in data.chunks(sz) { + for elt in chunk { black_box(elt); } } @@ -453,8 +453,8 @@ fn chunks_lazy_1(c: &mut Criterion) { c.bench_function("chunks lazy 1", move |b| { b.iter(|| { - for group in &data.iter().chunks(sz) { - for elt in group { + for chunk in &data.iter().chunks(sz) { + for elt in chunk { black_box(elt); } } @@ -813,8 +813,8 @@ criterion_group!( zipdot_i32_unchecked_counted_loop, zipdot_f32_unchecked_counted_loop, zip_unchecked_counted_loop3, - group_by_lazy_1, - group_by_lazy_2, + chunk_by_lazy_1, + chunk_by_lazy_2, slice_chunks, chunks_lazy_1, equal, From 9993de7dc409b0465b759ffc91a4fd2374a329e1 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:41:43 +0100 Subject: [PATCH 6/8] Fix tests 1: `group_by` to `chunk_by` --- tests/quick.rs | 16 ++++++++-------- tests/test_std.rs | 34 +++++++++++++++++----------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/quick.rs b/tests/quick.rs index 954655a43..e7c7c55ce 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -1053,24 +1053,24 @@ quickcheck! { } quickcheck! { - fn fuzz_group_by_lazy_1(it: Iter) -> bool { + fn fuzz_chunk_by_lazy_1(it: Iter) -> bool { let jt = it.clone(); - let groups = it.group_by(|k| *k); + let groups = it.chunk_by(|k| *k); itertools::equal(jt, groups.into_iter().flat_map(|(_, x)| x)) } } quickcheck! { - fn fuzz_group_by_lazy_2(data: Vec) -> bool { - let groups = data.iter().group_by(|k| *k / 10); + fn fuzz_chunk_by_lazy_2(data: Vec) -> bool { + let groups = data.iter().chunk_by(|k| *k / 10); let res = itertools::equal(data.iter(), groups.into_iter().flat_map(|(_, x)| x)); res } } quickcheck! { - fn fuzz_group_by_lazy_3(data: Vec) -> bool { - let grouper = data.iter().group_by(|k| *k / 10); + fn fuzz_chunk_by_lazy_3(data: Vec) -> bool { + let grouper = data.iter().chunk_by(|k| *k / 10); let groups = grouper.into_iter().collect_vec(); let res = itertools::equal(data.iter(), groups.into_iter().flat_map(|(_, x)| x)); res @@ -1078,8 +1078,8 @@ quickcheck! { } quickcheck! { - fn fuzz_group_by_lazy_duo(data: Vec, order: Vec<(bool, bool)>) -> bool { - let grouper = data.iter().group_by(|k| *k / 3); + fn fuzz_chunk_by_lazy_duo(data: Vec, order: Vec<(bool, bool)>) -> bool { + let grouper = data.iter().chunk_by(|k| *k / 3); let mut groups1 = grouper.into_iter(); let mut groups2 = grouper.into_iter(); let mut elts = Vec::<&u8>::new(); diff --git a/tests/test_std.rs b/tests/test_std.rs index 5d2864573..402385488 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -275,7 +275,7 @@ fn all_equal() { assert!("A".chars().all_equal()); assert!(!"AABBCCC".chars().all_equal()); assert!("AAAAAAA".chars().all_equal()); - for (_key, mut sub) in &"AABBCCC".chars().group_by(|&x| x) { + for (_key, mut sub) in &"AABBCCC".chars().chunk_by(|&x| x) { assert!(sub.all_equal()); } } @@ -797,14 +797,14 @@ fn pad_using() { } #[test] -fn group_by() { - for (ch1, sub) in &"AABBCCC".chars().group_by(|&x| x) { +fn chunk_by() { + for (ch1, sub) in &"AABBCCC".chars().chunk_by(|&x| x) { for ch2 in sub { assert_eq!(ch1, ch2); } } - for (ch1, sub) in &"AAABBBCCCCDDDD".chars().group_by(|&x| x) { + for (ch1, sub) in &"AAABBBCCCCDDDD".chars().chunk_by(|&x| x) { for ch2 in sub { assert_eq!(ch1, ch2); if ch1 == 'C' { @@ -817,7 +817,7 @@ fn group_by() { // try all possible orderings for indices in permutohedron::Heap::new(&mut [0, 1, 2, 3]) { - let groups = "AaaBbbccCcDDDD".chars().group_by(&toupper); + let groups = "AaaBbbccCcDDDD".chars().chunk_by(&toupper); let mut subs = groups.into_iter().collect_vec(); for &idx in &indices[..] { @@ -833,7 +833,7 @@ fn group_by() { } } - let groups = "AAABBBCCCCDDDD".chars().group_by(|&x| x); + let groups = "AAABBBCCCCDDDD".chars().chunk_by(|&x| x); let mut subs = groups.into_iter().map(|(_, g)| g).collect_vec(); let sd = subs.pop().unwrap(); @@ -851,7 +851,7 @@ fn group_by() { { let mut ntimes = 0; let text = "AABCCC"; - for (_, sub) in &text.chars().group_by(|&x| { + for (_, sub) in &text.chars().chunk_by(|&x| { ntimes += 1; x }) { @@ -863,7 +863,7 @@ fn group_by() { { let mut ntimes = 0; let text = "AABCCC"; - for _ in &text.chars().group_by(|&x| { + for _ in &text.chars().chunk_by(|&x| { ntimes += 1; x }) {} @@ -872,25 +872,25 @@ fn group_by() { { let text = "ABCCCDEEFGHIJJKK"; - let gr = text.chars().group_by(|&x| x); + let gr = text.chars().chunk_by(|&x| x); it::assert_equal(gr.into_iter().flat_map(|(_, sub)| sub), text.chars()); } } #[test] -fn group_by_lazy_2() { +fn chunk_by_lazy_2() { let data = [0, 1]; - let groups = data.iter().group_by(|k| *k); + let groups = data.iter().chunk_by(|k| *k); let gs = groups.into_iter().collect_vec(); it::assert_equal(data.iter(), gs.into_iter().flat_map(|(_k, g)| g)); let data = [0, 1, 1, 0, 0]; - let groups = data.iter().group_by(|k| *k); + let groups = data.iter().chunk_by(|k| *k); let mut gs = groups.into_iter().collect_vec(); gs[1..].reverse(); it::assert_equal(&[0, 0, 0, 1, 1], gs.into_iter().flat_map(|(_, g)| g)); - let grouper = data.iter().group_by(|k| *k); + let grouper = data.iter().chunk_by(|k| *k); let mut groups = Vec::new(); for (k, group) in &grouper { if *k == 1 { @@ -900,7 +900,7 @@ fn group_by_lazy_2() { it::assert_equal(&mut groups[0], &[1, 1]); let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; - let grouper = data.iter().group_by(|k| *k); + let grouper = data.iter().chunk_by(|k| *k); let mut groups = Vec::new(); for (i, (_, group)) in grouper.into_iter().enumerate() { if i < 2 { @@ -918,7 +918,7 @@ fn group_by_lazy_2() { // use groups as chunks let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; let mut i = 0; - let grouper = data.iter().group_by(move |_| { + let grouper = data.iter().chunk_by(move |_| { let k = i / 3; i += 1; k @@ -935,10 +935,10 @@ fn group_by_lazy_2() { } #[test] -fn group_by_lazy_3() { +fn chunk_by_lazy_3() { // test consuming each group on the lap after it was produced let data = [0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2]; - let grouper = data.iter().group_by(|elt| *elt); + let grouper = data.iter().chunk_by(|elt| *elt); let mut last = None; for (key, group) in &grouper { if let Some(gr) = last.take() { From 1146cfd0318dc5727fdfa507fd0f52c00790bf39 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:43:19 +0100 Subject: [PATCH 7/8] Fix tests 2: `groups` to `chunks` (variables) --- tests/quick.rs | 30 +++++++++++++++--------------- tests/test_std.rs | 35 +++++++++++++++++------------------ 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/tests/quick.rs b/tests/quick.rs index e7c7c55ce..f96418f40 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -1055,15 +1055,15 @@ quickcheck! { quickcheck! { fn fuzz_chunk_by_lazy_1(it: Iter) -> bool { let jt = it.clone(); - let groups = it.chunk_by(|k| *k); - itertools::equal(jt, groups.into_iter().flat_map(|(_, x)| x)) + let chunks = it.chunk_by(|k| *k); + itertools::equal(jt, chunks.into_iter().flat_map(|(_, x)| x)) } } quickcheck! { fn fuzz_chunk_by_lazy_2(data: Vec) -> bool { - let groups = data.iter().chunk_by(|k| *k / 10); - let res = itertools::equal(data.iter(), groups.into_iter().flat_map(|(_, x)| x)); + let chunks = data.iter().chunk_by(|k| *k / 10); + let res = itertools::equal(data.iter(), chunks.into_iter().flat_map(|(_, x)| x)); res } } @@ -1071,8 +1071,8 @@ quickcheck! { quickcheck! { fn fuzz_chunk_by_lazy_3(data: Vec) -> bool { let grouper = data.iter().chunk_by(|k| *k / 10); - let groups = grouper.into_iter().collect_vec(); - let res = itertools::equal(data.iter(), groups.into_iter().flat_map(|(_, x)| x)); + let chunks = grouper.into_iter().collect_vec(); + let res = itertools::equal(data.iter(), chunks.into_iter().flat_map(|(_, x)| x)); res } } @@ -1080,31 +1080,31 @@ quickcheck! { quickcheck! { fn fuzz_chunk_by_lazy_duo(data: Vec, order: Vec<(bool, bool)>) -> bool { let grouper = data.iter().chunk_by(|k| *k / 3); - let mut groups1 = grouper.into_iter(); - let mut groups2 = grouper.into_iter(); + let mut chunks1 = grouper.into_iter(); + let mut chunks2 = grouper.into_iter(); let mut elts = Vec::<&u8>::new(); - let mut old_groups = Vec::new(); + let mut old_chunks = Vec::new(); let tup1 = |(_, b)| b; for &(ord, consume_now) in &order { - let iter = &mut [&mut groups1, &mut groups2][ord as usize]; + let iter = &mut [&mut chunks1, &mut chunks2][ord as usize]; match iter.next() { Some((_, gr)) => if consume_now { - for og in old_groups.drain(..) { + for og in old_chunks.drain(..) { elts.extend(og); } elts.extend(gr); } else { - old_groups.push(gr); + old_chunks.push(gr); }, None => break, } } - for og in old_groups.drain(..) { + for og in old_chunks.drain(..) { elts.extend(og); } - for gr in groups1.map(&tup1) { elts.extend(gr); } - for gr in groups2.map(&tup1) { elts.extend(gr); } + for gr in chunks1.map(&tup1) { elts.extend(gr); } + for gr in chunks2.map(&tup1) { elts.extend(gr); } itertools::assert_equal(&data, elts); true } diff --git a/tests/test_std.rs b/tests/test_std.rs index 402385488..7852de2ff 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -817,8 +817,8 @@ fn chunk_by() { // try all possible orderings for indices in permutohedron::Heap::new(&mut [0, 1, 2, 3]) { - let groups = "AaaBbbccCcDDDD".chars().chunk_by(&toupper); - let mut subs = groups.into_iter().collect_vec(); + let chunks = "AaaBbbccCcDDDD".chars().chunk_by(&toupper); + let mut subs = chunks.into_iter().collect_vec(); for &idx in &indices[..] { let (key, text) = match idx { @@ -833,8 +833,8 @@ fn chunk_by() { } } - let groups = "AAABBBCCCCDDDD".chars().chunk_by(|&x| x); - let mut subs = groups.into_iter().map(|(_, g)| g).collect_vec(); + let chunks = "AAABBBCCCCDDDD".chars().chunk_by(|&x| x); + let mut subs = chunks.into_iter().map(|(_, g)| g).collect_vec(); let sd = subs.pop().unwrap(); let sc = subs.pop().unwrap(); @@ -880,42 +880,41 @@ fn chunk_by() { #[test] fn chunk_by_lazy_2() { let data = [0, 1]; - let groups = data.iter().chunk_by(|k| *k); - let gs = groups.into_iter().collect_vec(); + let chunks = data.iter().chunk_by(|k| *k); + let gs = chunks.into_iter().collect_vec(); it::assert_equal(data.iter(), gs.into_iter().flat_map(|(_k, g)| g)); let data = [0, 1, 1, 0, 0]; - let groups = data.iter().chunk_by(|k| *k); - let mut gs = groups.into_iter().collect_vec(); + let chunks = data.iter().chunk_by(|k| *k); + let mut gs = chunks.into_iter().collect_vec(); gs[1..].reverse(); it::assert_equal(&[0, 0, 0, 1, 1], gs.into_iter().flat_map(|(_, g)| g)); let grouper = data.iter().chunk_by(|k| *k); - let mut groups = Vec::new(); + let mut chunks = Vec::new(); for (k, group) in &grouper { if *k == 1 { - groups.push(group); + chunks.push(group); } } - it::assert_equal(&mut groups[0], &[1, 1]); + it::assert_equal(&mut chunks[0], &[1, 1]); let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; let grouper = data.iter().chunk_by(|k| *k); - let mut groups = Vec::new(); + let mut chunks = Vec::new(); for (i, (_, group)) in grouper.into_iter().enumerate() { if i < 2 { - groups.push(group); + chunks.push(group); } else if i < 4 { for _ in group {} } else { - groups.push(group); + chunks.push(group); } } - it::assert_equal(&mut groups[0], &[0, 0, 0]); - it::assert_equal(&mut groups[1], &[1, 1]); - it::assert_equal(&mut groups[2], &[3, 3]); + it::assert_equal(&mut chunks[0], &[0, 0, 0]); + it::assert_equal(&mut chunks[1], &[1, 1]); + it::assert_equal(&mut chunks[2], &[3, 3]); - // use groups as chunks let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; let mut i = 0; let grouper = data.iter().chunk_by(move |_| { From 14f5b6225550b0a687de1b9ec6422543112ba621 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet <44676486+Philippe-Cholet@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:43:31 +0100 Subject: [PATCH 8/8] Fix tests 3: `group` to `chunk` (variables) Remain `grouper`, rename it to what? --- tests/test_std.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/test_std.rs b/tests/test_std.rs index 7852de2ff..7266fdfef 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -892,9 +892,9 @@ fn chunk_by_lazy_2() { let grouper = data.iter().chunk_by(|k| *k); let mut chunks = Vec::new(); - for (k, group) in &grouper { + for (k, chunk) in &grouper { if *k == 1 { - chunks.push(group); + chunks.push(chunk); } } it::assert_equal(&mut chunks[0], &[1, 1]); @@ -902,13 +902,13 @@ fn chunk_by_lazy_2() { let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; let grouper = data.iter().chunk_by(|k| *k); let mut chunks = Vec::new(); - for (i, (_, group)) in grouper.into_iter().enumerate() { + for (i, (_, chunk)) in grouper.into_iter().enumerate() { if i < 2 { - chunks.push(group); + chunks.push(chunk); } else if i < 4 { - for _ in group {} + for _ in chunk {} } else { - chunks.push(group); + chunks.push(chunk); } } it::assert_equal(&mut chunks[0], &[0, 0, 0]); @@ -922,12 +922,12 @@ fn chunk_by_lazy_2() { i += 1; k }); - for (i, group) in &grouper { + for (i, chunk) in &grouper { match i { - 0 => it::assert_equal(group, &[0, 0, 0]), - 1 => it::assert_equal(group, &[1, 1, 0]), - 2 => it::assert_equal(group, &[0, 2, 2]), - 3 => it::assert_equal(group, &[3, 3]), + 0 => it::assert_equal(chunk, &[0, 0, 0]), + 1 => it::assert_equal(chunk, &[1, 1, 0]), + 2 => it::assert_equal(chunk, &[0, 2, 2]), + 3 => it::assert_equal(chunk, &[3, 3]), _ => unreachable!(), } } @@ -935,17 +935,17 @@ fn chunk_by_lazy_2() { #[test] fn chunk_by_lazy_3() { - // test consuming each group on the lap after it was produced + // test consuming each chunk on the lap after it was produced let data = [0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2]; let grouper = data.iter().chunk_by(|elt| *elt); let mut last = None; - for (key, group) in &grouper { + for (key, chunk) in &grouper { if let Some(gr) = last.take() { for elt in gr { assert!(elt != key && i32::abs(elt - key) == 1); } } - last = Some(group); + last = Some(chunk); } }