itertools icon indicating copy to clipboard operation
itertools copied to clipboard

`chunk_by` returns only the last value when collected

Open buinauskas opened this issue 1 year ago • 2 comments

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![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.

buinauskas avatar Aug 01 '24 18:08 buinauskas

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: @.***>

phimuemue avatar Aug 01 '24 19:08 phimuemue

In the past I used group_by and it has a deprecation warning telling me to use chunk_by, so that's what I did.

But apparently, there's into_group_map_by which does exactly what I'm after. This only left me with an open question whether that functionality is by design and whether it's expected.

buinauskas avatar Aug 02 '24 06:08 buinauskas