@@ -55,8 +55,10 @@ Construct a specialized array with evenly spaced elements and optimized storage
55
55
Mathematically a range is uniquely determined by any three of `start`, `step`, `stop` and `length`.
56
56
Valid invocations of range are:
57
57
* Call `range` with any three of `start`, `step`, `stop`, `length`.
58
- * Call `range` with two of `start`, `stop`, `length`. In this case `step` will be assumed
59
- to be one. If both arguments are Integers, a [`UnitRange`](@ref) will be returned.
58
+ * Call `range` with two of `start`, `stop`, `length`. In this case `step` will be assumed to be one.
59
+ * Call `range` with one of `stop` or `length`. `start` and `step` will be assumed to be one.
60
+
61
+ See Extended Help for additional details on the returned type.
60
62
61
63
# Examples
62
64
```jldoctest
@@ -86,6 +88,15 @@ julia> range(stop=10, step=1, length=5)
86
88
87
89
julia> range(start=1, step=1, stop=10)
88
90
1:1:10
91
+
92
+ julia> range(; length = 10)
93
+ Base.OneTo(10)
94
+
95
+ julia> range(; stop = 6)
96
+ Base.OneTo(6)
97
+
98
+ julia> range(; stop = 6.5)
99
+ 1.0:1.0:6.0
89
100
```
90
101
If `length` is not specified and `stop - start` is not an integer multiple of `step`, a range that ends before `stop` will be produced.
91
102
```jldoctest
@@ -103,7 +114,20 @@ If both are specified as positional arguments, one of `step` or `length` must al
103
114
`stop` as a positional argument requires at least Julia 1.1.
104
115
105
116
!!! compat "Julia 1.7"
106
- `start` as a keyword argument requires at least Julia 1.7.
117
+ `start` as a keyword argument, or
118
+ `stop` or `length` as a sole argument requires at least Julia 1.7.
119
+
120
+ # Extended Help
121
+
122
+ `range` will produce a `Base.OneTo` when the arguments are Integers and
123
+ * Only `length` is provided
124
+ * Only `stop` is provided
125
+
126
+ `range` will produce a `UnitRange` when the arguments are Integers and
127
+ * Only `start` and `stop` are provided
128
+ * Only `length` and `stop` are provided
129
+
130
+ A `UnitRange` is not produced if `step` is provided even if specified as one.
107
131
"""
108
132
function range end
109
133
@@ -129,8 +153,8 @@ range(;start=nothing, stop=nothing, length::Union{Integer, Nothing}=nothing, ste
129
153
_range (start, step, stop, length)
130
154
131
155
_range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Nothing ) = range_error (start, step, stop, len)
132
- _range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Any ) = range_error (start, step, stop, len)
133
- _range (start:: Nothing , step:: Nothing , stop:: Any , len:: Nothing ) = range_error (start, step, stop, len )
156
+ _range (start:: Nothing , step:: Nothing , stop:: Nothing , len:: Any ) = range_length ( len)
157
+ _range (start:: Nothing , step:: Nothing , stop:: Any , len:: Nothing ) = range_stop ( stop)
134
158
_range (start:: Nothing , step:: Nothing , stop:: Any , len:: Any ) = range_stop_length (stop, len)
135
159
_range (start:: Nothing , step:: Any , stop:: Nothing , len:: Nothing ) = range_error (start, step, stop, len)
136
160
_range (start:: Nothing , step:: Any , stop:: Nothing , len:: Any ) = range_error (start, step, stop, len)
@@ -145,6 +169,11 @@ _range(start::Any , step::Any , stop::Nothing, len::Any ) = range_start
145
169
_range (start:: Any , step:: Any , stop:: Any , len:: Nothing ) = range_start_step_stop (start, step, stop)
146
170
_range (start:: Any , step:: Any , stop:: Any , len:: Any ) = range_error (start, step, stop, len)
147
171
172
+ range_length (len:: Integer ) = OneTo (len)
173
+
174
+ range_stop (stop) = oneunit (stop): stop
175
+ range_stop (stop:: Integer ) = OneTo (stop)
176
+
148
177
range_stop_length (stop, length) = (stop- length+ 1 ): stop
149
178
150
179
range_step_stop_length (step, stop, length) = reverse (range_start_step_length (stop, - step, length))
0 commit comments