Non-contiguous range?

Is there a way to create non-contiguous range with series notation? Like 1..3, 10..13 to produce [1, 2, 3, 10, 11, 13] ?

Enum.concat(1..3, 10..13)

8 Likes

By definition:

Ranges represent a sequence of zero, one or many, ascending or descending integers
with a common difference called step.

So you cannot have a Range where the step varies, i.e. between 2…3 is a step of 1, while 3…10 is a step of 7.

If you want to make a List, then I would agree with @wojtekmach.

1 Like

And you can even make it lazy!

Stream.concat(1..3, 10..113)
5 Likes

These are elegant! Thank you!

In both examples the “step” is 1.

The “step” is the difference between the current and the next element of a range.

So 1..10//2 will contain 1, 3, 5, 7, and 9.

I didn’t mean to imply that those two examples are Ranges… I just used an ellipsis between them out of habit from prose and pure math. I wanted to say that between 2 and 3 (as in 1..3) is a step of 1, while between 3 and 10 (as in the imaginary “range” 1..3,10..13 from the original question) would have a step of 7, and therefore the definition of a Range would be violated.

Sorry for the lack of clarity in my first post.

1 Like