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

Jonathan Hull jhull at gbis.com
Sun Apr 2 20:17:49 CDT 2017


I guess we could do containsOnly()?

> On Apr 2, 2017, at 6:06 PM, Ricardo Parada <rparada at mac.com> wrote:
> 
> I think the problem would be that if you want to use it with a trailing closure then it becomes misleading:
> 
> nums.contains { $0 % 2 == 0 }
> 
> 
> Sent from my iPhone
> 
>> On Apr 2, 2017, at 9:01 PM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> What about contains(only:)?
>> 
>> Thanks,
>> Jon
>> 
>>> On Apr 2, 2017, at 6:32 AM, BJ Homer via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> '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
>>> _______________________________________________
>>> 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