News for hackers. Stuff that matters.
Re:Swift Programming Language Evolution by andybak on Monday May 09, 2016 @12:48PM (#11661211)
With regard to C-style for loops - other than familiarity - why?

It always struck me as a fairly warty syntax.

Consider Python where you usually do:

    for item in iterator
If you want an integer sequence it's:

   for integer in range(1, 100)
and for those rare occurrences where you need an integer index as well:

    for index, item in enumerate(iterator) 
Swift seems to have a similar approach. Why would you ever want the C-style iterators?
Re:Swift Programming Language Evolution by DonFromWyoming on Monday May 09, 2016 @01:55PM (#11661736)
For the simple case with a single iterator, yes the C-style for loop isn't as concise. However, there are lots of real-world situations where there is not a single iterator (e.g. looping through 2 arrays). Having to go back to 'while(boolean) { }' loops with initializers outside of the loop and incrementors at strange places inside the loop is much more confusing and error prone.
Re:Swift Programming Language Evolution by joewillsher on Monday May 09, 2016 @02:09PM (#11661853)
You can use the zip function to loop through 2 collections.

`for (l, r) in zip(c1, c2) {`

It is not simply motivated by making code concise, I would say `for num in collection.reverse()` is less error prone and clearer than `for (var i = collection.count; i >= 0; i--) { var num = collection[i] ... }`

The reverse collection iterator is computed lazily too, so there is no little perf overhead

Re:Swift Programming Language Evolution by bluecalm on Monday May 09, 2016 @06:07PM (#11663615)
Sometimes having those counting variables available is needed. You can use enumerate now but it introduces a lot of noise as now everything is a tuple with a number. It just makes expressing some algorithms messier and less readable although in general for sure "new style" loop is more readable and less bug prone.