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
> <[email protected]> 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)
> }
> ```
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution