From f69895cf9b13fe31b4cfad79f9e515b12df6eec2 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Wed, 13 Jan 2021 20:58:52 -0500 Subject: [PATCH 1/7] Export oneto --- base/exports.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/exports.jl b/base/exports.jl index 440b28fb155b2..3ee12d1cb4232 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -405,6 +405,7 @@ export minmax, ndims, ones, + oneto, parent, parentindices, partialsort, From 891b87c055a99657f4316ca1f8971e5829a7128b Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Wed, 13 Jan 2021 21:33:23 -0500 Subject: [PATCH 2/7] oneto: Document and test --- NEWS.md | 1 + base/range.jl | 30 ++++++++++++++++++++++++++++++ test/ranges.jl | 4 ++++ 3 files changed, 35 insertions(+) diff --git a/NEWS.md b/NEWS.md index 61f3eb4fb8867..d1caa03d3b31d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -41,6 +41,7 @@ Standard library changes * `count` and `findall` now accept an `AbstractChar` argument to search for a character in a string ([#38675]). * `range` now supports `start` as an optional keyword argument ([#38041]). +* `oneto` is exported and allows access to `Base.OneTo`, an optimized version of `1:n` where `n` is an `Integer` ([#39242]). * `islowercase` and `isuppercase` are now compliant with the Unicode lower/uppercase categories ([#38574]). * `iseven` and `isodd` functions now support non-`Integer` numeric types ([#38976]). * `escape_string` can now receive a collection of characters in the keyword diff --git a/base/range.jl b/base/range.jl index 6e5a86f9c863f..3e12eeb38e575 100644 --- a/base/range.jl +++ b/base/range.jl @@ -415,6 +415,36 @@ struct OneTo{T<:Integer} <: AbstractUnitRange{T} end OneTo(stop::T) where {T<:Integer} = OneTo{T}(stop) OneTo(r::AbstractRange{T}) where {T<:Integer} = OneTo{T}(r) + +""" + oneto(n) + +Create an `AbstractRange` that behaves like to `1:n`. The returned +range may be more efficient than using `1:n` since the lower limit +is guaranteed to be one by the type system. The definition in `Base` +requires that `n` be an `Integer`. + +See also [`Base.OneTo`](@ref). + +# Examples +```jldoctest +julia> oneto(5) +Base.OneTo(5) + +julia> collect( oneto(6) ) +6-element Array{Int64,1}: + 1 + 2 + 3 + 4 + 5 + 6 + +julia> oneto(5.5) +ERROR: MethodError: no method matching Base.OneTo(::Float64) +... +```` +""" oneto(r) = OneTo(r) ## Step ranges parameterized by length diff --git a/test/ranges.jl b/test/ranges.jl index b9f77f211193d..17589c591519c 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -863,6 +863,7 @@ end @test 1:10 != 2:10 != 2:11 != Base.OneTo(11) @test Base.OneTo(10) != Base.OneTo(11) != 1:10 @test Base.OneTo(0) == 5:4 + @test oneto(5) == 1:5 end # issue #2959 @test 1.0:1.5 == 1.0:1.0:1.5 == 1.0:1.0 @@ -1271,6 +1272,7 @@ end @test isempty(r) @test length(r) == 0 @test size(r) == (0,) + @test r === oneto(-5) end let r = Base.OneTo(3) @test !isempty(r) @@ -1290,6 +1292,7 @@ end @test broadcast(+, r, 1) === 2:4 @test 2*r === 2:2:6 @test r + r === 2:2:6 + @test r === oneto(3) k = 0 for i in r @test i == (k += 1) @@ -1317,6 +1320,7 @@ end @test Base.OneTo{Int32}(1:2) === Base.OneTo{Int32}(2) @test Base.OneTo(Int32(1):Int32(2)) === Base.OneTo{Int32}(2) @test Base.OneTo{Int16}(3.0) === Base.OneTo{Int16}(3) + @test oneto(9) === Base.OneTo{Int}(9) @test_throws InexactError(:Int16, Int16, 3.2) Base.OneTo{Int16}(3.2) end From 63d3b266e44f1f8114ecb2dbcb3e1e3b87327ec2 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Thu, 14 Jan 2021 00:35:48 -0500 Subject: [PATCH 3/7] oneto: fix typo in doc --- base/range.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 3e12eeb38e575..bf4be2168a6fd 100644 --- a/base/range.jl +++ b/base/range.jl @@ -443,7 +443,7 @@ julia> collect( oneto(6) ) julia> oneto(5.5) ERROR: MethodError: no method matching Base.OneTo(::Float64) ... -```` +``` """ oneto(r) = OneTo(r) From b2fda74aa6a824dffc961b7bacba8e3556da610e Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Thu, 14 Jan 2021 18:38:33 -0500 Subject: [PATCH 4/7] Change one to 1 for oneto documentation Co-authored-by: Daniel Karrasch --- base/range.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index bf4be2168a6fd..b40f4960648f6 100644 --- a/base/range.jl +++ b/base/range.jl @@ -421,7 +421,7 @@ OneTo(r::AbstractRange{T}) where {T<:Integer} = OneTo{T}(r) Create an `AbstractRange` that behaves like to `1:n`. The returned range may be more efficient than using `1:n` since the lower limit -is guaranteed to be one by the type system. The definition in `Base` +is guaranteed to be 1 by the type system. The definition in `Base` requires that `n` be an `Integer`. See also [`Base.OneTo`](@ref). From aaa7b08b8517c3b544c7229e704140925e8ad0e2 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Thu, 14 Jan 2021 19:29:04 -0500 Subject: [PATCH 5/7] oneto: Simplify docs and NEWS, removes OneTo refs --- NEWS.md | 2 +- base/range.jl | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index d1caa03d3b31d..271766bc50931 100644 --- a/NEWS.md +++ b/NEWS.md @@ -41,7 +41,7 @@ Standard library changes * `count` and `findall` now accept an `AbstractChar` argument to search for a character in a string ([#38675]). * `range` now supports `start` as an optional keyword argument ([#38041]). -* `oneto` is exported and allows access to `Base.OneTo`, an optimized version of `1:n` where `n` is an `Integer` ([#39242]). +* `oneto` is exported and returns the equivalen to `1:n` where `n` is an `Integer` ([#39242]). * `islowercase` and `isuppercase` are now compliant with the Unicode lower/uppercase categories ([#38574]). * `iseven` and `isodd` functions now support non-`Integer` numeric types ([#38976]). * `escape_string` can now receive a collection of characters in the keyword diff --git a/base/range.jl b/base/range.jl index bf4be2168a6fd..160ca4cc2331e 100644 --- a/base/range.jl +++ b/base/range.jl @@ -417,14 +417,11 @@ OneTo(stop::T) where {T<:Integer} = OneTo{T}(stop) OneTo(r::AbstractRange{T}) where {T<:Integer} = OneTo{T}(r) """ - oneto(n) + oneto(n::Integer) -Create an `AbstractRange` that behaves like to `1:n`. The returned -range may be more efficient than using `1:n` since the lower limit -is guaranteed to be one by the type system. The definition in `Base` -requires that `n` be an `Integer`. - -See also [`Base.OneTo`](@ref). +Create an [`AbstractRange`](@ref) that behaves like to `1:n` +where `n` must be an Integer. The returned `AbstractRange` +may be more efficient than `1:n`. See also [`range`](@ref). # Examples ```jldoctest @@ -440,9 +437,11 @@ julia> collect( oneto(6) ) 5 6 +julia> oneto(3) == range(1; stop = 3) +true + julia> oneto(5.5) ERROR: MethodError: no method matching Base.OneTo(::Float64) -... ``` """ oneto(r) = OneTo(r) From 4d75866fa6b3e22874185509d86da84d09b0fe8d Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Thu, 14 Jan 2021 19:30:11 -0500 Subject: [PATCH 6/7] oneto: Add crossref to : and range --- base/range.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 160ca4cc2331e..b410f68b5ec64 100644 --- a/base/range.jl +++ b/base/range.jl @@ -421,7 +421,8 @@ OneTo(r::AbstractRange{T}) where {T<:Integer} = OneTo{T}(r) Create an [`AbstractRange`](@ref) that behaves like to `1:n` where `n` must be an Integer. The returned `AbstractRange` -may be more efficient than `1:n`. See also [`range`](@ref). +may be more efficient than `1:n`. See also [`:`](@ref) and +[`range`](@ref). # Examples ```jldoctest From 55b21d40d359197c188f47b9e8a1931f2bddb61a Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Fri, 15 Jan 2021 01:41:40 -0500 Subject: [PATCH 7/7] oneto: Fix doc test for Julia 1.7 --- base/range.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index b410f68b5ec64..5d95728a68177 100644 --- a/base/range.jl +++ b/base/range.jl @@ -430,7 +430,7 @@ julia> oneto(5) Base.OneTo(5) julia> collect( oneto(6) ) -6-element Array{Int64,1}: +6-element Vector{Int64}: 1 2 3