itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Add array_chunks

Open ronnodas opened this issue 8 months ago • 3 comments
trafficstars

Added an implementation of arrays based on the work in next_array(). Currently the N = 0 produces a post-monomorphization error, see the discussion in #1012.

Some other choices that I'm not sure about:

  • If the number of items yielded by the input iterator is not a multiple of N then there is exactly one allocation the first time next() returns None. I don't think this is a performance issue but this means the method has to be gated behind use_alloc. The alternative would be something like implementing IntoIterator for ArrayBuilder, which seems possible but the safety invariants get more complicated. A middle ground would be to only gate the remainder() method, but this seems worse.
  • Should ArrayChunks::remainder() return a custom type, so that it can be ExactSizeIterator?
  • Is it okay to not specify the behavior if the input iterator is not fused? I think the current implementation behaves slightly differently depending on whether the None is on a multiple of N.

ronnodas avatar Mar 02 '25 18:03 ronnodas

Codecov Report

Attention: Patch coverage is 91.25000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 94.32%. Comparing base (6814180) to head (db21b2f). Report is 137 commits behind head on master.

Files with missing lines Patch % Lines
src/arrays.rs 82.92% 7 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1023      +/-   ##
==========================================
- Coverage   94.38%   94.32%   -0.07%     
==========================================
  Files          48       51       +3     
  Lines        6665     6963     +298     
==========================================
+ Hits         6291     6568     +277     
- Misses        374      395      +21     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Mar 02 '25 18:03 codecov[bot]

Re the MSRV failure, I'm not sure what the idiomatic way is to trigger a PME on 1.63 based on N > 0.

ronnodas avatar Mar 02 '25 23:03 ronnodas

Re the MSRV failure, I'm not sure what the idiomatic way is to trigger a PME on 1.63 based on N > 0.

You can do it like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=233fb2f6ca3df52d15e3df96b8c4cb61

jswrenn avatar Mar 03 '25 00:03 jswrenn

what are the thoughts about having an array_chunks which can fill the partially filled last item by fetching from a filler that the caller provides? I think it would be a very useful API in case people always want a certain length chunks

king-11 avatar Oct 12 '25 10:10 king-11

im curious why you didnt want to just copy the previous stdlib implementation? you can simply implement this on top of a manual as_chunks? returning an owned array and using a vec for remainder is kind of funky.

bend-n avatar Nov 11 '25 07:11 bend-n

@bend-n Do you mean slice::as_chunks? That works for slices, but this is for a general iterator.

There's also a nightly version of this in std: Iterator::array_chunks. If you're asking about the implementation there, I don't think the code in this PR is that different, just trying to reuse things already in the crate. But TBF I haven't looked at either that implementation or this PR closely in a while.

ronnodas avatar Nov 11 '25 07:11 ronnodas

oh, my bad. i forgot that iterator::array_chunks was still unstable and assumed this was with regards to slice array chunks for some reason.

you could still copy the implementation of iterator::array_chunks, though?

bend-n avatar Nov 11 '25 07:11 bend-n