[swift-evolution] [Pitch] Add an all algorithm to Sequence

BJ Homer bjhomer at gmail.com
Sun Apr 2 08:32:18 CDT 2017


'forAll' is definitely confusing; it sounds like iteration; I would not expect that the closure would be required to return a Bool. The implementation would likely bail out as soon as a single item failed the test; there is no guarantee that each item would be visited, so iteration is an incorrect mental model.

In Python, this is just called 'all()'. (There is a corresponding 'any()'.) We could follow the example of 'filter(_ isIncluded:)', which has a in internal parameter name for documentation, but takes no parameter at the call site; this might look like 'all(_ predicate:)'. Or we could follow the example of 'drop(while:)' and do 'all(test:)'. (And with trailing closure syntax, this would simply become 'all' (e.g.  'let readyToGo = collection.all { $0.isReady }'.

If a more explicit base name is desired, I suggest 'allPass(test:)'.

-BJ

> On Apr 2, 2017, at 3:17 AM, Richard Wei via swift-evolution <swift-evolution at swift.org> wrote:
> 
> `withoutException` sounds confusing to me. And it’ll potentially make a Swift newcomer think it has something to do with runtime exceptions.
> 
> IMO `forAll(_:)` is the best name. It looks logically, quantificationally clear. With regard to the possible confusion w/ `forEach`, the “each" in `forEach` conveys the sense of iteration, while the “all” in `forAll` conveys both iteration and conjunction.
> 
> -Richard
> 
>> On Apr 2, 2017, at 00:05, Robert Bennett via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> It figures, the hardest thing to pick is the name of this function…
>> 
>> I like forAll the best so far, but I worry that it sounds too much like forEach and would be confusing.
>> 
>> What does everyone think of withoutException? nums.withoutException(isEven) and nums.withoutException { isEven($0) } make their purpose clear, and even make clear what happens for an empty Collection.
>> 
>> Other options that come to mind that I am less enthusiastic about:
>> 
>> nums.every(satisfies: isEven) / nums.every { isEven($0) }
>> nums.entirely(isEven) / nums.entirely { isEven($0) }
>> _______________________________________________
>> 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


More information about the swift-evolution mailing list