[swift-evolution] Proposal: Add SequenceType.first

Gwendal Roué gwendal.roue at gmail.com
Sat Jan 2 06:02:09 CST 2016


My two cents: I feel uncomfortable with SequenceType.first since SequenceType clearly states that it may be destructed on iteration.

Compare :

seq.generate().next() // clear that it may give another result if called twice
seq.first             // unclear that it may give another result if called twice


> Le 31 déc. 2015 à 00:57, Kevin Ballard via swift-evolution <swift-evolution at swift.org> a écrit :
> It's sometimes useful to get the first element of a sequence. To that end I'd like to propose
> extension SequenceType {
>     /// Returns the first element of `self`, or `nil` if `self` is empty.
>     /// - Complexity: O(1)
>     var first: Self.Generator.Element? {
>         var gen = generate()
>         return gen.next()
>     }
> }
> I think it makes sense to add this property to the definition of SequenceType as well, so various sequences can override it to avoid constructing a generator.
> With this added to SequenceType, we can remove it from CollectionType, as the behavior will be the same.
> -Kevin Ballard
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160102/5515083e/attachment.html>

More information about the swift-evolution mailing list