Add a while clause to for loops


(Tim Vermeulen) #1

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).


(TJ Usiyan) #2

I hadn't thought about `while` in this regard but wouldn't `until` make
more sense? `while`, to me, actually reads like it should do what `where`
does. In any case, whether it is `while` or `where`, this seems like a
reasonable feature in my opinion.

TJ

···

On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution < swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jacob Bandes-Storch) #3

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Haravikk) #4

I’m a +1 for this idea. Like Thorsten I was initially a little concerned that while and where may look too similar, but actually I find them visually distinct enough, and actually in my code I’m probably more likely to use while than where on for loops, although both are useful.

···

On 6 Jun 2016, at 11:15, Tim Vermeulen via swift-evolution <swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tim Vermeulen) #5

I also considered `until`, but it would be a bit confusing that `where` makes sure a condition is met, while `until` makes sure the condition isn’t met. I think `while` makes more sense because it corresponds to `break` in the same way that `where` corresponds to `continue`.

`while`, to me, actually reads like it should do what `where` does.

To me, `while` reads like it should stop the loop once the condition isn’t met, just like in a while loop.

···

I hadn't thought about `while` in this regard but wouldn't `until` make more sense? `while`, to me, actually reads like it should do what `where` does. In any case, whether it is `while` or `where`, this seems like a reasonable feature in my opinion.

TJ

On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)
> https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #6

You're describing a while loop:
`while let element = sequence.next() where condition {...}`

Which as we've discussed can already be re-written with a for loop (which,
yes, can be lazy):
`for element in sequence.lazy.filter({ condition }) {...}`

And it can be explicitly spelled out inside the loop, a definite
readability gain for the same reason guard always requires an explicit else
block.

What do you gain with a new keyword?

···

On Tue, Jun 7, 2016 at 05:02 Haravikk via swift-evolution < swift-evolution@swift.org> wrote:

I’m a +1 for this idea. Like Thorsten I was initially a little concerned
that while and where may look too similar, but actually I find them
visually distinct enough, and actually in my code I’m probably more likely
to use while than where on for loops, although both are useful.

> On 6 Jun 2016, at 11:15, Tim Vermeulen via swift-evolution < > swift-evolution@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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Thorsten Seitz) #7

I also considered `until`, but it would be a bit confusing that `where` makes sure a condition is met, while `until` makes sure the condition isn’t met. I think `while` makes more sense because it corresponds to `break` in the same way that `where` corresponds to `continue`.

That's a good argument! The only drawback is that `while` and `where` look quite similar at a glance.

-Thorsten

···

Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution <swift-evolution@swift.org>:

`while`, to me, actually reads like it should do what `where` does.

To me, `while` reads like it should stop the loop once the condition isn’t met, just like in a while loop.

I hadn't thought about `while` in this regard but wouldn't `until` make more sense? `while`, to me, actually reads like it should do what `where` does. In any case, whether it is `while` or `where`, this seems like a reasonable feature in my opinion.

TJ

On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tim Vermeulen) #8

`while let element = sequence.next() where condition {…}`

This is not a good alternative. First of all, it uses `while let` and `next()` instead of `for … in` which is way more complex than necessary. Also, if we want to skip over some elements, we either have to use

`sequence.lazy.filter({ someCondition($0) }).next()`

which is very hard to read compared to the proposal, or

`guard someCondition(element) else { continue }`

which is certainly better, but the added noise is still not very elegant. It’s also slightly confusing because we’d be both evaluating a condition outside of the loop body and inside the body.

`for element in sequence.lazy.filter({ condition }) {…}`

Not something I would go for either, again, because of the boilerplate code. You end up with an anonymous closure parameter despite already having named the element “element”, unless you name the parameter, but then you’d have to name it twice. Also, `.lazy.filter({ condition })` is a lot harder for beginners to grasp than a `where` or `while` keyword.

And it can be explicitly spelled out inside the loop

Probably the best option for now. In my opinion it’s still a bit confusing to enter the loop body but then decide to leave anyways, but it’s not too bad.

···

You're describing a while loop:
`while let element = sequence.next() where condition {...}`

Which as we've discussed can already be re-written with a for loop (which, yes, can be lazy):
`for element in sequence.lazy.filter({ condition }) {...}`

And it can be explicitly spelled out inside the loop, a definite readability gain for the same reason guard always requires an explicit else block.

What do you gain with a new keyword?

On Tue, Jun 7, 2016 at 05:02 Haravikk via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)>wrote:
> I’m a +1 for this idea. Like Thorsten I was initially a little concerned that while and where may look too similar, but actually I find them visually distinct enough, and actually in my code I’m probably more likely to use while than where on for loops, although both are useful.
>
> >On 6 Jun 2016, at 11:15, Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)
> >https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> https://lists.swift.org/mailman/listinfo/swift-evolution


(TJ Usiyan) #9

(As I said, I can live with `while`. I am simply presenting a potential
point of confusion.)
You aren't evaluating the statements in the loop 'while' the condition
isn't met. The first time that the condition isn't met, evaluation of the
loop stops. I get that this is technically true for the `while` construct
but I suggest that the only reason that it works there is that 'stopping
the first time that the condition isn't met' *is* the construct. Here, we
have a loop that we execute for each thing and we're tacking
on/intermingling the `while` construct.

···

On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz <tseitz42@icloud.com> wrote:

> Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution < > swift-evolution@swift.org>:
>
> I also considered `until`, but it would be a bit confusing that `where`
makes sure a condition is met, while `until` makes sure the condition isn’t
met. I think `while` makes more sense because it corresponds to `break` in
the same way that `where` corresponds to `continue`.

That's a good argument! The only drawback is that `while` and `where` look
quite similar at a glance.

-Thorsten

