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

Taras Zakharko taras.zakharko at uzh.ch
Mon Mar 21 08:30:28 CDT 2016


To be honest, I have difficulty understanding the premise of this discussion. I have always found the C-style  for(;;) loop an awkward construct, because it is essentially just an alternative form of writing a general-purpose while loop.  It is perfectly reasonable to restrict the for construct to, well, ‘for’ contexts: iterating through a sequence of items. If you need something more complex, either write your own iterator or use a general-purpose while loop.  

As discussed previously, an optimising compiler generates the same machine code whether you are using an iterator abstraction or low-level coding. Besides, the for .. in .. construct potentially offers more potential for optimisation, because it makes the counter variable as well as the semantics of the entire loop more explicit (a for(;;) can contain arbitrary stuff). Talking about numerical algorithms: this is something I can’t understand at all. I have been working with numerical algorithms in Python and R for years, and I have never ever missed the C-style for loop. These languages offer convenient functions for generating iterable sequences, which are more readable and also represent the loop  semantics much better then the for(;;;). Not to mention that the iterator pattern allows you to write simpler and more maintainable code. Imagine iterating through a sparse matrix or something. With a proper iterator pattern you just do:

for (i,j,k) in matrix.indices {

}

With a for(;;) loop you’l probably end up writing a nested construct of quite some complexity. 
 
Swift could certainly use some improvements to its iterators (for example something like Python generator expressions might be useful), but that is a different topic.  

To sum it up:
- for(;;) is an awkward construct which is closer to a while loop than an actual sequence iterator pattern (that it should be)
- a proper iterator pattern is more convenient, more readable and offers the same performance
- numerical code does not need a for(;;) construct, in fact, using the proper iterator pattern makes writing numeric code easier, not more difficult, because you can abstract away some common processing/aggregating steps easily, and without any drawback
- we should think about how to improve the iterators offered by Swift standard library, not go back to an inferior construction

— Taras



> On 21 Mar 2016, at 12:37, Greg Parker via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On Mar 19, 2016, at 12:46 AM, Dmitri Gribenko via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Hi Ted,
>> 
>> Thank you for starting this thread.  I agree that removing the C-style
>> for loop has degraded the readability and clarity of some of numerics
>> code.
>> 
>> In the feedback to SE-0007 many people have said that they can convert
>> their code to for-in loops, but I think this actually means that in
>> code that is typically written in Swift today, loops primarily operate
>> on sequences and collections.  It means that numerics is a domain that
>> not many people work in.  But it is a very important domain
>> nevertheless, and clarity for numerics code matters at least as much
>> as it does everywhere else.
>> 
>> I think one way to approach this discussion would be to present
>> multiple concrete code samples that contain C-style for loops and are
>> awkward to write without them.  We can then try looking for patterns,
>> generalize and simplify, and discuss possible solutions.
> 
> Let me emphasize this more strongly. *Concrete*, *real-world* examples are quite likely the only way that you are going to get `for(;;)` back or get any sort of semantically-similar syntactically-different replacement.
> 
> There have been lots of suggestions. None of them are perfect. If we assume that there is in fact no perfect solution then the only way to proceed is to provide sufficient justification for some imperfect solution.
> 
> I'm still waiting for something like this: "We ported our code to Swift 3. Here are 5 reasonable-looking for(;;) loop shapes from 150 call sites, and here are their ugly-looking rewrites."
> 
> (Personally I think removing for(;;) without direct replacement was too aggressive. That view lost. Now its advocates will need to do more work to upend the status quo.)
> 
> 
> -- 
> Greg Parker     gparker at apple.com <mailto:gparker at apple.com>     Runtime Wrangler
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160321/945509ed/attachment.html>


More information about the swift-evolution mailing list