PartialRangeFrom
A partial interval extending upward from a lower bound.
@frozen struct PartialRangeFrom<Bound> where Bound : Comparable
You create PartialRangeFrom
instances by using the postfix range operator (postfix ...
).
let atLeastFive = 5...
You can use a partial range to quickly check if a value is contained in a particular range of values. For example:
atLeastFive.contains(4)
// false
atLeastFive.contains(5)
// true
atLeastFive.contains(6)
// true
You can use a partial range of a collection’s indices to represent the range from the partial range’s lower bound up to the end of the collection.
let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[3...])
// Prints "[40, 50, 60, 70]"
Using a Partial Range as a Sequence
When a partial range uses integers as its lower and upper bounds, or any other type that conforms to the Strideable
protocol with an integer stride, you can use that range in a for
-in
loop or with any sequence method that doesn’t require that the sequence is finite. The elements of a partial range are the consecutive values from its lower bound continuing upward indefinitely.
func isTheMagicNumber(_ x: Int) -> Bool {
return x == 3
}
for x in 1... {
if isTheMagicNumber(x) {
print("\(x) is the magic number!")
break
} else {
print("\(x) wasn't it...")
}
}
// "1 wasn't it..."
// "2 wasn't it..."
// "3 is the magic number!"
Because a PartialRangeFrom
sequence counts upward indefinitely, do not use one with methods that read the entire sequence before returning, such as map(_:)
, filter(_:)
, or suffix(_:)
. It is safe to use operations that put an upper limit on the number of elements they access, such as prefix(_:)
or dropFirst(_:)
, and operations that you can guarantee will terminate, such as passing a closure you know will eventually return true
to first(where:)
.
In the following example, the asciiTable
sequence is made by zipping together the characters in the alphabet
string with a partial range starting at 65, the ASCII value of the capital letter A. Iterating over two zipped sequences continues only as long as the shorter of the two sequences, so the iteration stops at the end of alphabet
.
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let asciiTable = zip(65..., alphabet)
for (code, letter) in asciiTable {
print(code, letter)
}
// "65 A"
// "66 B"
// "67 C"
// ...
// "89 Y"
// "90 Z"
The behavior of incrementing indefinitely is determined by the type of Bound
. For example, iterating over an instance of PartialRangeFrom<Int>
traps when the sequence’s next value would be above Int.max
.
Citizens in Swift
Members
Citizens in Swift
where Bound:Comparable, Bound:Encodable
Conformances
protocol Encodable
A type that can encode itself to an external representation.
Members
Citizens in Swift
where Bound:Comparable
Conformances
protocol RangeExpression
A type that can be used to slice a collection.
Members
Features
static func ~= (Self, Self
.Bound) -> Bool Returns a Boolean value indicating whether a value is included in a range.
Citizens in Swift
where Bound:Strideable, Bound.Stride:SignedInteger
Conformances
protocol Sequence
A type that provides sequential, iterated access to its elements.
Members
func makeIterator(
) -> PartialRangeFrom<Bound>.Iterator Returns an iterator for this sequence.
struct Iterator
The iterator for a
PartialRangeFrom
instance.typealias Element
Citizens in Swift
where Bound:Comparable, Bound:Sendable
Conformances
protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
Citizens in Swift
where Bound:Comparable, Bound:Decodable
Conformances
protocol Decodable
A type that can decode itself from an external representation.