>
>> `while`, to me, actually reads like it should do what `where` does.
>
> To me, `while` reads like it should stop the loop once the condition
isn’t met, just like in a while loop.
>
>> I hadn't thought about `while` in this regard but wouldn't `until` make
more sense? `while`, to me, actually reads like it should do what `where`
does. In any case, whether it is `while` or `where`, this seems like a
reasonable feature in my opinion.
>>
>> TJ
>>
>> On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution< > swift-evolution@swift.org(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #10

The functionality being asked for here is already accepted for inclusion to
Swift as a method on Sequence named `prefix(while:)` (SE-0045):

`for element in array.prefix(while: { someCondition($0) }) { ... }`

···

On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via swift-evolution < swift-evolution@swift.org> wrote:

(As I said, I can live with `while`. I am simply presenting a potential
point of confusion.)
You aren't evaluating the statements in the loop 'while' the condition
isn't met. The first time that the condition isn't met, evaluation of the
loop stops. I get that this is technically true for the `while` construct
but I suggest that the only reason that it works there is that 'stopping
the first time that the condition isn't met' *is* the construct. Here, we
have a loop that we execute for each thing and we're tacking
on/intermingling the `while` construct.

On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz <tseitz42@icloud.com> > wrote:

> Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution < >> swift-evolution@swift.org>:
>
> I also considered `until`, but it would be a bit confusing that `where`
makes sure a condition is met, while `until` makes sure the condition isn’t
met. I think `while` makes more sense because it corresponds to `break` in
the same way that `where` corresponds to `continue`.

That's a good argument! The only drawback is that `while` and `where`
look quite similar at a glance.

-Thorsten

>
>> `while`, to me, actually reads like it should do what `where` does.
>
> To me, `while` reads like it should stop the loop once the condition
isn’t met, just like in a while loop.
>
>> I hadn't thought about `while` in this regard but wouldn't `until`
make more sense? `while`, to me, actually reads like it should do what
`where` does. In any case, whether it is `while` or `where`, this seems
like a reasonable feature in my opinion.
>>
>> TJ
>>
>> On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution< >> swift-evolution@swift.org(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #11

> `while let element = sequence.next() where condition {…}`

This is not a good alternative. First of all, it uses `while let` and
`next()` instead of `for … in` which is way more complex than necessary.
Also, if we want to skip over some elements, we either have to use

`sequence.lazy.filter({ someCondition($0) }).next()`

which is very hard to read compared to the proposal, or

`guard someCondition(element) else { continue }`

which is certainly better, but the added noise is still not very elegant.
It’s also slightly confusing because we’d be both evaluating a condition
outside of the loop body and inside the body.

> `for element in sequence.lazy.filter({ condition }) {…}`

Not something I would go for either, again, because of the boilerplate
code. You end up with an anonymous closure parameter despite already having
named the element “element”, unless you name the parameter, but then you’d
have to name it twice. Also, `.lazy.filter({ condition })` is a lot harder
for beginners to grasp than a `where` or `while` keyword.

> And it can be explicitly spelled out inside the loop

Probably the best option for now. In my opinion it’s still a bit confusing
to enter the loop body but then decide to leave anyways, but it’s not too
bad.

I think it's a very good option, so we don't really disagree here :slight_smile:

The bottom line is this: we have powerful methods on Sequence and
Collection that allow you to iterate over conforming types without ever
writing a loop. You're right that it becomes harder to read when you put
them in a condition statement. But it's not just because there are dots and
braces and dollar signs. Taken to the extreme, you could probably figure
out a way to rewrite the whole body of a loop into the condition statement
itself, leaving only empty braces. But that would be very silly and very
unreadable even if we didn't use dots and braces and dollar signs.

Clearly, we both agree that a good way is to stick with how things were
done in the old days, where things that are evaluated every iteration
strictly inside the loop. Features like `where` clauses and `while` clauses
provide new sugar that removes the punctuation (you call it "noise") when
using the same powerful methods on Sequence and Collection that we already
have, thus *encouraging* more things to be put into the condition
statement. Here, I disagree with you this would make for more readable
code. As you and I agree, if the alternative to `sequence.lazy.filter
{...}` is to put things inside the loop, we'll think twice about it and put
things inside the loop. That, IMO, is where they best belong.

···

On Tue, Jun 7, 2016 at 2:39 PM, Tim Vermeulen <tvermeulen@me.com> wrote:

> You're describing a while loop:
> `while let element = sequence.next() where condition {...}`
>
> Which as we've discussed can already be re-written with a for loop
(which, yes, can be lazy):
> `for element in sequence.lazy.filter({ condition }) {...}`
>
> And it can be explicitly spelled out inside the loop, a definite
readability gain for the same reason guard always requires an explicit else
block.
>
> What do you gain with a new keyword?
>
> On Tue, Jun 7, 2016 at 05:02 Haravikk via swift-evolution< > swift-evolution@swift.org(mailto:swift-evolution@swift.org)>wrote:
> > I’m a +1 for this idea. Like Thorsten I was initially a little
concerned that while and where may look too similar, but actually I find
them visually distinct enough, and actually in my code I’m probably more
likely to use while than where on for loops, although both are useful.
> >
> > >On 6 Jun 2016, at 11:15, Tim Vermeulen via swift-evolution< > swift-evolution@swift.org(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)
> > >https://lists.swift.org/mailman/listinfo/swift-evolution
> >
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> > https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>


(Tim Vermeulen) #12

The functionality of the `where` clause in `for` loops also already can be mimicked using `filter`. Wouldn’t we have to get ride of the `where` clause by that logic?

···

The functionality being asked for here is already accepted for inclusion to Swift as a method on Sequence named `prefix(while:)` (SE-0045):

`for element in array.prefix(while: { someCondition($0) }) { ... }`
On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)>wrote:
> (As I said, I can live with `while`. I am simply presenting a potential point of confusion.)
> You aren't evaluating the statements in the loop 'while' the condition isn't met. The first time that the condition isn't met, evaluation of the loop stops. I get that this is technically true for the `while` construct but I suggest that the only reason that it works there is that 'stopping the first time that the condition isn't met' *is* the construct. Here, we have a loop that we execute for each thing and we're tacking on/intermingling the `while` construct.
>
>
>
> On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz<tseitz42@icloud.com(mailto:tseitz42@icloud.com)>wrote:
> >
> > >Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)>:
> > >
> > >I also considered `until`, but it would be a bit confusing that `where` makes sure a condition is met, while `until` makes sure the condition isn’t met. I think `while` makes more sense because it corresponds to `break` in the same way that `where` corresponds to `continue`.
> >
> > That's a good argument! The only drawback is that `while` and `where` look quite similar at a glance.
> >
> > -Thorsten
> >
> > >
> > >>`while`, to me, actually reads like it should do what `where` does.
> > >
> > >To me, `while` reads like it should stop the loop once the condition isn’t met, just like in a while loop.
> > >
> > >>I hadn't thought about `while` in this regard but wouldn't `until` make more sense? `while`, to me, actually reads like it should do what `where` does. In any case, whether it is `while` or `where`, this seems like a reasonable feature in my opinion.
> > >>
> > >>TJ
> > >>
> > >>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> > >>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >_______________________________________________
> > >swift-evolution mailing list
> > >swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> > >https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #13

