Skip to content

Commit e210532

Browse files
author
Dave Abrahams
committed
Merge pull request #825 from kballard/collectiontype-lazy-first
[Stdlib] Optimize CollectionType.first
2 parents 15c9d2b + 6ae85ce commit e210532

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

stdlib/public/core/Collection.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,12 @@ extension CollectionType {
262262
///
263263
/// - Complexity: O(1)
264264
public var first: Generator.Element? {
265-
return isEmpty ? nil : self[startIndex]
265+
// NB: Accessing `startIndex` may not be O(1) for some lazy collections,
266+
// so instead of testing `isEmpty` and then returning the first element,
267+
// we'll just rely on the fact that the generator always yields the
268+
// first element first.
269+
var gen = generate()
270+
return gen.next()
266271
}
267272

268273
/// Returns a value less than or equal to the number of elements in

0 commit comments

Comments
 (0)