Skip to content

[Question] Having hard time trying to create a chunk out to a function #1123

Description

@fatboyzz
use anyhow::Result;
use itertools::Itertools;

fn main() -> Result<()> {
    let ns = vec![2, 2, 3, 3, 4, 4, 5, 5];

    let chunk = ns.iter().chunk_by(|n| *n % 2 == 0);
    // let chunk = chunked(ns.iter());

    let group = chunk.into_iter().take(2);
    for (even, ns) in group {
        println!("{} {:?}", even, ns.collect::<Vec<_>>());
    }
    Ok(())
}

// Don't know how to write the return type.
// Note that impl IntoIterator is not an option.
// the trait `IntoIterator` is implemented 
// - for `&itertools::ChunkBy<K, I, F>`
// - not `itertools::ChunkBy<K, I, F>`
//
// fn chunked(ns: impl Iterator<Item = i32>) -> impl IntoIterator {
//   ns.chunk_by(|n| *n % 2 == 0)
// }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions