ReverseCollection

struct ReverseCollection<Base : Collection where Base.Index : BidirectionalIndex>

A Collection that presents the elements of its Base collection in reverse order.

Note: This type is the result of x.reversed() where x is a collection having bidirectional indices.

The reversed() method is always lazy when applied to a collection with bidirectional indices, but does not implicitly confer laziness on algorithms applied to its result. In other words, for ordinary collections c having bidirectional indices:

  • c.reversed() does not create new storage
  • c.reversed().map(f) maps eagerly and returns a new array
  • c.lazy.reversed().map(f) maps lazily and returns a LazyMapCollection

See Also: ReverseRandomAccessCollection

Inheritance Collection, Indexable, Sequence View Protocol Hierarchy →
Associated Types
Index = ReverseIndex<Base.Index>

A type that represents a valid position in the collection.

Valid indices consist of the position of every element and a "past the end" position that's not valid for use as a subscript.

Iterator = IndexingIterator<ReverseCollection<Base>>

A type that provides the sequence's iteration interface and encapsulates its iteration state.

Element = Iterator.Element

Type alias inferred.

SubSequence = Slice<ReverseCollection<Base>>

Type alias inferred.

Import import Swift

Instance Variables

var count: ReverseCollection<Base>.Index.Distance

Returns the number of elements.

Complexity: O(1) if Index conforms to RandomAccessIndex; O(N) otherwise.

Declaration

var count: ReverseCollection<Base>.Index.Distance { get }

Declared In

Collection
var first: Iterator.Element?

Returns the first element of self, or nil if self is empty.

Complexity: O(1)

Declaration

var first: Iterator.Element? { get }

Declared In

Collection
var indices: Range<ReverseCollection<Base>.Index>

Returns the range of valid index values.

The result's endIndex is the same as that of self. Because Range is half-open, iterating the values of the result produces all valid subscript arguments for self, omitting its endIndex.

Declaration

var indices: Range<ReverseCollection<Base>.Index> { get }

Declared In

Collection
var isEmpty: Bool

Default implementations of core requirements Returns true iff self is empty.

Complexity: O(1)

Declaration

var isEmpty: Bool { get }

Declared In

Collection
var lazy: LazyCollection<ReverseCollection<Base>>

Augment self with lazy methods such as map, filter, etc. A collection with contents identical to self, but on which normally-eager operations such as map and filter are implemented lazily.

See Also: LazySequenceProtocol, LazyCollectionProtocol.

Declaration

var lazy: LazyCollection<ReverseCollection<Base>> { get }

Declared In

Collection
var underestimatedCount: Int

Returns a value less than or equal to the number of elements in self, nondestructively.

Complexity: O(count).

Declaration

var underestimatedCount: Int { get }

Declared In

Collection , Sequence

Subscripts

subscript(_: Range<ReverseCollection<Base>.Index>)

Supply the default "slicing" subscript for Collection models that accept the default associated SubSequence, Slice<Self>.

Declaration

subscript(bounds: Range<ReverseCollection<Base>.Index>) -> Slice<ReverseCollection<Base>> { get }

Declared In

Collection

Instance Methods

func contains(_:)

Returns true iff an element in self satisfies predicate.

Declaration

func contains(@noescape predicate: (Iterator.Element) throws -> Bool) rethrows -> Bool

Declared In

Collection, Sequence
func dropFirst()

Returns a subsequence containing all but the first element.

Complexity: O(1)

Declaration

func dropFirst() -> Slice<ReverseCollection<Base>>

Declared In

Collection, Sequence
func dropFirst(_:)

Returns a subsequence containing all but the first n elements.

Precondition: n >= 0 Complexity: O(n)

Declaration

func dropFirst(n: Int) -> Slice<ReverseCollection<Base>>

Declared In

Collection, Sequence
func dropLast()

Returns a subsequence containing all but the last element.

Precondition: self is a finite sequence. Precondition: n >= 0 Complexity: O(self.count)

Declaration

func dropLast() -> Slice<ReverseCollection<Base>>

Declared In

Collection, Sequence
func dropLast(_:)

Returns a subsequence containing all but the last n elements.

Precondition: n >= 0 Complexity: O(self.count)

Declaration

func dropLast(n: Int) -> Slice<ReverseCollection<Base>>

Declared In

Collection, Sequence
func elementsEqual(_:isEquivalent:)

Returns true iff self and other contain equivalent elements, using isEquivalent as the equivalence test.

Precondition: isEquivalent is an equivalence relation.

Declaration

