Julia: document range types syntax and semantics

Created on 16 May 2016  路  5Comments  路  Source: JuliaLang/julia

Range's and the : notation are mentioned in passing in several spots in the documentation, but it doesn't seem there's any thorough presentation about them.

Compare Python's documentation Slicings in 6.3.3 of https://docs.python.org/3/reference/expressions.html#primaries and the discussion of : in sequences at https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range and https://docs.python.org/3/library/functions.html#func-range

Also, x:y:z has step y in julia but z in python. Maybe julia should match python? The change would be bad for old code, but might prevent errors for people coming from python.

Possible topics:

  • Behavior if types of x, y, z differ.
  • If y
  • But if y<0 then x:y:z generates a descending range if z
  • Precedence of : relative to other operators.
  • Numerical issues when z-x is apparently an integer multiple of y but is not. I would expect there would be circumstances in which z would not be an element of the generated set, but I can't generate any examples!
  • Circumstances under which ranges can be used in place of vector/array/tuple types, and how to convert to same.
doc

Most helpful comment

Also, x:y:z has step y in julia but z in python. Maybe julia should match python? The change would be bad for old code, but might prevent errors for people coming from python.

Julia uses the MATLAB/Octave convention for range steps. So you have to pick which group of users have to adjust. Since the rest of our range syntax follows from MATLAB as well (end, one-based indexes (at least for normal everyday arrays)) this is unlikely to be changed.

All 5 comments

Also, x:y:z has step y in julia but z in python. Maybe julia should match python? The change would be bad for old code, but might prevent errors for people coming from python.

Julia uses the MATLAB/Octave convention for range steps. So you have to pick which group of users have to adjust. Since the rest of our range syntax follows from MATLAB as well (end, one-based indexes (at least for normal everyday arrays)) this is unlikely to be changed.

I agree that's a good reason to leave it as from:step:end. You can also add R to the have to adjust group, since they have seq(from, end, step).

@tkelman thanks for the tag. I need to find out how to add them.

The main issue is the documentation.

Would you make a pull request? Other than that, it's unlikely to happen soon.

Ranges using DateTime are also undocumented. I'll note that they are rather special as the step is a different type:

julia> DateTime(2015):Dates.Day(1):DateTime(2016)
2015-01-01T00:00:00:1 day:2016-01-01T00:00:00

1帽

Was this page helpful?
0 / 5 - 0 ratings