Skip to content

Ranges with strides #174

@apblack

Description

@apblack

I've long found it annoying that, while we have a nice notation for ascending ranges,

1 .. 4

we don't have one for descending ranges like [4, 3, 2, 1]. No, we can't allow 4..1, because that's an empty range. (1..4).reversed works, but is long-winded. range.from 4 downTo 1 is the primitive constructor, and it's even longer-winded.

A possible soution is to devise a general notation for "strided ranges" (or should it be "stridden" ranges?) For example

1 .. 4 .. 1    => same as 1 .. 4, because 1 is the default stride
1 .. 10 .. 2  => [1, 3, 5, 7, 9]: a stride of 2, starting at 1 and continuing until the next element would exceed the second bound
4 .. 1 .. -1  =>  [4, 3, 2, 1]: a stride of -1, starting at 4 and continuing until the next element would be less than the second bound

This could be implemented pretty easily by defining the .. operator on ranges.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions