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

Ted F.A. van Gaalen tedvgiosdev at gmail.com
Wed Mar 30 15:05:00 CDT 2016


I still would like to see this new for-loop statement be implemented in the next Swift version

 Examples:
     for v from 0.5 to 30.0 by 0.3    // floating point types
     for v from 0 to 100 by 5           // Integer
     for v from 12.0 to -10.0 by -2  // Floating points backward 

the “by …” clause is optional for Ints only 


As previously written, a tolerance factor could also be implemented as optional,
allowing to “end on a humanly accepted boundary” so like in this example
the highest loop value would be 10.0 (+/- ca. 0.000000001) , not 9.9 :  
  
 for v from 0.0 to 10.0 by 0.1 tolerance 0.001     

// the “tolerance ..” clause is optional and allowed for floating point vars only


Again I need to emphasize very strongly that this for-loop really 
has absolutely 
     nothing, nada, zilch, niente, nichts, niets, niks, rien, zero, nenio,
to do with the: 

for i in stride(…. 

or any other for in… variant working with collections.

However inexplicably, in the previous discussions, a lot of people ***
tried desperately to replace this simple but precious gem, 
a miracle of astonishing beauty:           (sorry, got carried away a bit :o) 
  
    for v from v1 to v2 by vstep 
 
with the collection based 

for in ….

The for in… is wonderful for collection based iterations, I use it 
all the time like
    for thing in things // etc.



for d in 10.0.stride(to: 5.0, by: -0.1)
{
    print(d)
}


but, once again - 
provided you don’t want to do other operations
on the generated collection before iterating -  
a collection is used here totally unnecessary, 
which puts a burden on performance because the contents 
of a collection are unpredictable 
It is also tedious to write and (as a matter of my personal taste) downright ugly.

Imho this looks a whole lot better and can also be very efficiently compiled: 

for d from 10.0 to 5.0 by -0.1 tolerance 0.01
{
    print(d)
}


  **************************** !!! **************************************
  Important is to see that this “for…”  is in fact
  a convenience solution for  “while” and “repeat” constructs: 
  and thus a totally different beast compared to the   "for in…” ! 
  *********************************************************************
var d = 10.0   
var v = 0.0
let step = 0.1

while d > 5.0     
{ 
    print(d)
    d -= step
}


The above is a bare minimum “while” equivalent
for the above for-loop, but still tedious to write!

What more can I write to convince? 


*** Please tell me if I am wrong, but: 
I am inclined to think that Functional Programming Minded
Colleagues are trying to push persistently their 
(mathematically correct?)  way of thinking upon Swift, 
thereby ignoring that Swift is and should remain a 
general purpose programming language.


TedvG
www.ravelnotes.com

@Erica,

Erica, as I seem to remember, You wrote somewhere
that Stride is broken, but the "10.0.stride…” example
above works perfectly well in Swift 2.2. playground.

So, what do you regard as wrong with it?
(apart from in some cases needing to specify an 
epsilon(tolerance) value? ) 



  














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


More information about the swift-evolution mailing list