Skip to content

replace Random.nth by Iterators.nth#61486

Merged
adienes merged 1 commit intoJuliaLang:masterfrom
matthias314:m3/nth
Apr 3, 2026
Merged

replace Random.nth by Iterators.nth#61486
adienes merged 1 commit intoJuliaLang:masterfrom
matthias314:m3/nth

Conversation

@matthias314
Copy link
Copy Markdown
Contributor

Iterators.nth has been added in #56580 for Julia 1.13. Currently Random has its own (non-public) nth function, which is used for AbstractDict and AbstractSet. This PR replaces it by Iterators.nth. Apart from removing redundant code, this also allows user-defined methods for Iterators.nth (which is public) to be used in rand.

Note that I call Iterators.nth with @inbounds. This doesn't make any difference for the default method (at least not for AbstractDict and AbstractSet), but it may do so for user-defined methods. I'm having SmallBitSet from my package SmallCollections.jl in mind, where nth is just some bit juggling, see matthias314/SmallCollections.jl#17. When filling a vector with random elements of a SmallBitSet, around 20% of the runtime would be spent on checking bounds. Of course, I can remove the @inbounds if you don't like it.

I've also dropped the ::eltype(iter) return type indicator of Random.nth. I've tested Set{T} where T is a Union with up to 6 elements, and also Generator. In all cases, @code_typed could predict the return type.

@adienes adienes merged commit b2e6508 into JuliaLang:master Apr 3, 2026
8 checks passed
@matthias314 matthias314 deleted the m3/nth branch April 3, 2026 19:35
IanButterworth added a commit to IanButterworth/julia that referenced this pull request Apr 7, 2026
Several functions and types introduced in Julia 1.13 were missing
`!!! compat "Julia 1.13"` annotations in their docstrings, and some were
absent from the API reference docs entirely.

Add compat notes to:
- `Iterators.nth` (base/iterators.jl)
- `@__FUNCTION__` (base/runtime_internals.jl)
- `Base.AbstractOneTo` (base/range.jl)
- `Base.setindex(::ImmutableDict, ...)` (base/dict.jl)
- `isassigned(::Tuple, ::Integer)` (base/tuple.jl; also adds docstring)
- `Base.active_manifest` (base/initdefs.jl)
- `macroexpand` `legacyscope` kwarg and `macroexpand!` (base/expr.jl)
- `AbstractSpinLock` and `PaddedSpinLock` (base/locks-mt.jl)
- Type annotation support in `gen_call_with_extracted_types`
  (stdlib/InteractiveUtils/src/macros.jl)

Add missing `@docs` entries to the API reference for:
- `Base.Iterators.nth` (doc/src/base/iterators.md)
- `Base.AbstractOneTo` (doc/src/base/collections.md)
- `Base.setindex(::ImmutableDict, ...)` (doc/src/base/collections.md)
- `Base.isassigned(::Tuple, ::Integer)` (doc/src/base/arrays.md)
- `Base.@acquire` (doc/src/base/parallel.md)
- `Base.ScopedValues.AbstractScopedValue` (doc/src/base/scopedvalues.md)
- `Base.ScopedValues.LazyScopedValue` (doc/src/base/scopedvalues.md)

