[swift-evolution] Proposal: Add function SequenceType.find()
Dave Abrahams
dabrahams at apple.com
Wed Feb 17 17:30:17 CST 2016
on Wed Feb 17 2016, Radosław Pietruszewski <swift-evolution at swift.org> wrote:
> +1. Like others, this was one of the first stdlib extensions I’ve put
> into my Swift projects. It’s very common to want to find the first
> matching element in an array (or, perhaps even more common, find what
> you know if the only matching element — `filter` without the extra
> step of unpacking the value from array.)
>
> It’s also common in other languages, and `find` is a good name.
My only hesitation is that find has the strong connotation of not
modifying the underlying sequence, but any single-pass sequence will be
consumed. I don't know why that should bother me, though, considering
the many other such methods we have defined on SequenceType.
>
> — Radek
>
>> On 30 Dec 2015, at 03: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:
>>
>> extension SequenceType {
>> /// 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
--
-Dave
More information about the swift-evolution
mailing list