itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Add chunks_exact method

Open JosephKyser opened this issue 3 years ago • 4 comments

Would like to request an _exact version of the chunks method akin to how slices work. I believe this would be useful functionality.

JosephKyser avatar Dec 10 '22 16:12 JosephKyser

+1 to that!

mzr avatar Feb 29 '24 14:02 mzr

Like array_chunks? Or something else?

jswrenn avatar Feb 29 '24 14:02 jswrenn

I'm unsure how best to represent the tail for this. Slices have it easy, since https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.as_chunks can just split it up-front into the chunks part and the less-than-the-chunk-size tail (https://github.com/rust-lang/rust/issues/76354).

@mzr @JosephKyser Would it be fine for your uses if this only worked on ExactSizeIterators? The simplest version of this would be something like

if self.len() < N { None }
else { Some(array::from_fn(|_| self.inner.next().unwrap())) }

but if it needs to build up state as it go and return that for the tail later it gets much messier.

scottmcm avatar Feb 29 '24 19:02 scottmcm