Generic Structure

PartialRangeFrom

A partial interval extending upward from a lower bound.

Declaration

@frozen struct PartialRangeFrom<Bound> where Bound : Comparable

Overview

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.

Topics

Type Aliases

typealias PartialRangeFrom.Element

A type representing the sequence’s elements.

Initializers

init(from: Decoder)

Creates a new instance by decoding from the given decoder.

Instance Properties

var lazy: LazySequence<PartialRangeFrom<Bound>>

A sequence containing the same elements as this sequence, but on which some operations, such as map and filter, are implemented lazily.

var underestimatedCount: Int

A value less than or equal to the number of elements in the sequence, calculated nondestructively.

Instance Methods

func allSatisfy((Bound) -> Bool) -> Bool

Returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.

func compactMap<ElementOfResult>((Bound) -> ElementOfResult?) -> [ElementOfResult]

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

func contains(Bound) -> Bool

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

func contains(where: (Bound) -> Bool) -> Bool

Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.

func drop(while: (Bound) -> Bool) -> DropWhileSequence<PartialRangeFrom<Bound>>

Returns a sequence by skipping the initial, consecutive elements that satisfy the given predicate.

func dropFirst(Int) -> DropFirstSequence<PartialRangeFrom<Bound>>

Returns a sequence containing all but the given number of initial elements.

func dropLast(Int) -> [Bound]

Returns a sequence containing all but the given number of final elements.

func elementsEqual<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.

func elementsEqual<OtherSequence>(OtherSequence, by: (Bound, OtherSequence.Element) -> Bool) -> Bool

Returns a Boolean value indicating whether this sequence and another sequence contain equivalent elements in the same order, using the given predicate as the equivalence test.

func encode(to: Encoder)

Encodes this value into the given encoder.

func enumerated() -> EnumeratedSequence<PartialRangeFrom<Bound>>

Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.

func filter((Bound) -> Bool) -> [Bound]

Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.

func first(where: (Bound) -> Bool) -> Bound?

Returns the first element of the sequence that satisfies the given predicate.

func flatMap<SegmentOfResult>((Bound) -> SegmentOfResult) -> [SegmentOfResult.Element]

Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.

func flatMap<ElementOfResult>((Bound) -> ElementOfResult?) -> [ElementOfResult]

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

Deprecated
func forEach((Bound) -> Void)

Calls the given closure on each element in the sequence in the same order as a for-in loop.

func joined() -> FlattenSequence<PartialRangeFrom<Bound>>

Returns the elements of this sequence of sequences, concatenated.

func joined(separator: String) -> String

Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.

func joined<Separator>(separator: Separator) -> JoinedSequence<PartialRangeFrom<Bound>>

Returns the concatenated elements of this sequence of sequences, inserting the given separator between each element.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence) -> Bool

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<) to compare elements.

func lexicographicallyPrecedes<OtherSequence>(OtherSequence, by: (Bound, Bound) -> Bool) -> Bool

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the given predicate to compare elements.

func makeIterator() -> PartialRangeFrom<Bound>.Iterator

Returns an iterator for this sequence.

func map<T>((Bound) -> T) -> [T]

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func max() -> Bound?

Returns the maximum element in the sequence.

func max(by: (Bound, Bound) -> Bool) -> Bound?

Returns the maximum element in the sequence, using the given predicate as the comparison between elements.

func min() -> Bound?

Returns the minimum element in the sequence.

func min(by: (Bound, Bound) -> Bool) -> Bound?

Returns the minimum element in the sequence, using the given predicate as the comparison between elements.

func prefix(Int) -> PrefixSequence<PartialRangeFrom<Bound>>

Returns a sequence, up to the specified maximum length, containing the initial elements of the sequence.

func prefix(while: (Bound) -> Bool) -> [Bound]

Returns a sequence containing the initial, consecutive elements that satisfy the given predicate.

func reduce<Result>(Result, (Result, Bound) -> Result) -> Result

Returns the result of combining the elements of the sequence using the given closure.

func reduce<Result>(into: Result, (inout Result, Bound) -> ()) -> Result

Returns the result of combining the elements of the sequence using the given closure.

func relative<C>(to: C) -> Range<Bound>

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

func reversed() -> [Bound]

Returns an array containing the elements of this sequence in reverse order.

func shuffled() -> [Bound]

Returns the elements of the sequence, shuffled.

func shuffled<T>(using: inout T) -> [Bound]

Returns the elements of the sequence, shuffled using the given generator as a source for randomness.

func sorted() -> [Bound]

Returns the elements of the sequence, sorted.

func sorted(by: (Bound, Bound) -> Bool) -> [Bound]

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (Bound) -> Bool) -> [ArraySlice<Bound>]

Returns the longest possible subsequences of the sequence, in order, that don’t contain elements satisfying the given predicate. Elements that are used to split the sequence are not returned as part of any subsequence.

func split(separator: Bound, maxSplits: Int, omittingEmptySubsequences: Bool) -> [ArraySlice<Bound>]

Returns the longest possible subsequences of the sequence, in order, around elements equal to the given element.

func starts<PossiblePrefix>(with: PossiblePrefix) -> Bool

Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.

func starts<PossiblePrefix>(with: PossiblePrefix, by: (Bound, PossiblePrefix.Element) -> Bool) -> Bool

Returns a Boolean value indicating whether the initial elements of the sequence are equivalent to the elements in another sequence, using the given predicate as the equivalence test.

func suffix(Int) -> [Bound]

Returns a subsequence, up to the given maximum length, containing the final elements of the sequence.

func withContiguousStorageIfAvailable<R>((UnsafeBufferPointer<Bound>) -> R) -> R?

Call body(p), where p is a pointer to the collection’s contiguous storage. If no such storage exists, it is first created. If the collection does not support an internal representation in a form of contiguous storage, body is not called and nil is returned.

Structures

struct PartialRangeFrom.Iterator

The iterator for a PartialRangeFrom instance.

Relationships

Conforms To

  • Decodable
  • Encodable
  • Sequence

See Also

Range Expressions

struct PartialRangeUpTo

A partial half-open interval up to, but not including, an upper bound.

struct PartialRangeThrough

A partial interval up to, and including, an upper bound.

protocol RangeExpression

A type that can be used to slice a collection.

enum UnboundedRange_

A range expression that represents the entire range of a collection.