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

William Dillon william at housedillon.com
Tue Mar 22 20:19:21 CDT 2016

> 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.

- Will

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

More information about the swift-evolution mailing list