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

Ricardo Parada rparada at mac.com
Thu Apr 6 08:13:46 CDT 2017



If we were looking for conciseness then my picks would be:

every() - every elements meets condition
any() - anyone element meets condition
none() - none of the elements meets condition

Examples:

nums.every { isEven }
nums.any { isEven }
nums.none { isEven }

nums.every(matches: { isEven })
nums.any(matches:  { isEven })
nums.none(matches:  { isEven })

I rarely ever have a need for checking to see if all the elements equal a value. It's normally all the elements match a given condition   However if you wanted to see if every element is the number nine then:

nums.every { $0 == 9 }
nums.every(matches: { $0 == 9 })

func every(matches condition: (Element) -> Bool) -> Bool
func any(matches condition: (Element) -> Bool) -> Bool
func none(matches condition: (Element) -> Bool) -> Bool



> On Apr 6, 2017, at 8:07 AM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> 
> In JavaScipt they are known as "every" and "includes".
> 
> In C# they are "TrueForAll" and "Exists".
> 
> Not sure why "all" and "contains" is any less consistent.
>> On Thu, Apr 6, 2017 at 05:50 Víctor Pimentel Rodríguez via swift-evolution <swift-evolution at swift.org> wrote:
>> On Thu, Apr 6, 2017 at 12:44 AM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
>> On that note: ‘containsOnly' is still my favorite by a wide margin.  I know it is longer than ‘all’, but it’s behavior is much clearer (especially for those of us who have never used or heard of ‘all’ in other languages), and it’s relationship with ‘contains’ is also very clear.
>> 
>> Also +1 to containsOnly for this very reason.
>> 
>> In other languages (Python, Ruby, etc) that have an `all` method, they also have an `any` method, thus maintaining certain consistency.
>> 
>> In the Swift standard library that `any` method is called `contains`, so `containsOnly` matches nicely that consistency.
>> 
>> -- 
>> Víctor Pimentel
>> _______________________________________________
>> 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/20170406/8d74ecdd/attachment.html>


More information about the swift-evolution mailing list