Yesterday, someone was asking me about how to do loops for non-linear sequences. Normally, you just do stuff like:
for i in 1...5 {print(i)} // 1, 2, 3, 4, 5
And stuff like:
for i in 1.0.stride(through: 2.01, by: 0.1) {
print(i) // 1.0, 1.1, ... 1.9, 2.0
}
for floating point. The 2.01 ensures you get that last value. You had to add some kind of epsilon when working with C-style for loops too because reasons and floating point math.

Ditto stride:

Leaving floating point aside, the question of the day had to do with non-linear sequences, for example, how do you do a loop for 1^-2 through 12^-2? Using map
applies a function across each value, offering a simple non-linear value sequence:

Doing math inside the for-in statement itself can quickly become ugly. It’s much nicer to map a named function across an integer sequence than implement the transformation directly in the for loop declaration.

Of course, once you separate out the function like this, you can throw in any function at all in place. Just swap out inverseSquare
with whatever function you’d prefer.
If your functionality depends on the count of iterations, for example doing some kind of trig across every 15°, then you’d want to pull out the count calculation, so it could be used both in the loop and the applied function, like this:

Update: Joe Groff writes, “1.0.stride(through: 2.01, by: 0.1) will accrue error as you repeatedly add 0.1000….02. You can avoid the ugly epsilon by iterating exact values and scaling down, say by 10.0.stride(through: 20.0, by: 1.0).lazy.map { $0 * 0.1 }, which will produce more accurate tenths.” If I were going to go that way, I think I’d prefer just using integer math and converting from there. Both solutions shown in the following screenshot.
