Description
Previous ID | SR-413 |
Radar | None |
Original Reporter | bnut (JIRA User) |
Type | Bug |
Status | Resolved |
Resolution | Done |
Additional Detail from JIRA
Votes | 0 |
Component/s | Standard Library |
Labels | Bug |
Assignee | None |
Priority | Medium |
md5: f1f0d117a5204e887ab4187dd39016b6
relates to:
- SR-4499 Iterating over elements from existential collections is super slow
Issue Description:
TL;DR
At the moment (0...1000000000).suffix(2)
is really efficient.
However, AnySequence(0...1000000000).suffix(2)
is not.
Long version
AnySequence implements underestimateCount and generate, however it does not implement any of the other methods on SequenceType.
This means that if you've provided custom implementations of these methods it still uses the implementation from SequenceType's protocol extension.
For example:
struct MySequence: SequenceType {
private static let array = [1,2,3]
func generate() -> AnyGenerator<Int> {
return anyGenerator(MySequence.array.generate())
}
func suffix(maxLength: Int) -> AnySequence<Int> {
return AnySequence(MySequence.array.suffix(maxLength).map{$0 + 1})
}
}
let a = MySequence()
let b = AnySequence(a)
print("\(Array(a.suffix(2))) == \(Array(b.suffix(2)))?")
Outputs:
[3, 4]==[2, 3]?
but it should output:
[3, 4]==[3, 4]?
This example is probably not something you would want to do, but it is representative of the problem. Other cases this may be an issue are when implementing infinite sequences, implementing suffix efficiently on a bidirectional indexed collection type, etc.