The burden of proof for adding new features is different from that for
taking away existing features.

If a feature doesn't yet exist, a successful proposal will show how it
provides additional and non-trivial utility. If a feature already exists, a
successful proposal to remove it will show how it is harmful to the
language or contrary to the direction in which it is evolving.

···

On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen <tvermeulen@me.com> wrote:

The functionality of the `where` clause in `for` loops also already can be
mimicked using `filter`. Wouldn’t we have to get ride of the `where` clause
by that logic?

> The functionality being asked for here is already accepted for inclusion
to Swift as a method on Sequence named `prefix(while:)` (SE-0045):
>
> `for element in array.prefix(while: { someCondition($0) }) { ... }`
> On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via swift-evolution< > swift-evolution@swift.org(mailto:swift-evolution@swift.org)>wrote:
> > (As I said, I can live with `while`. I am simply presenting a
potential point of confusion.)
> > You aren't evaluating the statements in the loop 'while' the condition
isn't met. The first time that the condition isn't met, evaluation of the
loop stops. I get that this is technically true for the `while` construct
but I suggest that the only reason that it works there is that 'stopping
the first time that the condition isn't met' *is* the construct. Here, we
have a loop that we execute for each thing and we're tacking
on/intermingling the `while` construct.
> >
> >
> >
> > On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz<tseitz42@icloud.com > (mailto:tseitz42@icloud.com)>wrote:
> > >
> > > >Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution<
swift-evolution@swift.org(mailto:swift-evolution@swift.org)>:
> > > >
> > > >I also considered `until`, but it would be a bit confusing that
`where` makes sure a condition is met, while `until` makes sure the
condition isn’t met. I think `while` makes more sense because it
corresponds to `break` in the same way that `where` corresponds to
`continue`.
> > >
> > > That's a good argument! The only drawback is that `while` and
`where` look quite similar at a glance.
> > >
> > > -Thorsten
> > >
> > > >
> > > >>`while`, to me, actually reads like it should do what `where` does.
> > > >
> > > >To me, `while` reads like it should stop the loop once the
condition isn’t met, just like in a while loop.
> > > >
> > > >>I hadn't thought about `while` in this regard but wouldn't `until`
make more sense? `while`, to me, actually reads like it should do what
`where` does. In any case, whether it is `while` or `where`, this seems
like a reasonable feature in my opinion.
> > > >>
> > > >>TJ
> > > >>
> > > >>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution< > swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto: > swift-evolution@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@swift.org(mailto:swift-evolution@swift.org
)(mailto:swift-evolution@swift.org)
> > > >>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > > >_______________________________________________
> > > >swift-evolution mailing list
> > > >swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> > > >https://lists.swift.org/mailman/listinfo/swift-evolution
> >
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> > https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>


(Tim Vermeulen) #14

I didn’t mean we should really get rid of the `where` clause, it’s great. I guess the point I was trying to make is that we can use a `where` clause with a `for` loop in Swift, despite the existence of the `filter` method. So despite `prefix(while:)` in Swift 3, there might be room for a `while` clause. I think it makes the code a lot more readable, much like how `where` can make a `for` loop a lot more readable than using `filter`.

···

The burden of proof for adding new features is different from that for taking away existing features.

If a feature doesn't yet exist, a successful proposal will show how it provides additional and non-trivial utility. If a feature already exists, a successful proposal to remove it will show how it is harmful to the language or contrary to the direction in which it is evolving.

On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen<tvermeulen@me.com(mailto:tvermeulen@me.com)>wrote:
> The functionality of the `where` clause in `for` loops also already can be mimicked using `filter`. Wouldn’t we have to get ride of the `where` clause by that logic?
>
> >The functionality being asked for here is already accepted for inclusion to Swift as a method on Sequence named `prefix(while:)` (SE-0045):
> >
> >`for element in array.prefix(while: { someCondition($0) }) { ... }`
> >On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)>wrote:
> >>(As I said, I can live with `while`. I am simply presenting a potential point of confusion.)
> >>You aren't evaluating the statements in the loop 'while' the condition isn't met. The first time that the condition isn't met, evaluation of the loop stops. I get that this is technically true for the `while` construct but I suggest that the only reason that it works there is that 'stopping the first time that the condition isn't met' *is* the construct. Here, we have a loop that we execute for each thing and we're tacking on/intermingling the `while` construct.
> >>
> >>
> >>
> >>On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz<tseitz42@icloud.com(mailto:tseitz42@icloud.com)(mailto:tseitz42@icloud.com)>wrote:
> >>>
> >>>>Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)>:
> >>>>
> >>>>I also considered `until`, but it would be a bit confusing that `where` makes sure a condition is met, while `until` makes sure the condition isn’t met. I think `while` makes more sense because it corresponds to `break` in the same way that `where` corresponds to `continue`.
> >>>
> >>>That's a good argument! The only drawback is that `while` and `where` look quite similar at a glance.
> >>>
> >>>-Thorsten
> >>>
> >>>>
> >>>>>`while`, to me, actually reads like it should do what `where` does.
> >>>>
> >>>>To me, `while` reads like it should stop the loop once the condition isn’t met, just like in a while loop.
> >>>>
> >>>>>I hadn't thought about `while` in this regard but wouldn't `until` make more sense? `while`, to me, actually reads like it should do what `where` does. In any case, whether it is `while` or `where`, this seems like a reasonable feature in my opinion.
> >>>>>
> >>>>>TJ
> >>>>>
> >>>>>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> >>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> >>>>_______________________________________________
> >>>>swift-evolution mailing list
> >>>>swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> >>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> >>
> >>_______________________________________________
> >>swift-evolution mailing list
> >>swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> >>https://lists.swift.org/mailman/listinfo/swift-evolution
> >
> >
> >_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #15

Personally, given this discussion and the one about `where` in if and while
statements, I would not be opposed to elimination of `where` in control
statements altogether.

My reasoning would be that words like filter and prefix unambiguously
indicate what happens to elements of a sequence for which the predicate
returns false, whereas words like where and while are ambiguous.

···

On Mon, Jun 6, 2016 at 17:52 Tim Vermeulen <tvermeulen@me.com> wrote:

