InclusiveRange
An InclusiveRange<T: Integer>
value represents a range of numerical values between two integers, with the start and end numbers included in the range as suggested by the name.
A range value has a start
, end
, and a step
, which represents the interval at which the range's values are separated from start
to end
.
A range can be created using the InclusiveRange
constructor, which can take two or three arguments.
In the case where the range is constructed with two arguments, the first argument is the start
and the second is the end
. The step
is inferred to be either 1
(when end >= start
) or -1
(when end < start
). For example:
_10// start is 1, end is 100, step is 1_10let range: InclusiveRange<UInt> = InclusiveRange(1, 100)
Optionally a third, labeled, non-zero step
argument can be provided to specify a step other than 1
. For example, the following range contains all odd numbers from 1 to 100:
_10// start is 1, end is 100, step is 2_10let range: InclusiveRange<UInt> = InclusiveRange(1, 100, step: 2)
Note that in this example, even though the specified end of the range is 100, the last actual value the range can attain is 99.
If the specified step
argument would progress the range away from the end
, the creation will fail. For example:
_10// Throws an error because a step of -2 cannot progress from 1 to 100_10let range: InclusiveRange<Int> = InclusiveRange(1, 100, step: -2)
A range requires a type annotation when created.
InclusiveRange fields and functions
A value of type InclusiveRange<T>
, where T
is a number type, has the following fields and functions:
-
_10let start: T
The start of the range.
_10// Declare a range of `Int`s_10let range = let r: InclusiveRange<Int> = InclusiveRange(3, 9)_10_10// Get the start of the range_10let start = range.start_10_10// `start` is `3` -
_10let end: T
The end of the range.
_10// Declare a range of `Int`s_10let range: InclusiveRange<Int> = InclusiveRange(3, 9)_10_10// Get the end of the range_10let end = range.end_10_10// `end` is `9` -
_10let step: T
The step of the range.
_15// Declare a range of `Int`s with a `step` of 2_15let rangeWithStep: InclusiveRange<Int> = InclusiveRange(3, 9, step: 2)_15_15// Get the step of the range_15var step = range.step_15_15// `step` is `2`_15_15// Declare a range of `Int`s without an explicit `step`_15let rangeWithStep: InclusiveRange<Int> = InclusiveRange(3, 9)_15_15/ Get the step of the range_15step = rangeWithStep.step_15_15// `step` is implicitly `1` -
_10access(all)_10view fun contains(_ element: T): Bool
Returns
true
if the given integer is in theInclusiveRange
sequence, andfalse
otherwise.Specifically, for some
InclusiveRange
r
defined withstart
,step
, andend
,r.contains(x)
returns true if either:start <= end
and there exists some integeri >= 0
such thatstart + i * step = x
andx <= end
start > end
and there exists some integeri >= 0
such thatstart - i * step = x
andx >= end
_14// Declare a range of `Int`s with a `step` of 2_14let rangeWithStep: InclusiveRange<Int> = InclusiveRange(3, 9, step: 2)_14_14// `contains` is `true`_14var contains = range.contains(5)_14_14// `contains` is `true`_14var contains = range.contains(9)_14_14// `contains` is `false`_14contains = range.contains(6)_14_14// `contains` is `false`_14contains = range.contains(11)