[swift-evolution] Proposal: Add function SequenceType.find()

Kevin Ballard kevin at sb.org
Wed Dec 30 16:06:14 CST 2015


On Wed, Dec 30, 2015, at 02:13 AM, James Campbell wrote:
> We should add the full collection of ruby methodsĀ http://matthewcarriere.com/06/23/using-select-reject-collect-inject-and-detect/

We already have them. Well, almost:

select - this is filter reject - this is just filter with a negated
predicate collect - this is map inject - this is reduce detect - this is
the find() method that I'm proposing here.

-Kevin

> On 30 Dec 2015, at 02:40, Keith Smiley via swift-evolution <swift-
> evolution at swift.org> wrote:
>> +1. We've added an extension for this and find it very useful. On
>> Tue, Dec 29, 2015 at 18:38 Kevin Ballard via swift-evolution <swift-
>> evolution at swift.org> wrote:
>>> __
>>> I'm proposing a new extension method on SequenceType called find().
>>> It's similar to CollectionType.indexOf() except it returns the
>>> element:
>>>
>>> extensionSequenceType {


>>> /// Returns the first element where `predicate` returns `true`,
>>> or `nil`


>>> /// if such value is not found.


>>> public func find(@noescape predicate: (Self.Generator.Element)
>>> throws -> Bool) rethrows -> Self.Generator.Element? {


>>> for elt in self {


>>> if try predicate(elt) {


>>> return elt


>>> }


>>> }


>>> return nil


>>> }


>>> }


>>>
>>> -Kevin Ballard
>>>
>>>
>>> _______________________________________________
>>>
swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>> _______________________________________________
>> 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/20151230/86b58426/attachment.html>


More information about the swift-evolution mailing list