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

Export oneto rather than implement range(stop) #39242

Closed
wants to merge 8 commits into from
Closed
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ export
minmax,
ndims,
ones,
oneto,
parent,
parentindices,
partialsort,
Expand Down
30 changes: 30 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down