I didn’t mean we should really get rid of the `where` clause, it’s great.
I guess the point I was trying to make is that we can use a `where` clause
with a `for` loop in Swift, despite the existence of the `filter` method.
So despite `prefix(while:)` in Swift 3, there might be room for a `while`
clause. I think it makes the code a lot more readable, much like how
`where` can make a `for` loop a lot more readable than using `filter`.

> The burden of proof for adding new features is different from that for
taking away existing features.
>
> If a feature doesn't yet exist, a successful proposal will show how it
provides additional and non-trivial utility. If a feature already exists, a
successful proposal to remove it will show how it is harmful to the
language or contrary to the direction in which it is evolving.
>
> On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen<tvermeulen@me.com(mailto: > tvermeulen@me.com)>wrote:
> > The functionality of the `where` clause in `for` loops also already
can be mimicked using `filter`. Wouldn’t we have to get ride of the `where`
clause by that logic?
> >
> > >The functionality being asked for here is already accepted for
inclusion to Swift as a method on Sequence named `prefix(while:)` (SE-0045):
> > >
> > >`for element in array.prefix(while: { someCondition($0) }) { ... }`
> > >On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via swift-evolution< > swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto: > swift-evolution@swift.org)>wrote:
> > >>(As I said, I can live with `while`. I am simply presenting a
potential point of confusion.)
> > >>You aren't evaluating the statements in the loop 'while' the
condition isn't met. The first time that the condition isn't met,
evaluation of the loop stops. I get that this is technically true for the
`while` construct but I suggest that the only reason that it works there is
that 'stopping the first time that the condition isn't met' *is* the
construct. Here, we have a loop that we execute for each thing and we're
tacking on/intermingling the `while` construct.
> > >>
> > >>
> > >>
> > >>On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz<tseitz42@icloud.com > (mailto:tseitz42@icloud.com)(mailto:tseitz42@icloud.com)>wrote:
> > >>>
> > >>>>Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution<
swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:
swift-evolution@swift.org)>:
> > >>>>
> > >>>>I also considered `until`, but it would be a bit confusing that
`where` makes sure a condition is met, while `until` makes sure the
condition isn’t met. I think `while` makes more sense because it
corresponds to `break` in the same way that `where` corresponds to
`continue`.
> > >>>
> > >>>That's a good argument! The only drawback is that `while` and
`where` look quite similar at a glance.
> > >>>
> > >>>-Thorsten
> > >>>
> > >>>>
> > >>>>>`while`, to me, actually reads like it should do what `where`
does.
> > >>>>
> > >>>>To me, `while` reads like it should stop the loop once the
condition isn’t met, just like in a while loop.
> > >>>>
> > >>>>>I hadn't thought about `while` in this regard but wouldn't
`until` make more sense? `while`, to me, actually reads like it should do
what `where` does. In any case, whether it is `while` or `where`, this
seems like a reasonable feature in my opinion.
> > >>>>>
> > >>>>>TJ
> > >>>>>
> > >>>>>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution< > swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto: > swift-evolution@swift.org)(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org
)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> > >>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >>>>_______________________________________________
> > >>>>swift-evolution mailing list
> > >>>>swift-evolution@swift.org(mailto:swift-evolution@swift.org
)(mailto:swift-evolution@swift.org)
> > >>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >>
> > >>_______________________________________________
> > >>swift-evolution mailing list
> > >>swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:
swift-evolution@swift.org)
> > >>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >
> > >
> > >_______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>


(Charlie Monroe) #16

I strongly disagree.

Exchanging

for result in results where result.value != .Warning while result.value != .Error {
  /// ...
}

for either

for result in results.filter({ $0.value != .Warning }).prefix(while: { $0.value != .Error })) {
  /// ...
}

or

for result in results {
  if result.value == .Warning { continue }
  if result.value == .Error { break }
  
  /// ...
}

Seems like an absolute step back. Not to mention filter(_:slight_smile: doesn't return a lazy collection, but will recreate it, while the `where` will do on-the-fly check.

···

On Jun 7, 2016, at 1:34 AM, Xiaodi Wu via swift-evolution <swift-evolution@swift.org> wrote:

Personally, given this discussion and the one about `where` in if and while statements, I would not be opposed to elimination of `where` in control statements altogether.

My reasoning would be that words like filter and prefix unambiguously indicate what happens to elements of a sequence for which the predicate returns false, whereas words like where and while are ambiguous.

On Mon, Jun 6, 2016 at 17:52 Tim Vermeulen <tvermeulen@me.com <mailto:tvermeulen@me.com>> wrote:
I didn’t mean we should really get rid of the `where` clause, it’s great. I guess the point I was trying to make is that we can use a `where` clause with a `for` loop in Swift, despite the existence of the `filter` method. So despite `prefix(while:)` in Swift 3, there might be room for a `while` clause. I think it makes the code a lot more readable, much like how `where` can make a `for` loop a lot more readable than using `filter`.

> The burden of proof for adding new features is different from that for taking away existing features.
>
> If a feature doesn't yet exist, a successful proposal will show how it provides additional and non-trivial utility. If a feature already exists, a successful proposal to remove it will show how it is harmful to the language or contrary to the direction in which it is evolving.
>
> On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen<tvermeulen@me.com <mailto:tvermeulen@me.com>(mailto:tvermeulen@me.com <mailto:tvermeulen@me.com>)>wrote:
> > The functionality of the `where` clause in `for` loops also already can be mimicked using `filter`. Wouldn’t we have to get ride of the `where` clause by that logic?
> >
> > >The functionality being asked for here is already accepted for inclusion to Swift as a method on Sequence named `prefix(while:)` (SE-0045):
> > >
> > >`for element in array.prefix(while: { someCondition($0) }) { ... }`
> > >On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via swift-evolution<swift-evolution@swift.org <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)>wrote:
> > >>(As I said, I can live with `while`. I am simply presenting a potential point of confusion.)
> > >>You aren't evaluating the statements in the loop 'while' the condition isn't met. The first time that the condition isn't met, evaluation of the loop stops. I get that this is technically true for the `while` construct but I suggest that the only reason that it works there is that 'stopping the first time that the condition isn't met' *is* the construct. Here, we have a loop that we execute for each thing and we're tacking on/intermingling the `while` construct.
> > >>
> > >>
> > >>
> > >>On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz<tseitz42@icloud.com <mailto:tseitz42@icloud.com>(mailto:tseitz42@icloud.com <mailto:tseitz42@icloud.com>)(mailto:tseitz42@icloud.com <mailto:tseitz42@icloud.com>)>wrote:
> > >>>
> > >>>>Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution<swift-evolution@swift.org <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)>:
> > >>>>
> > >>>>I also considered `until`, but it would be a bit confusing that `where` makes sure a condition is met, while `until` makes sure the condition isn’t met. I think `while` makes more sense because it corresponds to `break` in the same way that `where` corresponds to `continue`.
> > >>>
> > >>>That's a good argument! The only drawback is that `while` and `where` look quite similar at a glance.
> > >>>
> > >>>-Thorsten
> > >>>
> > >>>>
> > >>>>>`while`, to me, actually reads like it should do what `where` does.
> > >>>>
> > >>>>To me, `while` reads like it should stop the loop once the condition isn’t met, just like in a while loop.
> > >>>>
> > >>>>>I hadn't thought about `while` in this regard but wouldn't `until` make more sense? `while`, to me, actually reads like it should do what `where` does. In any case, whether it is `while` or `where`, this seems like a reasonable feature in my opinion.
> > >>>>>
> > >>>>>TJ
> > >>>>>
> > >>>>>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution<swift-evolution@swift.org <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)
> > >>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >>>>_______________________________________________
> > >>>>swift-evolution mailing list
> > >>>>swift-evolution@swift.org <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)
> > >>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >>
> > >>_______________________________________________
> > >>swift-evolution mailing list
> > >>swift-evolution@swift.org <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org <mailto:swift-evolution@swift.org>)
> > >>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >
> > >
> > >_______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org <mailto:swift-evolution@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Tim Vermeulen) #17

