JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.55k stars 5.47k forks source link

Suggesting new syntax: a..b #8949

Closed xianrenb closed 9 years ago

xianrenb commented 9 years ago

I would suggest that new syntax "a..b" represents "[a, a+1, a+2, ... , b]". The followings should then become true: "1..5" represents "[1, 2, 3, 4, 5]". "reverse(1..5)" represents "[5, 4, 3, 2, 1]". "1..99[1:2:end]" represents "[1, 3, 5, 7, ..., 99]".

Related issue: #3737

pao commented 9 years ago

Ranges can already do exactly those things. See http://nbviewer.ipython.org/gist/pao/dbd0a405a56afd6dbfa2

(and a special thanks to http://tmpnb.org for making this example straightforward to create)

xianrenb commented 9 years ago

This is exactly the problem. Ranges are not the same as arrays! I believe we need to be able to create array of ranges in a concise way. For example, "[1:5, 1:3]" should be an array of two ranges: "1 to 5" and "1 to 3". Range(s) between "[" and "]" should still be range(s). Currently, we could convert range to array: "convert(Array, 1:5)" would give "[1, 2, 3, 4, 5]". "[1:5]" should not equal "[1, 2, 3, 4, 5]".

StefanKarpinski commented 9 years ago

There are already several issues discussing this, which you have linked to – why open a duplicate issue intentionally?

xianrenb commented 9 years ago

Simple, because this is a new syntax. It may or may not be related to other issues.

johnmyleswhite commented 9 years ago

New syntax is not something that should be added lightly.

lindahua commented 9 years ago

You can create an array of ranges using

UnitRange[1:3, 1:5]
JeffBezanson commented 9 years ago

Dense range vectors should be avoided, so I don't think they deserve special syntax. Something like collect(a:b) is enough.