func elementsEqual<OtherSequence : Sequence where OtherSequence.Iterator.Element == Iterator.Element>(other: OtherSequence, @noescape isEquivalent: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> Bool

Declared In

Collection, Sequence
func enumerated()

Returns a lazy Sequence containing pairs (n, x), where ns are consecutive Ints starting at zero, and xs are the elements of self:

> for (n, c) in "Swift".characters.enumerated() {
    print("\(n): '\(c)'")
  }
0: 'S'
1: 'w'
2: 'i'
3: 'f'
4: 't'

Declaration

func enumerated() -> EnumeratedSequence<ReverseCollection<Base>>

Declared In

Collection, Sequence
func filter(_:)

Returns an Array containing the elements of self, in order, that satisfy the predicate includeElement.

Declaration

func filter(@noescape includeElement: (Iterator.Element) throws -> Bool) rethrows -> [Iterator.Element]

Declared In

Collection, Sequence
func flatMap<T>(_: (Iterator.Element) throws -> T?)

Returns an Array containing the non-nil results of mapping transform over self.

Complexity: O(M + N), where M is the length of self and N is the length of the result.

Declaration

func flatMap<T>(@noescape transform: (Iterator.Element) throws -> T?) rethrows -> [T]

Declared In

Collection, Sequence
func flatMap<S : Sequence>(_: (Iterator.Element) throws -> S)

Returns an Array containing the concatenated results of mapping transform over self.

s.flatMap(transform)

is equivalent to

Array(s.map(transform).flatten())

Complexity: O(M + N), where M is the length of self and N is the length of the result.

Declaration

func flatMap<S : Sequence>(@noescape transform: (Iterator.Element) throws -> S) rethrows -> [S.Iterator.Element]

Declared In

Collection, Sequence
func forEach(_:)

Call body on each element in self in the same order as a for-in loop.

sequence.forEach {
  // body code
}

is similar to:

for element in sequence {
  // body code
}

Note: You cannot use the break or continue statement to exit the current call of the body closure or skip subsequent calls. Note: Using the return statement in the body closure will only exit from the current call to body, not any outer scope, and won't skip subsequent calls.

Complexity: O(self.count)

Declaration

func forEach(@noescape body: (Iterator.Element) throws -> Swift.Void) rethrows

Declared In

Collection, Sequence
func index( where:)

Returns the first index where predicate returns true for the corresponding value, or nil if such value is not found.

Complexity: O(self.count).

Declaration

func index(@noescape where predicate: (Iterator.Element) throws -> Bool) rethrows -> ReverseCollection<Base>.Index?

Declared In

Collection
func lexicographicallyPrecedes(_:isOrderedBefore:)

Returns true iff self precedes other in a lexicographical ("dictionary") ordering, using isOrderedBefore as the comparison between elements.

Note: This method implements the mathematical notion of lexicographical ordering, which has no connection to Unicode. If you are sorting strings to present to the end-user, you should use String APIs that perform localized comparison.

Precondition: isOrderedBefore is a strict weak ordering over the elements of self and other.

Declaration

func lexicographicallyPrecedes<OtherSequence : Sequence where OtherSequence.Iterator.Element == Iterator.Element>(other: OtherSequence, @noescape isOrderedBefore: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> Bool

Declared In

Collection, Sequence
func map(_:)

Returns an Array containing the results of mapping transform over self.

Complexity: O(N).

Declaration

func map<T>(@noescape transform: (Iterator.Element) throws -> T) rethrows -> [T]

Declared In

Collection, Sequence
func max( isOrderedBefore:)

Returns the maximum element in self or nil if the sequence is empty.

Complexity: O(elements.count).

Precondition: isOrderedBefore is a strict weak ordering over self.

Declaration

func max(@noescape isOrderedBefore isOrderedBefore: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> Iterator.Element?

Declared In

Collection, Sequence
func min( isOrderedBefore:)

Returns the minimum element in self or nil if the sequence is empty.

Complexity: O(elements.count).

Precondition: isOrderedBefore is a strict weak ordering over self.

Declaration

func min(@noescape isOrderedBefore isOrderedBefore: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> Iterator.Element?

Declared In

Collection, Sequence
func prefix(_:)

Returns a subsequence, up to maxLength in length, containing the initial elements.

If maxLength exceeds self.count, the result contains all the elements of self.

Precondition: maxLength >= 0 Complexity: O(maxLength)

Declaration

func prefix(maxLength: Int) -> Slice<ReverseCollection<Base>>

Declared In

Collection, Sequence
func prefix(through:)

Returns prefix(upTo: position.successor())

Complexity: O(1)

Declaration

func prefix(through position: ReverseCollection<Base>.Index) -> Slice<ReverseCollection<Base>>

Declared In

Collection
func prefix(upTo:)

Returns self[startIndex..<end]

Complexity: O(1)

Declaration

func prefix(upTo end: ReverseCollection<Base>.Index) -> Slice<ReverseCollection<Base>>

Declared In

Collection
func reduce(_:combine:)

Returns the result of repeatedly calling combine with an accumulated value initialized to initial and each element of self, in turn, i.e. return combine(combine(...combine(combine(initial, self[0]), self[1]),...self[count-2]), self[count-1]).

Declaration

func reduce<T>(initial: T, @noescape combine: (T, Iterator.Element) throws -> T) rethrows -> T

Declared In

Collection, Sequence
func reversed()

Returns an Array containing the elements of self in reverse order.

Complexity: O(N), where N is the length of self.

Declaration

func reversed() -> [Iterator.Element]

Declared In

Collection, Sequence
func sorted( isOrderedBefore:)

Returns an Array containing the sorted elements of source according to isOrderedBefore.

The sorting algorithm is not stable (can change the relative order of elements for which isOrderedBefore does not establish an order).

Precondition: isOrderedBefore is a strict weak ordering over the elements in self.

Declaration

func sorted(@noescape isOrderedBefore isOrderedBefore: (Iterator.Element, Iterator.Element) -> Bool) -> [Iterator.Element]

Declared In

Collection, Sequence
func split(maxSplits:omittingEmptySubsequences:isSeparator:)

Returns the maximal SubSequences of self, in order, that don't contain elements satisfying the predicate isSeparator.

maxSplits: The maximum number of SubSequences to return, minus 1. If maxSplits + 1 SubSequences are returned, the last one is a suffix of self containing all the elements of self following the last split point. The default value is Int.max.

omittingEmptySubsequences: If false, an empty SubSequence is produced in the result for each pair of consecutive elements satisfying isSeparator. The default value is true.

Precondition: maxSplits >= 0

Declaration

func split(maxSplits maxSplits: Int = default, omittingEmptySubsequences: Bool = default, @noescape isSeparator: (Iterator.Element) throws -> Bool) rethrows -> [Slice<ReverseCollection<Base>>]

Declared In

Collection, Sequence
func starts(with:isEquivalent:)

Returns true iff self begins with elements equivalent to those of other, using isEquivalent as the equivalence test. Returns true if other is empty.

Precondition: isEquivalent is an equivalence relation.

Declaration

func starts<PossiblePrefix : Sequence where PossiblePrefix.Iterator.Element == Iterator.Element>(with possiblePrefix: PossiblePrefix, @noescape isEquivalent: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> Bool

Declared In

Collection, Sequence
func suffix(_:)

Returns a slice, up to maxLength in length, containing the final elements of self.

If maxLength exceeds s.count, the result contains all the elements of self.

Precondition: maxLength >= 0 Complexity: O(self.count)

Declaration

func suffix(maxLength: Int) -> Slice<ReverseCollection<Base>>

Declared In

Collection, Sequence
func suffix(from:)

Returns self[start..<endIndex]

Complexity: O(1)

Declaration

func suffix(from start: ReverseCollection<Base>.Index) -> Slice<ReverseCollection<Base>>

Declared In

Collection

Conditionally Inherited Items

The initializers, methods, and properties listed below may be available on this type under certain conditions (such as methods that are available on Array when its elements are Equatable) or may not ever be available if that determination is beyond SwiftDoc.org's capabilities. Please open an issue on GitHub if you see something out of place!

Where Index : BidirectionalIndex

var last: Iterator.Element?

Declaration

var last: Iterator.Element? { get }

Declared In

Collection
func dropLast(_:)

Returns a subsequence containing all but the last n elements.

Precondition: n >= 0 Complexity: O(n)

Declaration

func dropLast(n: Int) -> Slice<ReverseCollection<Base>>

Declared In

Collection
func reversed()

Returns the elements of self in reverse order.

Complexity: O(1)

Declaration

func reversed() -> ReverseCollection<ReverseCollection<Base>>

Declared In

Collection
func suffix(_:)

Returns a slice, up to maxLength in length, containing the final elements of self.

If maxLength exceeds s.count, the result contains all the elements of self.

Precondition: maxLength >= 0 Complexity: O(maxLength)

Declaration

func suffix(maxLength: Int) -> Slice<ReverseCollection<Base>>

Declared In

Collection

Where Index : RandomAccessIndex

func reversed()

Returns the elements of self in reverse order.

Complexity: O(1)

Declaration

func reversed() -> ReverseRandomAccessCollection<ReverseCollection<Base>>

Declared In

Collection

Where Iterator == IndexingIterator

func makeIterator()

Supply the default makeIterator() method for Collection models that accept the default associated Iterator, IndexingIterator<Self>.

Declaration

func makeIterator() -> IndexingIterator<ReverseCollection<Base>>

Declared In

Collection

Where Iterator == Self, Self : IteratorProtocol

func makeIterator()

A default makeIterator() function for IteratorProtocol instances that are declared to conform to Sequence

Declaration

func makeIterator() -> ReverseCollection<Base>

Declared In

Collection, Sequence

Where Iterator.Element : Collection

func flatten()

A concatenation of the elements of self.

Declaration

func flatten() -> FlattenCollection<ReverseCollection<Base>>

Declared In

Collection

Where Iterator.Element : Collection, Index : BidirectionalIndex, Iterator.Element.Index : BidirectionalIndex

func flatten()

A concatenation of the elements of self.

Declaration

func flatten() -> FlattenBidirectionalCollection<ReverseCollection<Base>>

Declared In

Collection

Where Iterator.Element : Comparable

func lexicographicallyPrecedes(_:)

Returns true iff self precedes other in a lexicographical ("dictionary") ordering, using "<" as the comparison between elements.

Note: This method implements the mathematical notion of lexicographical ordering, which has no connection to Unicode. If you are sorting strings to present to the end-user, you should use String APIs that perform localized comparison.

Declaration

func lexicographicallyPrecedes<OtherSequence : Sequence where OtherSequence.Iterator.Element == Iterator.Element>(other: OtherSequence) -> Bool

Declared In

Collection, Sequence
func max()

Returns the maximum element in self or nil if the sequence is empty.

Complexity: O(elements.count).

Declaration

func max() -> Iterator.Element?

Declared In

Collection, Sequence
func min()

Returns the minimum element in self or nil if the sequence is empty.

Complexity: O(elements.count).

Declaration

func min() -> Iterator.Element?

Declared In

Collection, Sequence
func sorted()

Returns an Array containing the sorted elements of source.

The sorting algorithm is not stable (can change the relative order of elements that compare equal).

Precondition: The less-than operator (func <) defined in the Comparable conformance is a strict weak ordering over the elements in self.

Declaration

func sorted() -> [Iterator.Element]

Declared In

Collection, Sequence

Where Iterator.Element : Equatable

func contains(_:)

Returns true iff element is in self.

Declaration

func contains(element: Iterator.Element) -> Bool

Declared In

Collection, Sequence
func elementsEqual(_:)

Returns true iff self and other contain the same elements in the same order.

Declaration

func elementsEqual<OtherSequence : Sequence where OtherSequence.Iterator.Element == Iterator.Element>(other: OtherSequence) -> Bool

Declared In

Collection, Sequence
func index(of:)

Returns the first index where value appears in self or nil if value is not found.

Complexity: O(self.count).

Declaration

func index(of element: Iterator.Element) -> ReverseCollection<Base>.Index?

Declared In

Collection
func split(separator:maxSplits:omittingEmptySubsequences:)

Returns the maximal SubSequences of self, in order, around a separator element.

maxSplits: The maximum number of SubSequences to return, minus 1. If maxSplits + 1 SubSequences are returned, the last one is a suffix of self containing all the elements of self following the last split point. The default value is Int.max.

omittingEmptySubsequences: If false, an empty SubSequence is produced in the result for each pair of consecutive elements equal to separator. The default value is true.

Precondition: maxSplits >= 0

Declaration

func split(separator separator: Iterator.Element, maxSplits: Int = default, omittingEmptySubsequences: Bool = default) -> [Slice<ReverseCollection<Base>>]

Declared In

Collection, Sequence
func starts(with:)

Returns true iff the initial elements of self are equal to prefix. Returns true if other is empty.

Declaration

func starts<PossiblePrefix : Sequence where PossiblePrefix.Iterator.Element == Iterator.Element>(with possiblePrefix: PossiblePrefix) -> Bool

Declared In

Collection, Sequence

Where Iterator.Element : Sequence

func flatten()

A concatenation of the elements of self.

Declaration

func flatten() -> FlattenSequence<ReverseCollection<Base>>

Declared In

Collection, Sequence
func joined(separator:)

Returns a view, whose elements are the result of interposing a given separator between the elements of the sequence self.

For example, [[1, 2, 3], [4, 5, 6], [7, 8, 9]].joined(separator: [-1, -2]) yields [1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9].

Declaration

func joined<Separator : Sequence where Separator.Iterator.Element == Iterator.Element.Iterator.Element>(separator separator: Separator) -> JoinedSequence<ReverseCollection<Base>>

Declared In

Collection, Sequence

Where Iterator.Element == String

func joined(separator:)

Interpose the separator between elements of self, then concatenate the result. For example:

["foo", "bar", "baz"].joined(separator: "-|-") // "foo-|-bar-|-baz"

Declaration

func joined(separator separator: String) -> String

Declared In

Collection, Sequence