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

adds the nth function for iterables #56580

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import .Base:
getindex, setindex!, get, iterate,
popfirst!, isdone, peek, intersect

export enumerate, zip, rest, countfrom, take, drop, takewhile, dropwhile, cycle, repeated, product, flatten, flatmap, partition
export enumerate, zip, rest, countfrom, take, drop, takewhile, dropwhile, cycle, repeated, product, flatten, flatmap, partition, nth
public accumulate, filter, map, peel, reverse, Stateful

"""
Expand Down Expand Up @@ -1595,4 +1595,72 @@ end
# be the same as the keys, so this is a valid optimization (see #51631)
pairs(s::AbstractString) = IterableStatePairs(s)

"""
nth(itr, n::Integer)

Get the `n`th element of an iterable collection. Return `nothing` if not existing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning nothing makes it impossible to distinguish between "the nth element was nothing", and "there was no nth element". Perhaps return Union{Nothing, Some}?

Copy link
Contributor Author

@ghyatzo ghyatzo Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point.
Should it be Union{nothing, Some} even in those cases where we know there can't be a nothing value in the iterator (for sake of uniform api)? I.e. Count Iterator or Repeated (with its element different than nothing) or AbstractRanges

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should, otherwise it would be too confusing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just throw an error if there is no nth element. There could also be a default argument as in get, where a user can pass a value that should be returned if no nth element exists.

I don't really follow the logic that the spirit of iterators is to return nothing in such cases?

Copy link
Contributor

@mcabbott mcabbott Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree nothing is weird, your iterator can produce that. Some seems a bit technical & unfriendly? An error seems fine. Matches what first([]) does.

I suppose it can't literally be a method of get since it goes by enumeration not keys:

julia> first(Dict('a':'z' .=> 'A':'Z'), 3)
3-element Vector{Pair{Char, Char}}:
 'n' => 'N'
 'f' => 'F'
 'w' => 'W'

julia> nth(Dict('a':'z' .=> 'A':'Z'), 3)
'w' => 'W'


ghyatzo marked this conversation as resolved.
Show resolved Hide resolved
See also: [`first`](@ref), [`last`](@ref)