I agree. `where` and `while` fit so well in for loops, IMO, because a for loop already loops over the elements - this is exactly what filter and prefix also do. You’ve even given the current element a name (for myElement in …) and it’s more natural to just use that name in a `where` clause or `whil`e clause, than to have to deal with $0 inside a closure.

···

I strongly disagree.

Exchanging

forresultinresultswhereresult.value != .Warningwhileresult.value != .Error {
/// ...
}

for either

forresultinresults.filter({ $0.value != .Warning }).prefix(while: { $0.value != .Error })) {
/// ...
}

or

forresultinresults {
ifresult.value == .Warning {continue}
ifresult.value == .Error {break}

/// ...
}

Seems like an absolute step back. Not to mention filter(_:slight_smile: doesn't return a lazy collection, but will recreate it, while the `where` will do on-the-fly check.

> On Jun 7, 2016, at 1:34 AM, Xiaodi Wu via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)>wrote:
> Personally, given this discussion and the one about `where` in if and while statements, I would not be opposed to elimination of `where` in control statements altogether.
>
> My reasoning would be that words like filter and prefix unambiguously indicate what happens to elements of a sequence for which the predicate returns false, whereas words like where and while are ambiguous.
>
> On Mon, Jun 6, 2016 at 17:52 Tim Vermeulen<tvermeulen@me.com(mailto:tvermeulen@me.com)>wrote:
> > I didn’t mean we should really get rid of the `where` clause, it’s great. I guess the point I was trying to make is that we can use a `where` clause with a `for` loop in Swift, despite the existence of the `filter` method. So despite `prefix(while:)` in Swift 3, there might be room for a `while` clause. I think it makes the code a lot more readable, much like how `where` can make a `for` loop a lot more readable than using `filter`.
> >
> > >The burden of proof for adding new features is different from that for taking away existing features.
> > >
> > >If a feature doesn't yet exist, a successful proposal will show how it provides additional and non-trivial utility. If a feature already exists, a successful proposal to remove it will show how it is harmful to the language or contrary to the direction in which it is evolving.
> > >
> > >On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen<tvermeulen@me.com(mailto:tvermeulen@me.com)(mailto:tvermeulen@me.com)>wrote:
> > >>The functionality of the `where` clause in `for` loops also already can be mimicked using `filter`. Wouldn’t we have to get ride of the `where` clause by that logic?
> > >>
> > >>>The functionality being asked for here is already accepted for inclusion to Swift as a method on Sequence named `prefix(while:)` (SE-0045):
> > >>>
> > >>>`for element in array.prefix(while: { someCondition($0) }) { ... }`
> > >>>On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)>wrote:
> > >>>>(As I said, I can live with `while`. I am simply presenting a potential point of confusion.)
> > >>>>You aren't evaluating the statements in the loop 'while' the condition isn't met. The first time that the condition isn't met, evaluation of the loop stops. I get that this is technically true for the `while` construct but I suggest that the only reason that it works there is that 'stopping the first time that the condition isn't met' *is* the construct. Here, we have a loop that we execute for each thing and we're tacking on/intermingling the `while` construct.
> > >>>>
> > >>>>
> > >>>>
> > >>>>On Mon, Jun 6, 2016 at 2:19 PM, Thorsten Seitz<tseitz42@icloud.com(mailto:tseitz42@icloud.com)(mailto:tseitz42@icloud.com)(mailto:tseitz42@icloud.com)>wrote:
> > >>>>>
> > >>>>>>Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)>:
> > >>>>>>
> > >>>>>>I also considered `until`, but it would be a bit confusing that `where` makes sure a condition is met, while `until` makes sure the condition isn’t met. I think `while` makes more sense because it corresponds to `break` in the same way that `where` corresponds to `continue`.
> > >>>>>
> > >>>>>That's a good argument! The only drawback is that `while` and `where` look quite similar at a glance.
> > >>>>>
> > >>>>>-Thorsten
> > >>>>>
> > >>>>>>
> > >>>>>>>`while`, to me, actually reads like it should do what `where` does.
> > >>>>>>
> > >>>>>>To me, `while` reads like it should stop the loop once the condition isn’t met, just like in a while loop.
> > >>>>>>
> > >>>>>>>I hadn't thought about `while` in this regard but wouldn't `until` make more sense? `while`, to me, actually reads like it should do what `where` does. In any case, whether it is `while` or `where`, this seems like a reasonable feature in my opinion.
> > >>>>>>>
> > >>>>>>>TJ
> > >>>>>>>
> > >>>>>>>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via swift-evolution<swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@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@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> > >>>>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >>>>>>_______________________________________________
> > >>>>>>swift-evolution mailing list
> > >>>>>>swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> > >>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >>>>
> > >>>>_______________________________________________
> > >>>>swift-evolution mailing list
> > >>>>swift-evolution@swift.org(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)(mailto:swift-evolution@swift.org)
> > >>>>https://lists.swift.org/mailman/listinfo/swift-evolution
> > >>>
> > >>>
> > >>>_______________________________________________
> > >swift-evolution mailing list
> > >swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> > >https://lists.swift.org/mailman/listinfo/swift-evolution
> > >
> > >
> > >_______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org(mailto:swift-evolution@swift.org)
> https://lists.swift.org/mailman/listinfo/swift-evolution


