Skip to content

Commit a1700c0

Browse files
committed
allow range(start, stop; ...)
finishes #25896
1 parent f068f21 commit a1700c0

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

base/range.jl

+21-6
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,51 @@ function _colon(start::T, step, stop::T) where T
4646
end
4747

4848
"""
49-
range(start; length, stop, step=1)
49+
range(start[, stop]; length, stop, step=1)
5050
51-
Given a starting value, construct a range either by length or from `start` to `stop`,
52-
optionally with a given step (defaults to 1, a [`UnitRange`](@ref)).
53-
One of `length` or `stop` is required. If `length`, `stop`, and `step` are all specified, they must agree.
51+
Given a starting value, construct a range by specifying any two of `stop`,
52+
`length`, and `step`. If `length`, `stop`, and `step` are all specified, they must agree.
5453
5554
If `length` and `stop` are provided and `step` is not, the step size will be computed
5655
automatically such that there are `length` linearly spaced elements in the range (a [`LinRange`](@ref)).
5756
5857
If `step` and `stop` are provided and `length` is not, the overall range length will be computed
5958
automatically such that the elements are `step` spaced (a [`StepRange`](@ref)).
6059
60+
`stop` may be specified as either a positional or keyword argument.
61+
6162
# Examples
6263
```jldoctest
63-
julia> range(1, length=100)
64+
julia> range(1, step=1, length=100)
6465
1:100
6566
66-
julia> range(1, stop=100)
67+
julia> range(1, step=1, stop=100)
6768
1:100
6869
6970
julia> range(1, step=5, length=100)
7071
1:5:496
7172
7273
julia> range(1, step=5, stop=100)
7374
1:5:96
75+
76+
julia> range(1, 10, length=101)
77+
1.0:0.09:10.0
78+
79+
julia> range(1, 100, step=5)
80+
1:5:96
7481
```
7582
"""
7683
range(start; length::Union{Integer,Nothing}=nothing, stop=nothing, step=nothing) =
7784
_range(start, step, stop, length)
7885

86+
range(start, stop; length::Union{Integer,Nothing}=nothing, step=nothing) =
87+
_range2(start, step, stop, length)
88+
89+
_range2(start, ::Nothing, stop, ::Nothing) =
90+
throw(ArgumentError("At least one of `length` or `step` must be specified"))
91+
92+
_range2(start, step, stop, length) = _range(start, step, stop, length)
93+
7994
# Range from start to stop: range(a, [step=s,] stop=b), no length
8095
_range(start, step, stop, ::Nothing) = (:)(start, step, stop)
8196
_range(start, ::Nothing, stop, ::Nothing) = (:)(start, stop)

test/ranges.jl

+17
Original file line numberDiff line numberDiff line change
@@ -1418,3 +1418,20 @@ end
14181418
@test @allocated(0:286.493442:360) == 0
14191419
@test @allocated(0:286:360) == 0
14201420
end
1421+
1422+
@testset "range with start and stop" begin
1423+
for starts in [-1, 0, 1, 10]
1424+
for stops in [-2, 0, 2, 100]
1425+
for lengths in [2, 10, 100]
1426+
if stops >= starts
1427+
@test range(starts, stops, length=lengths) == range(starts, stop=stops, length=lengths)
1428+
end
1429+
end
1430+
for steps in [0.01, 1, 2]
1431+
@test range(starts, stops, step=steps) == range(starts, stop=stops, step=steps)
1432+
end
1433+
end
1434+
end
1435+
# require a keyword arg
1436+
@test_throws ArgumentError range(1, 100)
1437+
end

0 commit comments

Comments
 (0)