[swift-evolution] Proposal: Add SequenceType.first

Daniel Duan daniel at duan.org
Wed Dec 30 18:39:49 CST 2015


Here it is https://github.com/apple/swift/blob/master/stdlib/public/core/CollectionAlgorithms.swift.gyb#L26 <https://github.com/apple/swift/blob/master/stdlib/public/core/CollectionAlgorithms.swift.gyb#L26>

> On Dec 30, 2015, at 4:27 PM, Kevin Ballard <kevin at sb.org> wrote:
> 
> We already don't have a .last on CollectionType and nobody's been complaining about that. Besides, sequences don't necessarily even terminate.
>  
> -Kevin Ballard
>  
> On Wed, Dec 30, 2015, at 04:01 PM, Daniel Duan wrote:
>> Users who don’t get the single-pass nature of SequenceType may expect a .last as well.
>>  
>>> On Dec 30, 2015, at 3:57 PM, Kevin Ballard via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>  
>>> It's sometimes useful to get the first element of a sequence. To that end I'd like to propose
>>>  
>>> extensionSequenceType {
>>> /// 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 <mailto: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/20151230/ea689c26/attachment.html>


More information about the swift-evolution mailing list