(Vladimir) #18

My +1 to the proposal and for Charlie's opinion. I believe `while` in `for` loop would be very handy and helpful in some situations, it is a pair for existed `where`, its meaning is obvious, and its existence can't depend on existence of any method in collections. I'd like to see a formal proposal for this feature.

···

On 07.06.2016 8:18, Charlie Monroe via swift-evolution wrote:

I strongly disagree.

Exchanging

for result in results where result.value != .Warning while result.value !=
.Error {
/// ...
}

for either

for result in results.filter({ $0.value != .Warning }).prefix(while: {
$0.value != .Error })) {
/// ...
}

or

for result in results {
if result.value == .Warning { continue }
if result.value == .Error { break }

/// ...
}

Seems like an absolute step back. Not to mention filter(_:slight_smile: doesn't return
a lazy collection, but will recreate it, while the `where` will do
on-the-fly check.

On Jun 7, 2016, at 1:34 AM, Xiaodi Wu via swift-evolution >> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Personally, given this discussion and the one about `where` in if and
while statements, I would not be opposed to elimination of `where` in
control statements altogether.

My reasoning would be that words like filter and prefix unambiguously
indicate what happens to elements of a sequence for which the predicate
returns false, whereas words like where and while are ambiguous.

On Mon, Jun 6, 2016 at 17:52 Tim Vermeulen <tvermeulen@me.com >> <mailto:tvermeulen@me.com>> wrote:

    I didn’t mean we should really get rid of the `where` clause, it’s
    great. I guess the point I was trying to make is that we can use a
    `where` clause with a `for` loop in Swift, despite the existence of
    the `filter` method. So despite `prefix(while:)` in Swift 3, there
    might be room for a `while` clause. I think it makes the code a lot
    more readable, much like how `where` can make a `for` loop a lot more
    readable than using `filter`.

    > The burden of proof for adding new features is different from that
    for taking away existing features.
    >
    > If a feature doesn't yet exist, a successful proposal will show how
    it provides additional and non-trivial utility. If a feature already
    exists, a successful proposal to remove it will show how it is
    harmful to the language or contrary to the direction in which it is
    evolving.
    >
    > On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen<tvermeulen@me.com >> <mailto:tvermeulen@me.com>(mailto:tvermeulen@me.com >> <mailto:tvermeulen@me.com>)>wrote:
    > > The functionality of the `where` clause in `for` loops also
    already can be mimicked using `filter`. Wouldn’t we have to get ride
    of the `where` clause by that logic?
    > >
    > > >The functionality being asked for here is already accepted for
    inclusion to Swift as a method on Sequence named `prefix(while:)`
    (SE-0045):
    > > >
    > > >`for element in array.prefix(while: { someCondition($0) }) { ... }`
    > > >On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via >> swift-evolution<swift-evolution@swift.org >> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org >> <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org >> <mailto:swift-evolution@swift.org>)>wrote:
    > > >>(As I said, I can live with `while`. I am simply presenting a
    potential point of confusion.)
    > > >>You aren't evaluating the statements in the loop 'while' the
    condition isn't met. The first time that the condition isn't met,
    evaluation of the loop stops. I get that this is technically true for
    the `while` construct but I suggest that the only reason that it
    works there is that 'stopping the first time that the condition isn't
    met' *is* the construct. Here, we have a loop that we execute for
    each thing and we're tacking on/intermingling the `while` construct.
    > > >>
    > > >>On Mon, Jun 6, 2016 at 2:19 PM, Thorsten >> Seitz<tseitz42@icloud.com >> <mailto:tseitz42@icloud.com>(mailto:tseitz42@icloud.com >> <mailto:tseitz42@icloud.com>)(mailto:tseitz42@icloud.com >> <mailto:tseitz42@icloud.com>)>wrote:
    > > >>>
    > > >>>>Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via >> swift-evolution<swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)>:
    > > >>>>
    > > >>>>I also considered `until`, but it would be a bit confusing
    that `where` makes sure a condition is met, while `until` makes sure
    the condition isn’t met. I think `while` makes more sense because it
    corresponds to `break` in the same way that `where` corresponds to
    `continue`.
    > > >>>
    > > >>>That's a good argument! The only drawback is that `while` and
    `where` look quite similar at a glance.
    > > >>>
    > > >>>-Thorsten
    > > >>>
    > > >>>>
    > > >>>>>`while`, to me, actually reads like it should do what
    `where` does.
    > > >>>>
    > > >>>>To me, `while` reads like it should stop the loop once the
    condition isn’t met, just like in a while loop.
    > > >>>>
    > > >>>>>I hadn't thought about `while` in this regard but wouldn't
    `until` make more sense? `while`, to me, actually reads like it
    should do what `where` does. In any case, whether it is `while` or
    `where`, this seems like a reasonable feature in my opinion.
    > > >>>>>
    > > >>>>>TJ
    > > >>>>>
    > > >>>>>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via
    swift-evolution<swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@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@swift.org
    <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)
    > > >>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
    > > >>>>_______________________________________________
    > > >>>>swift-evolution mailing list
    > > >>>>swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)
    > > >>>>https://lists.swift.org/mailman/listinfo/swift-evolution
    > > >>
    > > >>_______________________________________________
    > > >>swift-evolution mailing list
    > > >>swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
    <mailto:swift-evolution@swift.org>)
    > > >>https://lists.swift.org/mailman/listinfo/swift-evolution
    > > >
    > > >_______________________________________________
    > swift-evolution mailing list
    > swift-evolution@swift.org <mailto:swift-evolution@swift.org>
    > https://lists.swift.org/mailman/listinfo/swift-evolution
    >

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Rob Norback) #19

I give my +1 to this feature, it allows you to be explicit about a for loop escape condition at the outset instead of it being contained within the loops logic making them easier to maintain. Also it reads extremely well.

