@@ -56,8 +56,10 @@ Construct a specialized array with evenly spaced elements and optimized storage
56
56
Mathematically a range is uniquely determined by any three of `start`, `step`, `stop` and `length`.
57
57
Valid invocations of range are:
58
58
* Call `range` with any three of `start`, `step`, `stop`, `length`.
59
- * Call `range` with two of `start`, `stop`, `length`. In this case `step` will be assumed
60
- to be one. If both arguments are Integers, a [`UnitRange`](@ref) will be returned.
59
+ * Call `range` with two of `start`, `stop`, `length`. In this case `step` will be assumed to be one.
60
+ * Call `range` with one of `stop` or `length`. `start` and `step` will be assumed to be one.
61
+
62
+ See Extended Help for additional details on the returned type.
61
63
62
64
# Examples
63
65
```jldoctest
@@ -87,6 +89,15 @@ julia> range(stop=10, step=1, length=5)
87
89
88
90
julia> range(start=1, step=1, stop=10)
89
91
1:1:10
92
+
93
+ julia> range(; length = 10)
94
+ Base.OneTo(10)
95
+
96
+ julia> range(; stop = 6)
97
+ Base.OneTo(6)
98
+
99
+ julia> range(; stop = 6.5)
100
+ 1.0:1.0:6.0
90
101
```
91
102
If `length` is not specified and `stop - start` is not an integer multiple of `step`, a range that ends before `stop` will be produced.
92
103
```jldoctest
@@ -101,8 +112,21 @@ To avoid this induced overhead, see the [`LinRange`](@ref) constructor.
101
112
`stop` as a positional argument requires at least Julia 1.1.
102
113
103
114
!!! compat "Julia 1.7"
104
- The versions without keyword arguments and `start` as a keyword argument
105
- require at least Julia 1.7.
115
+ The versions without keyword arguments, `start` as a keyword argument,
116
+ `stop` as a sole keyword argument, or length as a sole keyword argument
117
+ requires at least Julia 1.7.
118
+
119
+ # Extended Help
120
+
121
+ `range` will produce a `Base.OneTo` when the arguments are Integers and
122
+ * Only `length` is provided
123
+ * Only `stop` is provided
124
+
125
+ `range` will produce a `UnitRange` when the arguments are Integers and
126
+ * Only `start` and `stop` are provided
127
+ * Only `length` and `stop` are provided
128
+
129
+ A `UnitRange` is not produced if `step` is provided even if specified as one.
106
130
"""
107
131
function range end
108
132
@@ -115,8 +139,8 @@ range(;start=nothing, stop=nothing, length::Union{Integer, Nothing}=nothing, ste
115
139
_range (start, step, stop, length)
116
140
117
141
_range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Nothing ) = range_error (start, step, stop, len)
118
- _range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Any ) = range_error (start, step, stop, len)
119
- _range (start:: Nothing , step:: Nothing , stop:: Any , len:: Nothing ) = range_error (start, step, stop, len )
142
+ _range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Any ) = range_length ( len)
143
+ _range (start:: Nothing , step:: Nothing , stop:: Any , len:: Nothing ) = range_stop ( stop)
120
144
_range (start:: Nothing , step:: Nothing , stop:: Any , len:: Any ) = range_stop_length (stop, len)
121
145
_range (start:: Nothing , step:: Any , stop:: Nothing , len:: Nothing ) = range_error (start, step, stop, len)
122
146
_range (start:: Nothing , step:: Any , stop:: Nothing , len:: Any ) = range_error (start, step, stop, len)
@@ -131,6 +155,14 @@ _range(start::Any , step::Any , stop::Nothing, len::Any ) = range_start
131
155
_range (start:: Any , step:: Any , stop:: Any , len:: Nothing ) = range_start_step_stop (start, step, stop)
132
156
_range (start:: Any , step:: Any , stop:: Any , len:: Any ) = range_error (start, step, stop, len)
133
157
158
+ # Length as the only argument
159
+ range_length (len:: Integer ) = OneTo (len)
160
+
161
+ # Stop as the only argument
162
+ range_stop (stop) = range_start_stop (oneunit (stop), stop)
163
+ range_stop (stop:: Integer ) = range_length (stop)
164
+
165
+ # Stop and length as the only argument
134
166
range_stop_length (a:: Real , len:: Integer ) = UnitRange {typeof(a)} (oftype (a, a- len+ 1 ), a)
135
167
range_stop_length (a:: AbstractFloat , len:: Integer ) = range_step_stop_length (oftype (a, 1 ), a, len)
136
168
range_stop_length (a, len:: Integer ) = range_step_stop_length (oftype (a- a, 1 ), a, len)
0 commit comments