stride() over ranges

Aug 13, 2014 00:00 · 83 words · 1 minute read swift

Ranges in Swift grew a new function with Xcode 6 Beta 5. Now it’s possible to stride() over a range, that is to say, step through it in arbitrary increments, which I can see being pretty useful.

Consider

[peacock@trashcan ~]$ swift
Welcome to Swift!  Type :help for assistance.
  1> for x in stride(from: 1, to: 10, by: 0.50) { println(x) }
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
  2> :quit
[peacock@trashcan ~]$