[swift-evolution] C-style For Loops

Per Melin p at greendale.se
Sat Dec 5 07:09:06 CST 2015


On Sat, Dec 5, 2015 at 11:10 AM, Matthijs Hollemans <mail at hollance.com>
wrote:

>
>
Another example from the same LinkedList class. It finds the right place to
> insert a new node:
>
>   for next = head; next != nil && index > 0; prev = next, next = next!.
> next, --index { }
>
> Extreme? Probably, but I like it better than the same thing done in five
> lines of while loop.
>

next = head; while next != nil && index > 0 { prev = next; next =
next!.next; --index }

A 'for' statement gets away with a single line only because it substitutes
semicolons and commas for line breaks. You can do the same with 'while'.

But as Joe pointed out, you can recreate an almost identical 'for'
with autoclosures
anyway.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/03a80003/attachment.html>


More information about the swift-evolution mailing list