[swift-evolution] Feature proposal: Range operator with step
brent at architechies.com
Fri Apr 8 04:43:33 CDT 2016
> At the risk of asking one of those newbie questions, why bother with StrideTo and StrideThrough? Isn't a Generator or Array more to the point?
You don't want to return an Array because you want to generate the values lazily. If `stride(over: 1..<1_000_000, by: 10)` returned an Array, you would have to allocate an array with 100,000 elements. A StrideTo, by contrast, is the size of roughly 3 elements; it creates the values on demand.
You can't return Generator because Generator is a protocol with no real behavior associated with it; you need a concrete type. StrideTo and StrideThrough conform to Sequence, a protocol whose main purpose is to return a Generator. (well, Iterator in Swift 3). So in essence, StrideTo and StrideThrough *are* how you return a Generator.
More information about the swift-evolution