[swift-evolution] A (better) Swift Equivalent For The Classical For-Loop With Numeric Scalars

Dany St-Amant dsa.mls at icloud.com
Wed Mar 23 21:56:34 CDT 2016


> Le 23 mars 2016 à 21:59, William Dillon <william at housedillon.com> a écrit :
> 
>> 
>> On Mar 23, 2016, at 6:46 PM, Dany St-Amant via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 
>>> Le 22 mars 2016 à 21:19, William Dillon <william at housedillon.com <mailto:william at housedillon.com>> a écrit :
>>> 
>>>> 
>>>> These may be compact, but some programmer may fell that they are not in control: do these "stdlib" seq(), c.filter() pre-calculate all the entries (wasting precious cpu cycle if one break out early)  or are each value dynamically created? Having an explicit legacy loop format gives a feel of control. So something like
>>>> 
>>>> for i from 2 to 1_000_000 by 1 where i % 2 != 0 while foundCount < 5 { print(i); foundCount +=1 }
>>>> 
>>>> sounds less magical and seems easier to predict than
>>>> 
>>>> for i in 2.stride(to:1_000_000, by:1).filter({ $0 % 2 != 0}).prefix(5) { print(i) }
>>>> 
>>>> and is still quite readable, even if it mixes for loop, while loop and even simple condition.
>>>> 
>>> 
>>> I disagree.  I think the first case is venturing dangerously close to the AppleScript “uncanny valley” of human language-like programming verbs where I never know the exact words and exact order for things.  The second example is right out of any functional programming mold, and I fully understand what’s happening, and how to decompose the process to test assumptions.  Also, I know how to implement the second case (more or less) from scratch, and that’s pretty huge for me.
>>> 
>> 
>> There have been multiple fights over too verbose versus too terse, and on whether or not making it sound like English. I would agree a verbose syntax for a mandatory statement is a pain, but the while and where above are optional. And using them instead of relying on continue or break allow for code intent to be bit clearer.
>> 
>> Reusing my bad example, without the from/to/by:
>> 
>> for i in 2.stride(to: 1_000_000, by: 1)
>> {
>> 	if i % 2 != 0 { continue }
>> 	print(i)
>> 	foundCount += 1
>> 	if foundCount >= 5 { break }
>> }
>> 
>> // Where clause already work in current Swift
>> for i in 2.stride(to: 1_000_000, by: 1)
>> where i % 2 == 0
>> {
>> 	print(i)
>> 	foundCount += 1
>> 	if foundCount >= 5 { break }
>> }
>> 
>> // While do not exist yet in this context
>> for i in 2.stride(to: 1_000_000, by: 1)
>> where i % 2 == 0
>> while foundCount < 5
>> {
>> 	print(i)
>> 	foundCount += 1
>> }
>> 
> 
> The ‘where’ I can tolerate because there are other examples of such a construct in other parts of the language.  The while part is still problematic, because where have you declared it?  Do you think declaring a variable for that somewhere outside the context of the loop is cleaner than leaving it entirely within that scope then doing a test and break?

Usually when a loop exit early with a break, which would make sense to use with the while construct, such loop is followed by a if statement against that condition, so it make perfect sense to declare it i the outer scope.

// While do not exist yet in this context
for i in 2.stride(to: 1_000_000, by: 1)
where i % 2 == 0
while foundCount < 5
{
	print(i)
	foundCount += 1
}

if foundCount != 5
{
	print("Sorry not enough element for the quintuplet!.")
	// return or throw... we don’t want to continue.
}

Anyway, you cannot declare the above foundCount inside the loop, as it would get re-initialized on every iteration.
The for/in/while is not a replacement for some multiple variables loop: for(x=0,y=1;…;…) but  for some dual comparison loop (max and early exit): for(…;i<max && !found;…)

Dany

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160323/b69792bb/attachment.html>


More information about the swift-evolution mailing list