PartialRangeFrom

struct PartialRangeFrom

A partial interval extending upward from a lower bound.

Inheritance RangeExpression

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.

Initializers

init init(_:) Required

Declaration

@inlinable public init(_ lowerBound: Bound)

Instance Variables

let lowerBound Required

Declaration

let lowerBound: Bound

Instance Methods

func contains(_ element: Bound) -> Bool Required

Returns a Boolean value indicating whether the given element is contained within the range expression.

  • Parameter element: The element to check for containment.

Declaration

@inlinable public func contains(_ element: Bound) -> Bool
func relative(to collection: C) -> Range<Bound> Required

Returns the range of indices described by this range expression within the given collection.

You can use the relative(to:) method to convert a range expression, which could be missing one or both of its endpoints, into a concrete range that is bounded on both sides. The following example uses this method to convert a partial range up to 4 into a half-open range, using an array instance to add the range's lower bound.

let numbers = [10, 20, 30, 40, 50, 60, 70]
let upToFour = ..<4

let r1 = upToFour.relative(to: numbers)
// r1 == 0..<4

The r1 range is bounded on the lower end by 0 because that is the starting index of the numbers array. When the collection passed to relative(to:) starts with a different index, that index is used as the lower bound instead. The next example creates a slice of numbers starting at index 2, and then uses the slice with relative(to:) to convert upToFour to a concrete range.

let numbersSuffix = numbers[2...]
// numbersSuffix == [30, 40, 50, 60, 70]

let r2 = upToFour.relative(to: numbersSuffix)
// r2 == 2..<4

Use this method only if you need the concrete range it produces. To access a slice of a collection using a range expression, use the collection's generic subscript that uses a range expression as its parameter.

let numbersPrefix = numbers[upToFour]
// numbersPrefix == [10, 20, 30, 40]
  • Parameter collection: The collection to evaluate this range expression in relation to.

Declaration

public func relative<C>(to collection: C) -> Range<Bound> where Bound == C.Index, C: Collection

Type Methods

func ~=(pattern: Self, value: Self.Bound) -> Bool Required

Declaration

@inlinable public static func ~=(pattern: Self, value: Self.Bound) -> Bool