[swift-evolution] Add a while clause to for loops
Jacob Bandes-Storch
jtbandes at gmail.com
Mon Jun 6 15:55:55 CDT 2016
I brought up this suggestion in December:
http://thread.gmane.org/gmane.comp.lang.swift.evolution/1759
Jacob
On Mon, Jun 6, 2016 at 3:15 AM, Tim Vermeulen via swift-evolution <
swift-evolution at swift.org> wrote:
> We can already use a where clause in a for loop like this:
>
> for element in array where someCondition(element) {
> // …
> }
>
> which basically acts like
>
> for element in array {
> guard someCondition(element) else { continue }
> // …
> }
>
> Sometimes you want to break out of the loop when the condition isn’t met
> instead. I propose a while clause:
>
> for element in array while someCondition(element) {
> // …
> }
>
> which would be syntactic sugar for
>
> for element in array {
> guard someCondition(element) else { break }
> …
> }
>
> I can see this particularly being useful if we have a sorted array and we
> already know that once the condition isn’t met, it won’t be met either for
> subsequent elements. Another use case could be an infinite sequence that we
> want to cut off somewhere (which is simply not possible using a where
> clause).
> _______________________________________________
> 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/20160606/8f2b678a/attachment-0001.html>
More information about the swift-evolution
mailing list