|
68 | 68 | return lhs._pos == rhs._pos
|
69 | 69 | }
|
70 | 70 |
|
71 |
| -/// The lazy `Collection` returned by `filter(c)` where `c` is a |
72 |
| -/// `Collection` |
| 71 | +/// A lazy `Collection` wrapper that includes the elements of an |
| 72 | +/// underlying collection that satisfy a predicate. Not |
| 73 | +/// automatically returned by `filter(x)` for two reasons: |
| 74 | +/// |
| 75 | +/// * The O(1) guarantee of our `IndexType` would be iffy at best, since |
| 76 | +/// it advances an underlying `Index` until the predicate is |
| 77 | +/// satisfied. Be aware that a `FilterCollectionView` may not offer |
| 78 | +/// the expected efficiency for this reason. |
| 79 | +/// |
| 80 | +/// * Constructing an `Array` from a `Collection` measures the length |
| 81 | +/// of the collection before traversing it to read the elements. |
| 82 | +/// This causes the filter predicate to be called twice for each |
| 83 | +/// element of the underlying collection, which is surprising. |
73 | 84 | @public struct FilterCollectionView<Base: Collection> : Collection {
|
74 | 85 |
|
75 | 86 | @public typealias IndexType = FilterCollectionViewIndex<Base>
|
| 87 | + |
| 88 | + @public |
| 89 | + init(_ base: Base, includeElement: (Base.GeneratorType.Element)->Bool) { |
| 90 | + self._base = base |
| 91 | + self._include = includeElement |
| 92 | + } |
| 93 | + |
76 | 94 | @public var startIndex: IndexType {
|
77 | 95 | var first = _base.startIndex
|
78 | 96 | while first != _base.endIndex {
|
|
111 | 129 | return FilterSequenceView(_base: source, _include: includeElement)
|
112 | 130 | }
|
113 | 131 |
|
| 132 | +/* |
| 133 | +// No overload for collections; see the comment on |
| 134 | +// FilterCollectionView for an explanation. |
| 135 | + |
114 | 136 | /// Return a lazy Collection containing the elements `x` of `source` for
|
115 | 137 | /// which `includeElement(x)` is `true`
|
116 | 138 | @public func filter<C:Collection>(
|
117 | 139 | source: C, includeElement: (C.GeneratorType.Element)->Bool
|
118 | 140 | ) -> FilterCollectionView<C> {
|
119 | 141 | return FilterCollectionView(_base: source, _include: includeElement)
|
120 | 142 | }
|
| 143 | +*/ |
0 commit comments