Also declare `Base.setindex` as public in base/public.jl, which was
inadvertently omitted from the original PR (JuliaLang#59880), and fix
`nth` → `Iterators.nth` in HISTORY.md following the rename in JuliaLang#61486.

Co-Authored-By: Claude <claude@users.noreply.github.com>
IanButterworth added a commit to IanButterworth/julia that referenced this pull request Apr 7, 2026
Several functions and types introduced in Julia 1.13 were missing
`!!! compat "Julia 1.13"` annotations in their docstrings, and some were
absent from the API reference docs entirely.

Add compat notes to:
- `Iterators.nth` (base/iterators.jl)
- `@__FUNCTION__` (base/runtime_internals.jl)
- `Base.AbstractOneTo` (base/range.jl)
- `Base.setindex(::ImmutableDict, ...)` (base/dict.jl)
- `isassigned(::Tuple, ::Integer)` (base/tuple.jl; also adds docstring)
- `Base.active_manifest` (base/initdefs.jl)
- `macroexpand` `legacyscope` kwarg and `macroexpand!` (base/expr.jl)
- `AbstractSpinLock` and `PaddedSpinLock` (base/locks-mt.jl)
- Type annotation support in `gen_call_with_extracted_types`
  (stdlib/InteractiveUtils/src/macros.jl)

Add missing `@docs` entries to the API reference for:
- `Base.Iterators.nth` (doc/src/base/iterators.md)
- `Base.AbstractOneTo` (doc/src/base/collections.md)
- `Base.setindex(::ImmutableDict, ...)` (doc/src/base/collections.md)
- `Base.isassigned(::Tuple, ::Integer)` (doc/src/base/arrays.md)
- `Base.@acquire` (doc/src/base/parallel.md)
- `Base.ScopedValues.AbstractScopedValue` (doc/src/base/scopedvalues.md)
- `Base.ScopedValues.LazyScopedValue` (doc/src/base/scopedvalues.md)

Also declare `Base.setindex` as public in base/public.jl, which was
inadvertently omitted from the original PR (JuliaLang#59880), and fix
`nth` → `Iterators.nth` in HISTORY.md following the rename in JuliaLang#61486.

Co-Authored-By: Claude <claude@users.noreply.github.com>
IanButterworth added a commit to IanButterworth/julia that referenced this pull request Apr 7, 2026
Several functions and types introduced in Julia 1.13 were missing
`!!! compat "Julia 1.13"` annotations in their docstrings, and some were
absent from the API reference docs entirely.

Add compat notes to:
- `Iterators.nth` (base/iterators.jl)
- `@__FUNCTION__` (base/runtime_internals.jl)
- `Base.AbstractOneTo` (base/range.jl)
- `Base.setindex(::ImmutableDict, ...)` (base/dict.jl)
- `isassigned(::Tuple, ::Integer)` (base/tuple.jl; also adds docstring)
- `Base.active_manifest` (base/initdefs.jl)
- `macroexpand` `legacyscope` kwarg and `macroexpand!` (base/expr.jl)
- `AbstractSpinLock` and `PaddedSpinLock` (base/locks-mt.jl)
- Type annotation support in `gen_call_with_extracted_types`
  (stdlib/InteractiveUtils/src/macros.jl)

Add missing `@docs` entries to the API reference for:
- `Base.Iterators.nth` (doc/src/base/iterators.md)
- `Base.AbstractOneTo` (doc/src/base/collections.md)
- `Base.setindex(::ImmutableDict, ...)` (doc/src/base/collections.md)
- `Base.isassigned(::Tuple, ::Integer)` (doc/src/base/arrays.md)
- `Base.@acquire` (doc/src/base/parallel.md)
- `Base.ScopedValues.AbstractScopedValue` (doc/src/base/scopedvalues.md)
- `Base.ScopedValues.LazyScopedValue` (doc/src/base/scopedvalues.md)

Also declare `Base.setindex` as public in base/public.jl, which was
inadvertently omitted from the original PR (JuliaLang#59880), and fix
`nth` → `Iterators.nth` in HISTORY.md following the rename in JuliaLang#61486.

Co-Authored-By: Claude <claude@users.noreply.github.com>
IanButterworth added a commit to IanButterworth/julia that referenced this pull request Apr 7, 2026
Several functions and types introduced in Julia 1.13 were missing
`!!! compat "Julia 1.13"` annotations in their docstrings, and some were
absent from the API reference docs entirely.

Add compat notes to:
- `Iterators.nth` (base/iterators.jl)
- `@__FUNCTION__` (base/runtime_internals.jl)
- `Base.AbstractOneTo` (base/range.jl)
- `Base.setindex(::ImmutableDict, ...)` (base/dict.jl)
- `isassigned(::Tuple, ::Integer)` (base/tuple.jl; also adds docstring)
- `Base.active_manifest` (base/initdefs.jl)
- `macroexpand` `legacyscope` kwarg and `macroexpand!` (base/expr.jl)
- `AbstractSpinLock` and `PaddedSpinLock` (base/locks-mt.jl)
- Type annotation support in `gen_call_with_extracted_types`
  (stdlib/InteractiveUtils/src/macros.jl)

Add missing `@docs` entries to the API reference for:
- `Base.Iterators.nth` (doc/src/base/iterators.md)
- `Base.AbstractOneTo` (doc/src/base/collections.md)
- `Base.setindex(::ImmutableDict, ...)` (doc/src/base/collections.md)
- `Base.isassigned(::Tuple, ::Integer)` (doc/src/base/arrays.md)
- `Base.@acquire` (doc/src/base/parallel.md)
- `Base.ScopedValues.AbstractScopedValue` (doc/src/base/scopedvalues.md)
- `Base.ScopedValues.LazyScopedValue` (doc/src/base/scopedvalues.md)

Also declare `Base.setindex` as public in base/public.jl, which was
inadvertently omitted from the original PR (JuliaLang#59880), and fix
`nth` → `Iterators.nth` in HISTORY.md following the rename in JuliaLang#61486.

Co-Authored-By: Claude <claude@users.noreply.github.com>
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 this pull request may close these issues.

3 participants