<div dir="ltr">Agree, this is a nice addition as-is.<div class="gmail_extra"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 31, 2017 at 10:42 AM, Sean Heber via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I like it.<br>
<br>
l8r<br>
Sean<br>
<div><div class="h5"><br>
<br>
> On Mar 31, 2017, at 10:28 AM, Ben Cohen via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
> Hi,<br>
><br>
> A short proposal for you as part of the algorithms theme. Hopefully non-controversial, aside from the naming of the method and arguments, about which controversy abounds. Online copy here: <a href="https://github.com/airspeedswift/swift-evolution/blob/9a778e904c9be8a3692edd19bb757b23c54aacbe/proposals/0162-all-algorithm.md" rel="noreferrer" target="_blank">https://github.com/<wbr>airspeedswift/swift-evolution/<wbr>blob/<wbr>9a778e904c9be8a3692edd19bb757b<wbr>23c54aacbe/proposals/0162-all-<wbr>algorithm.md</a><br>
><br>
><br>
> Add an all algorithm to Sequence<br>
><br>
> • Proposal: SE-NNNN<br>
> • Authors: Ben Cohen<br>
> • Review Manager: TBD<br>
> • Status: Awaiting review<br>
> Introduction<br>
><br>
> It is common to want to confirm that every element of a sequence equals a value, or matches a certain criteria. Many implementations of this can be found in use on github. This proposal adds such a method to Sequence.<br>
><br>
> Motivation<br>
><br>
> You can achieve this in Swift 3 with contains by negating both the criteria and the result:<br>
><br>
> // every element is 9<br>
><br>
> !nums.<br>
> contains { $0 != 9<br>
> }<br>
><br>
> // every element is odd<br>
><br>
> !nums.<br>
> contains { !isOdd($0) }<br>
> but these are a readability nightmare. Additionally, developers may not make the leap to realize contains can be used this way, so may hand-roll their own for loop, which could be buggy, or compose other inefficient alternatives:<br>
><br>
> // misses opportunity to bail early<br>
><br>
> nums.<br>
> reduce(true) { $0.0 && $0.1 == 9<br>
> }<br>
><br>
> // the most straw-man travesty I could think of...<br>
> Set(nums).count == 1 && Set(nums).first == 9<br>
> Proposed solution<br>
><br>
> Introduce two algorithms on Sequence which test every element and return true if they match:<br>
><br>
> nums.all(equal: 9<br>
> )<br>
> nums.all(match: isOdd)<br>
><br>
> Detailed design<br>
><br>
> Add the following extensions to Sequence:<br>
><br>
> extension Sequence<br>
> {<br>
><br>
> /// Returns a Boolean value indicating whether every element of the sequence<br>
><br>
><br>
> /// satisfies the given predicate.<br>
><br>
><br>
> func all(match criteria: (Iterator.Element) throws -> Bool) rethrows -> Bool<br>
><br>
> }<br>
><br>
><br>
> extension Sequence where Iterator.Element: Equatable<br>
> {<br>
><br>
> /// Returns a Boolean value indicating whether every element of the sequence<br>
><br>
><br>
> /// equals the given element.<br>
><br>
><br>
> func all(equal element: Iterator.Element) -> Bool<br>
><br>
> }<br>
><br>
> Source compatibility<br>
><br>
> This change is purely additive so has no source compatibility consequences.<br>
><br>
> Effect on ABI stability<br>
><br>
> This change is purely additive so has no ABI stability consequences.<br>
><br>
> Effect on API resilience<br>
><br>
> This change is purely additive so has no API resilience consequences.<br>
><br>
> Alternatives considered<br>
><br>
> Not adding it.<br>
</div></div>> ______________________________<wbr>_________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br>
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</blockquote></div><br></div></div>