# [swift-evolution] [DRAFT] Regularizing Where Grammar (was Re: Add a while clause to for loops)

Christopher Kornher ckornher at me.com
Fri Jun 10 17:44:41 CDT 2016

I wonder how reactionary I will become when Swift is 5 years old :)

I goggled a bit, to get some context on the use of “where” in mathematics. I was surprised to learn that and the use of “where” in mathematics is disliked by some mathematicians for its lack of precision:

In his famous paper How to write mathematics <http://www.math.uh.edu/~tomforde/Books/Halmos-How-To-Write.pdf>, P.R. Halmos says the following about "where"

"Where" is usually a sign of a lazy afterthought that should have been thought through before.
From http://math.stackexchange.com/questions/1304270/math-symbol-for-where <http://math.stackexchange.com/questions/1304270/math-symbol-for-where>

So you may in good company in your concerns about the use of this word.

Taking a clue from this, perhaps a less vague term would clarify things for loops:

Current ‘where’ syntax

```
for i in arr where i%2 == 0 {
print( i )
}
```

can be rewritten (using the same variable name in the closure):

```
for i in arr.filter({(i) in i%2 == 0 }) {
print( i )
}
```

There area many extra characters in the second version. ‘i’ is already bound, so the binding (if that is the correct term) in the filter clause doesn’t add any information. The non-escaping closure requires a set of parens and a set of braces.

What if a new keyword is introduced as a shorthand for using ‘filter’ in this way:

```
for i in arr filteredby i%2 == 0 {
print( i )
}
```

or perhaps, keeping the closure syntax and reusing the variable binding:

```
for i in arr.filteredby{ i%2 == 0 } {
print( i )
}
```

> On Jun 10, 2016, at 1:08 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> On Jun 10, 2016, at 1:06 PM, Rob Norback via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> Following Brent's logic that the for-in where should mimic the switch statement functionality, then this example:
>>
>> for (eachKey, eachValue)
>> where eachValue > 5
>> in theKeyValuePairs {... }
>>
>
> <squish>
>
> I finally convinced myself of which direction I wanted to go: https://github.com/apple/swift-evolution/pull/362/files
>
> Related blog post here: http://ericasadun.com/2016/06/10/swift-where-oh-where-can-my-where-clause-be/
>
> Big thanks to Brent and Wux.
>
> -- E
>
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

> On Jun 10, 2016, at 1:08 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> On Jun 10, 2016, at 1:06 PM, Rob Norback via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> Following Brent's logic that the for-in where should mimic the switch statement functionality, then this example:
>>
>> for (eachKey, eachValue)
>> where eachValue > 5
>> in theKeyValuePairs {... }
>>
>
> <squish>
>
> I finally convinced myself of which direction I wanted to go: https://github.com/apple/swift-evolution/pull/362/files
>
> Related blog post here: http://ericasadun.com/2016/06/10/swift-where-oh-where-can-my-where-clause-be/
>
> Big thanks to Brent and Wux.
>
> -- E
>
>
>
>
> _______________________________________________
> 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/20160610/a6133dea/attachment.html>