···

On Jun 7, 2016, at 5:20 AM, Vladimir.S via swift-evolution <swift-evolution@swift.org> wrote:

My +1 to the proposal and for Charlie's opinion. I believe `while` in `for` loop would be very handy and helpful in some situations, it is a pair for existed `where`, its meaning is obvious, and its existence can't depend on existence of any method in collections. I'd like to see a formal proposal for this feature.

On 07.06.2016 8:18, Charlie Monroe via swift-evolution wrote:
I strongly disagree.

Exchanging

for result in results where result.value != .Warning while result.value !=
.Error {
/// ...
}

for either

for result in results.filter({ $0.value != .Warning }).prefix(while: {
$0.value != .Error })) {
/// ...
}

or

for result in results {
if result.value == .Warning { continue }
if result.value == .Error { break }

/// ...
}

Seems like an absolute step back. Not to mention filter(_:slight_smile: doesn't return
a lazy collection, but will recreate it, while the `where` will do
on-the-fly check.

On Jun 7, 2016, at 1:34 AM, Xiaodi Wu via swift-evolution >>> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Personally, given this discussion and the one about `where` in if and
while statements, I would not be opposed to elimination of `where` in
control statements altogether.

My reasoning would be that words like filter and prefix unambiguously
indicate what happens to elements of a sequence for which the predicate
returns false, whereas words like where and while are ambiguous.

On Mon, Jun 6, 2016 at 17:52 Tim Vermeulen <tvermeulen@me.com >>> <mailto:tvermeulen@me.com>> wrote:

   I didn’t mean we should really get rid of the `where` clause, it’s
   great. I guess the point I was trying to make is that we can use a
   `where` clause with a `for` loop in Swift, despite the existence of
   the `filter` method. So despite `prefix(while:)` in Swift 3, there
   might be room for a `while` clause. I think it makes the code a lot
   more readable, much like how `where` can make a `for` loop a lot more
   readable than using `filter`.

   > The burden of proof for adding new features is different from that
   for taking away existing features.
   >
   > If a feature doesn't yet exist, a successful proposal will show how
   it provides additional and non-trivial utility. If a feature already
   exists, a successful proposal to remove it will show how it is
   harmful to the language or contrary to the direction in which it is
   evolving.
   >
   > On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen<tvermeulen@me.com >>> <mailto:tvermeulen@me.com>(mailto:tvermeulen@me.com >>> <mailto:tvermeulen@me.com>)>wrote:
   > > The functionality of the `where` clause in `for` loops also
   already can be mimicked using `filter`. Wouldn’t we have to get ride
   of the `where` clause by that logic?
   > >
   > > >The functionality being asked for here is already accepted for
   inclusion to Swift as a method on Sequence named `prefix(while:)`
   (SE-0045):
   > > >
   > > >`for element in array.prefix(while: { someCondition($0) }) { ... }`
   > > >On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via >>> swift-evolution<swift-evolution@swift.org >>> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org >>> <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org >>> <mailto:swift-evolution@swift.org>)>wrote:
   > > >>(As I said, I can live with `while`. I am simply presenting a
   potential point of confusion.)
   > > >>You aren't evaluating the statements in the loop 'while' the
   condition isn't met. The first time that the condition isn't met,
   evaluation of the loop stops. I get that this is technically true for
   the `while` construct but I suggest that the only reason that it
   works there is that 'stopping the first time that the condition isn't
   met' *is* the construct. Here, we have a loop that we execute for
   each thing and we're tacking on/intermingling the `while` construct.
   > > >>
   > > >>
   > > >>
   > > >>On Mon, Jun 6, 2016 at 2:19 PM, Thorsten >>> Seitz<tseitz42@icloud.com >>> <mailto:tseitz42@icloud.com>(mailto:tseitz42@icloud.com >>> <mailto:tseitz42@icloud.com>)(mailto:tseitz42@icloud.com >>> <mailto:tseitz42@icloud.com>)>wrote:
   > > >>>
   > > >>>>Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via >>> swift-evolution<swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)>:
   > > >>>>
   > > >>>>I also considered `until`, but it would be a bit confusing
   that `where` makes sure a condition is met, while `until` makes sure
   the condition isn’t met. I think `while` makes more sense because it
   corresponds to `break` in the same way that `where` corresponds to
   `continue`.
   > > >>>
   > > >>>That's a good argument! The only drawback is that `while` and
   `where` look quite similar at a glance.
   > > >>>
   > > >>>-Thorsten
   > > >>>
   > > >>>>
   > > >>>>>`while`, to me, actually reads like it should do what
   `where` does.
   > > >>>>
   > > >>>>To me, `while` reads like it should stop the loop once the
   condition isn’t met, just like in a while loop.
   > > >>>>
   > > >>>>>I hadn't thought about `while` in this regard but wouldn't
   `until` make more sense? `while`, to me, actually reads like it
   should do what `where` does. In any case, whether it is `while` or
   `where`, this seems like a reasonable feature in my opinion.
   > > >>>>>
   > > >>>>>TJ
   > > >>>>>
   > > >>>>>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via
   swift-evolution<swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@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@swift.org
   <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)
   > > >>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
   > > >>>>_______________________________________________
   > > >>>>swift-evolution mailing list
   > > >>>>swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)
   > > >>>>https://lists.swift.org/mailman/listinfo/swift-evolution
   > > >>
   > > >>_______________________________________________
   > > >>swift-evolution mailing list
   > > >>swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)(mailto:swift-evolution@swift.org
   <mailto:swift-evolution@swift.org>)
   > > >>https://lists.swift.org/mailman/listinfo/swift-evolution
   > > >
   > > >
   > > >_______________________________________________
   > swift-evolution mailing list
   > swift-evolution@swift.org <mailto:swift-evolution@swift.org>
   > https://lists.swift.org/mailman/listinfo/swift-evolution
   >
   >
   >

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #20

filter() is and prefix(while:) will be available on all sequences. The
for...in loop only traverses through sequences.

The meaning of the proposed while is not at all a pair for where, since
where clauses in while loops would do the same thing as while clauses in
for loops. That's crazy.

···

On Tue, Jun 7, 2016 at 06:20 Vladimir.S via swift-evolution < swift-evolution@swift.org> wrote:

