@@ -46,36 +46,51 @@ function _colon(start::T, step, stop::T) where T
46
46
end
47
47
48
48
"""
49
- range(start; length, stop, step=1)
49
+ range(start[, stop] ; length, stop, step=1)
50
50
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.
54
53
55
54
If `length` and `stop` are provided and `step` is not, the step size will be computed
56
55
automatically such that there are `length` linearly spaced elements in the range (a [`LinRange`](@ref)).
57
56
58
57
If `step` and `stop` are provided and `length` is not, the overall range length will be computed
59
58
automatically such that the elements are `step` spaced (a [`StepRange`](@ref)).
60
59
60
+ `stop` may be specified as either a positional or keyword argument.
61
+
61
62
# Examples
62
63
```jldoctest
63
- julia> range(1, length=100)
64
+ julia> range(1, step=1, length=100)
64
65
1:100
65
66
66
- julia> range(1, stop=100)
67
+ julia> range(1, step=1, stop=100)
67
68
1:100
68
69
69
70
julia> range(1, step=5, length=100)
70
71
1:5:496
71
72
72
73
julia> range(1, step=5, stop=100)
73
74
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
74
81
```
75
82
"""
76
83
range (start; length:: Union{Integer,Nothing} = nothing , stop= nothing , step= nothing ) =
77
84
_range (start, step, stop, length)
78
85
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
+
79
94
# Range from start to stop: range(a, [step=s,] stop=b), no length
80
95
_range (start, step, stop, :: Nothing ) = (:)(start, step, stop)
81
96
_range (start, :: Nothing , stop, :: Nothing ) = (:)(start, stop)
0 commit comments