Skip to content
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

What should happen if length of collection is less than n? #58

Open
nhz2 opened this issue Mar 20, 2025 · 2 comments · May be fixed by #59
Open

What should happen if length of collection is less than n? #58

nhz2 opened this issue Mar 20, 2025 · 2 comments · May be fixed by #59

Comments

@nhz2
Copy link

nhz2 commented Mar 20, 2025

I am getting an error when using chunk if the length of the collection is less than n. I'm not sure if this is intended or a bug.

julia> using ChunkSplitters

julia> chunks(1:0; n=4)[3]
0:-1

julia> chunks(1:2; n=4)[3]
ERROR: ArgumentError: ichunk must be less or equal to the length of the ChunksIterator
Stacktrace:
 [1] getchunkindices(c::ChunkSplitters.Internals.ViewChunks{…}, ichunk::Int64)
   @ ChunkSplitters.Internals ~/.julia/packages/ChunkSplitters/p2yrz/src/internals.jl:158
 [2] getindex(c::ChunkSplitters.Internals.ViewChunks{…}, i::Int64)
   @ ChunkSplitters.Internals ~/.julia/packages/ChunkSplitters/p2yrz/src/internals.jl:102
 [3] top-level scope
   @ REPL[21]:1
Some type information was truncated. Use `show(err)` to see complete types.

I would expect chunks(1:2; n=4)[3] to also be an empty range to be consistent with chunks(1:0; n=4)[3].

@lmiq
Copy link
Collaborator

lmiq commented Mar 20, 2025

The error is expected, what I think is not expected is not throwing an error for chunks(1:0; n=4)[3], since the iterator is empty. I'll check if we did that on purpose for some reason.

@lmiq lmiq linked a pull request Mar 20, 2025 that will close this issue
@lmiq
Copy link
Collaborator

lmiq commented Mar 20, 2025

In the PR linked above the behavior becomes consistent in the sense that it will throw a bounds error for empty iterators, when direct indexing is intended. This won´t affect the collect for empty iterators, returning an empty collection of chunks, which was an intentional feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants