[swift-evolution] Feature proposal: Range operator with step
davesweeris at mac.com
davesweeris at mac.com
Fri Mar 25 09:43:56 CDT 2016
I don’t think you'd even need a new operator. This works with Ints (haven’t tried anything else):
extension Strideable {
func stride(by by: Self.Stride) -> (last: Self, by: Self.Stride) {
return (self, by)
}
}
func ..< <T: Strideable> (first: T, rhs: (last: T, by: T.Stride)) -> StrideTo<T> {
return first.stride(to: rhs.last, by: rhs.by)
}
func ... <T: Strideable> (first: T, rhs: (last: T, by: T.Stride)) -> StrideThrough<T> {
return first.stride(through: rhs.last, by: rhs.by)
}
Array(0..<10.stride(by: 2)) //[0, 2, 4, 6, 8]
Array(0...10.stride(by: 2)) //[0, 2, 4, 6, 8, 10]
> On Mar 24, 2016, at 3:40 AM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>
> If it's parentheses you want to avoid, you don't need a keyword to do that. For example, I can make a stride operator `..+` in four lines like so:
>
> ```
> infix operator ..+ { associativity left precedence 134 }
> func ..+ <Element: Strideable>(left: Range<Element>, right: Element.Stride) -> StrideTo<Element> {
> return left.startIndex.stride(to: left.endIndex, by: right)
> }
>
> // example of usage:
> for i in 1..<10..+2 {
> print(i)
> }
> ```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160325/f661671c/attachment.html>
More information about the swift-evolution
mailing list