# Examples
```jldoctest
julia> nth(2:2:10, 4)
8

julia> nth(reshape(1:30, (5,6)), 6)
6
```
"""
nth(itr, n::Integer) = _nth(IteratorSize(itr), itr, n)
nth(itr::AbstractArray, n::Integer) = n > length(itr) ? nothing : itr[n]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes one-based indexing. Perhaps do itr[begin + n - 1].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are absolutely correct.
would something like getindex(itr, nth(eachindex(IndexLinear(), itr), n)) be too overkill?
and adding a specialization with nth(itr::AbstractRange, n::Integer) = getindex(itr, n)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with the probably overkill approach, if it's too much i'll revert back to your suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AbstractRanges are not always one-based either, so that approach runs into the same issue

Copy link
Contributor Author

@ghyatzo ghyatzo Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I could gather that is included in the getindex already, since it ends up calling

unsafe_getindex(v::AbstractRange{T}, i::Integer) where T = convert(T, first(v) + (i - oneunit(i))*step_hp(v))

which should pretty much be the same sa [begin + n -1]
unless I'm missing the point completely?

Copy link
Contributor

@mcabbott mcabbott Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line nth(itr::AbstractRange, n) = getindex(itr, n) will for sure fail on the axes of an OffsetArray. (In fact, it will first be ambiguous, as n::Any is less specific.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was overthinking it, I'll just stick with [begin + n - 1]. Sorry.


_nth(::SizeUnknown, itr, n) = _fallback_nth(itr, n)
_nth(::Union{HasShape,HasLength}, itr, n) = _withlength_nth(itr, n, length(itr))
_nth(::IsInfinite, itr, n) = _inbounds_nth(itr, n)

_inbounds_nth(itr, n) = iterate(drop(itr, n - 1))[1]
_inbounds_nth(itr::AbstractArray, n) = itr[n]
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved

_withlength_nth(itr, n, N) = n > N ? nothing : _inbounds_nth(itr, n)

function _fallback_nth(itr, n)
y = iterate(drop(itr, n - 1))
y === nothing && return nothing
y[1]
end

# specialized versions to better interact with existing iterators
# Count
nth(itr::Count, n::Integer) = n > 0 ? itr.start + itr.step * (n - 1) : nothing

# Repeated
nth(itr::Repeated, n::Integer) = itr.x

# Take(Repeated)
nth(itr::Take{Repeated{O}}, n::Integer) where {O} = n > itr.n ? nothing : itr.xs.x

# infinite cycle
nth(itr::Cycle{I}, n::Integer) where {I} = _nth_inf_cycle(IteratorSize(I), itr, n)
_nth_inf_cycle(::IsInfinite, itr, n) = _inbounds_nth(itr.xs, n)
_nth_inf_cycle(::SizeUnknown, itr, n) = _fallback_nth(itr.xs, n)
_nth_inf_cycle(::Union{HasShape,HasLength}, itr, n) = _repeating_cycle_nth(itr.xs, n, length(itr.xs))

# finite cycle
# a finite cycle iterator is in reality a Flatten{Take{Repeated{O}}} iterator
nth(itr::Flatten{Take{Repeated{O}}}, n::Integer) where {O} = _nth_finite_cycle(IteratorSize(O), itr, n)
_nth_finite_cycle(::IsInfinite, itr, n) = _inbounds_nth(itr, n)
_nth_finite_cycle(::SizeUnknown, itr, n) = _fallback_nth(itr, n)
_nth_finite_cycle(::Union{HasShape,HasLength}, itr, n) = begin
N = itr.it.n # `Take` iterator n
torepeat = itr.it.xs.x # repeated object
K = length(torepeat)
n > K * N && return nothing
_repeating_cycle_nth(torepeat, n, K)
end


_repeating_cycle_nth(inner_itr, n, inner_N) = _inbounds_nth(inner_itr, 1 + ((n - 1) % inner_N))



end
24 changes: 24 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,30 @@ end
end
end

@testset "nth" begin
Z = Array{Int,0}(undef)
Z[] = 17
itrs = (collect(1:1000), 10:6:1000, "∀ϵ>0", (1, 3, 5, 10, 78), reshape(1:30, (5, 6)),
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved
Z, 3, true, 'x', 4 => 5, view(Z), view(reshape(1:30, (5, 6)), 2:4, 2:6),
(x^2 for x in 1:10), Iterators.Filter(isodd, 1:10), Iterators.flatten((1:10, 50:60)),
pairs(50:60), zip(1:10, 21:30, 51:60), Iterators.product(1:3, 10:12),
Iterators.repeated(3.14159, 5), (a=2, b=3, c=5, d=7, e=11), Iterators.cycle(collect(1:100)),
Iterators.cycle([1, 2, 3, 4, 5], 5))
ns = (
234, 123, 3, 2, 21, 1, 1, 1, 1, 1, 1, 10, 9, 3, 15, 7, 6, 3, 4, 4, 99999, 25
)
expected = (
234, 742, '>', 3, 21, 17, 3, true, 'x', 4, 17, 22, 81, 5, 54, (7 => 56), (6, 26, 56), (3, 10), 3.14159, 7, 99, 5
)
@test length(itrs) == length(ns) == length(expected)
testset = zip(itrs, ns, expected)
@testset "iter: $IT" for (IT, n, exp) in testset
@test exp == nth(IT, n)
IT isa Cycle && continue # cycles are infinite so never OOB
@test nth(IT, 999999999) === nothing
end
end

@testset "Iterators docstrings" begin
@test isempty(Docs.undocumented_names(Iterators))
end