-
Notifications
You must be signed in to change notification settings - Fork 308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chunk_by
returns only the last value when collected
#978
Comments
I think it chunks *consecutive*, so that your example leads to 1-element-chunks. Then, collecting into hashmap will overwrite values if they’re already present.
Try collecting into a Vec instead of Hashmap to see the result.
I think you’re looking for group_by or similar (sorry, I’m on mobile and cannot look up the details.)
Am Donnerstag, 1. August 2024, 20:22:37 MESZ hat Evaldas Buinauskas ***@***.***> Folgendes geschrieben:
use std::collections::HashMap;
use itertools::Itertools;
fn main() {
let numbers = vec![1, 2, 3, 4, 5, 6];
let chunks: HashMap<i32, Vec<i32>> = numbers
.into_iter()
.chunk_by(|number| number % 2)
.into_iter()
.map(|(k, v)| (k, v.collect()))
.collect();
let expectation = HashMap::from([
(0, vec![0, 2, 4, 6]),
(1, vec![1, 3, 5]),
]);
assert_eq!(chunks, expectation);
}
This is the full code and I would expect chunk_by to group elements by the result of closure and build a map out of it, however when running the equality assertion panics with:
assertion `left == right` failed
left: {0: [6], 1: [5]}
right: {1: [1, 3, 5], 0: [0, 2, 4, 6]}
- stable-aarch64-apple-darwin unchanged - rustc 1.80.0 (051478957 2024-07-21)
Is that by design? The method example explicitly shows a loop when it iterates over the chunked results and does not call collect on the iterator.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
In the past I used But apparently, there's |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the full code and I would expect
chunk_by
to group elements by the result of closure and build a map out of it, however when running the equality assertion panics with:Is that by design? The method example explicitly shows a loop when it iterates over the chunked results and does not call collect on the iterator.
The text was updated successfully, but these errors were encountered: