struct FilterCollectionView<Base : CollectionType>

A lazy CollectionType wrapper that includes the elements of an underlying collection that satisfy a predicate. Not automatically returned by filter(x) for two reasons:

  • The O(1) guarantee of our Index would be iffy at best, since it advances an underlying Index until the predicate is satisfied. Be aware that a FilterCollectionView may not offer the expected efficiency for this reason.

  • Constructing an Array from a CollectionType measures the length of the collection before traversing it to read the elements. This causes the filter predicate to be called twice for each element of the underlying collection, which is surprising.

Inheritance CollectionType, SequenceType, _CollectionType, _SequenceType, _Sequence_Type View Protocol Hierarchy →
Associated Types
Index = FilterCollectionViewIndex<Base>

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.

Generator = FilterGenerator<Base.Generator>

Type alias inferred.

Element = Base.Generator.Element

Type alias inferred.

Index = FilterCollectionViewIndex<Base>

Type alias inferred.

SubSequence = Slice<FilterCollectionView<Base>>

Type alias inferred.

Import import Swift



Construct an instance containing the elements of base that satisfy predicate.


init(_ base: Base, includeElement predicate: (Base.Generator.Element) -> Bool)

Instance Variables

var endIndex: FilterCollectionViewIndex<Base>

The collection's "past the end" position.

endIndex is not a valid argument to subscript, and is always reachable from startIndex by zero or more applications of successor().

Complexity: O(1)


var endIndex: FilterCollectionViewIndex<Base> { get }
var startIndex: FilterCollectionViewIndex<Base>

The position of the first element in a non-empty collection.

Identical to endIndex in an empty collection.

Complexity: O(N), where N is the ratio between unfiltered and filtered collection counts.


var startIndex: FilterCollectionViewIndex<Base> { get }


subscript(_: FilterCollectionViewIndex<Base>)


subscript(position: FilterCollectionViewIndex<Base>) -> Base.Generator.Element { get }

Instance Methods

func generate()

Return a generator over the elements of this sequence.

Complexity: O(1)


func generate() -> FilterGenerator<Base.Generator>