We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 15c9d2b + 6ae85ce commit e210532Copy full SHA for e210532
stdlib/public/core/Collection.swift
@@ -262,7 +262,12 @@ extension CollectionType {
262
///
263
/// - Complexity: O(1)
264
public var first: Generator.Element? {
265
- return isEmpty ? nil : self[startIndex]
+ // 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()
271
}
272
273
/// Returns a value less than or equal to the number of elements in
0 commit comments