Skip to content

Commit 05ba419

Browse files
committed
allow range(start, stop; ...)
finishes #25896
1 parent 08bf3e1 commit 05ba419

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

base/range.jl

+28
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ julia> range(1, step=5, stop=100)
7676
range(start; length::Union{Integer,Nothing}=nothing, stop=nothing, step=nothing) =
7777
_range(start, step, stop, length)
7878

79+
"""
80+
range(start, stop; length, step=1)
81+
82+
Construct a range from start and stop values,
83+
optionally with a given step (defaults to 1, a [`UnitRange`](@ref)).
84+
If `length` and `step` are both specified, they must agree.
85+
86+
If `length` is provided and `step` is not, the step size will be computed
87+
automatically such that there are `length` linearly spaced elements in the range (a [`LinRange`](@ref)).
88+
89+
If `step` is provided and `length` is not, the overall range length will be computed
90+
automatically such that the elements are `step` spaced (a [`StepRange`](@ref)).
91+
92+
# Examples
93+
```jldoctest
94+
julia> range(1, 100)
95+
1:100
96+
97+
julia> range(1, 10, length=101)
98+
1.0:0.09:10.0
99+
100+
julia> range(1, 100, step=5)
101+
1:5:96
102+
```
103+
"""
104+
range(start, stop; length::Union{Integer,Nothing}=nothing, step=nothing) =
105+
_range(start, step, stop, length)
106+
79107
# Range from start to stop: range(a, [step=s,] stop=b), no length
80108
_range(start, step, stop, ::Nothing) = (:)(start, step, stop)
81109
_range(start, ::Nothing, stop, ::Nothing) = (:)(start, stop)

test/ranges.jl

+15
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,18 @@ end # module NonStandardIntegerRangeTest
13711371
end
13721372
end
13731373
end
1374+
1375+
@testset "range with start and stop" begin
1376+
for starts in [-1, 0, 1, 10]
1377+
for stops in [-2, 0, 2, 100]
1378+
for lengths in [2, 10, 100]
1379+
if stops >= starts
1380+
@test range(starts, stops, length=lengths) == range(starts, stop=stops, length=lengths)
1381+
end
1382+
end
1383+
for steps in [0.01, 1, 2]
1384+
@test range(starts, stops, step=steps) == range(starts, stop=stops, step=steps)
1385+
end
1386+
end
1387+
end
1388+
end

0 commit comments

Comments
 (0)