My +1 to the proposal and for Charlie's opinion. I believe `while` in `for`
loop would be very handy and helpful in some situations, it is a pair for
existed `where`, its meaning is obvious, and its existence can't depend on
existence of any method in collections. I'd like to see a formal proposal
for this feature.

On 07.06.2016 8:18, Charlie Monroe via swift-evolution wrote:
> I strongly disagree.
>
> Exchanging
>
> for result in results where result.value != .Warning while result.value
!=
> .Error {
> /// ...
> }
>
> for either
>
> for result in results.filter({ $0.value != .Warning }).prefix(while: {
> $0.value != .Error })) {
> /// ...
> }
>
> or
>
> for result in results {
> if result.value == .Warning { continue }
> if result.value == .Error { break }
>
> /// ...
> }
>
> Seems like an absolute step back. Not to mention filter(_:slight_smile: doesn't
return
> a lazy collection, but will recreate it, while the `where` will do
> on-the-fly check.
>
>> On Jun 7, 2016, at 1:34 AM, Xiaodi Wu via swift-evolution > >> <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>>
>> Personally, given this discussion and the one about `where` in if and
>> while statements, I would not be opposed to elimination of `where` in
>> control statements altogether.
>>
>> My reasoning would be that words like filter and prefix unambiguously
>> indicate what happens to elements of a sequence for which the predicate
>> returns false, whereas words like where and while are ambiguous.
>>
>> On Mon, Jun 6, 2016 at 17:52 Tim Vermeulen <tvermeulen@me.com > >> <mailto:tvermeulen@me.com>> wrote:
>>
>> I didn’t mean we should really get rid of the `where` clause, it’s
>> great. I guess the point I was trying to make is that we can use a
>> `where` clause with a `for` loop in Swift, despite the existence of
>> the `filter` method. So despite `prefix(while:)` in Swift 3, there
>> might be room for a `while` clause. I think it makes the code a lot
>> more readable, much like how `where` can make a `for` loop a lot
more
>> readable than using `filter`.
>>
>> > The burden of proof for adding new features is different from that
>> for taking away existing features.
>> >
>> > If a feature doesn't yet exist, a successful proposal will show
how
>> it provides additional and non-trivial utility. If a feature already
>> exists, a successful proposal to remove it will show how it is
>> harmful to the language or contrary to the direction in which it is
>> evolving.
>> >
>> > On Mon, Jun 6, 2016 at 15:38 Tim Vermeulen<tvermeulen@me.com > >> <mailto:tvermeulen@me.com>(mailto:tvermeulen@me.com > >> <mailto:tvermeulen@me.com>)>wrote:
>> > > The functionality of the `where` clause in `for` loops also
>> already can be mimicked using `filter`. Wouldn’t we have to get ride
>> of the `where` clause by that logic?
>> > >
>> > > >The functionality being asked for here is already accepted for
>> inclusion to Swift as a method on Sequence named `prefix(while:)`
>> (SE-0045):
>> > > >
>> > > >`for element in array.prefix(while: { someCondition($0) }) {
... }`
>> > > >On Mon, Jun 6, 2016 at 14:31 T.J. Usiyan via
>> swift-evolution<swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)>wrote:
>> > > >>(As I said, I can live with `while`. I am simply presenting a
>> potential point of confusion.)
>> > > >>You aren't evaluating the statements in the loop 'while' the
>> condition isn't met. The first time that the condition isn't met,
>> evaluation of the loop stops. I get that this is technically true
for
>> the `while` construct but I suggest that the only reason that it
>> works there is that 'stopping the first time that the condition
isn't
>> met' *is* the construct. Here, we have a loop that we execute for
>> each thing and we're tacking on/intermingling the `while` construct.
>> > > >>
>> > > >>
>> > > >>
>> > > >>On Mon, Jun 6, 2016 at 2:19 PM, Thorsten > >> Seitz<tseitz42@icloud.com > >> <mailto:tseitz42@icloud.com>(mailto:tseitz42@icloud.com > >> <mailto:tseitz42@icloud.com>)(mailto:tseitz42@icloud.com > >> <mailto:tseitz42@icloud.com>)>wrote:
>> > > >>>
>> > > >>>>Am 06.06.2016 um 19:43 schrieb Tim Vermeulen via > >> swift-evolution<swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)>:
>> > > >>>>
>> > > >>>>I also considered `until`, but it would be a bit confusing
>> that `where` makes sure a condition is met, while `until` makes sure
>> the condition isn’t met. I think `while` makes more sense because it
>> corresponds to `break` in the same way that `where` corresponds to
>> `continue`.
>> > > >>>
>> > > >>>That's a good argument! The only drawback is that `while` and
>> `where` look quite similar at a glance.
>> > > >>>
>> > > >>>-Thorsten
>> > > >>>
>> > > >>>>
>> > > >>>>>`while`, to me, actually reads like it should do what
>> `where` does.
>> > > >>>>
>> > > >>>>To me, `while` reads like it should stop the loop once the
>> condition isn’t met, just like in a while loop.
>> > > >>>>
>> > > >>>>>I hadn't thought about `while` in this regard but wouldn't
>> `until` make more sense? `while`, to me, actually reads like it
>> should do what `where` does. In any case, whether it is `while` or
>> `where`, this seems like a reasonable feature in my opinion.
>> > > >>>>>
>> > > >>>>>TJ
>> > > >>>>>
>> > > >>>>>On Mon, Jun 6, 2016 at 5:15 AM, Tim Vermeulen via
>> swift-evolution<swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@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@swift.org
>> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)
>> > > >>>>>>https://lists.swift.org/mailman/listinfo/swift-evolution
>> > > >>>>_______________________________________________
>> > > >>>>swift-evolution mailing list
>> > > >>>>swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)
>> > > >>>>https://lists.swift.org/mailman/listinfo/swift-evolution
>> > > >>
>> > > >>_______________________________________________
>> > > >>swift-evolution mailing list
>> > > >>swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>(mailto:swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)(mailto:
swift-evolution@swift.org
>> <mailto:swift-evolution@swift.org>)
>> > > >>https://lists.swift.org/mailman/listinfo/swift-evolution
>> > > >
>> > > >
>> > > >_______________________________________________
>> > swift-evolution mailing list
>> > swift-evolution@swift.org <mailto:swift-evolution@swift.org>
>> > https://lists.swift.org/mailman/listinfo/swift-evolution
>> >
>> >
>> >
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution@swift.org <mailto:swift-evolution@swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution