Arc Forumnew | comments | leaders | submitlogin
2 points by fallintothis 5538 days ago | link | parent

  arc> (ssexpand 'xs.-1)
  (xs -1)
To model indexing after Python, valid indices for a sequence of length n would be integers in the range [-n, n).

  >>> xs = ['a', 'b', 'c']
  >>> xs[0] is xs[3]
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  IndexError: list index out of range
  >>> xs[0] is xs[-6]
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  IndexError: list index out of range
  >>> xs[0] is xs[-3]
  True


1 point by evanrmurphy 5538 days ago | link

Glad the 'ssexpand works, I should have tried that.

Python's indexing doesn't wrap around infinitely, but that doesn't mean Arc's couldn't.

-----

1 point by fallintothis 5538 days ago | link

Hence "To model indexing after Python". I figure Python's probably a good source of inspiration for Python-like features. ;)

Admittedly, I can't find specific rationale for IndexErrors discussed anywhere. Perhaps throwing errors for egregious indices just seems the "sane" thing to do. Essentially, it's a choice of foist: if indexing wraps around infinitely, you need to check bounds when you don't want to wrap around; if indexing throws errors, you need to mod explicitly when you do want to wrap around.

-----

1 point by evanrmurphy 5538 days ago | link

> if indexing wraps around infinitely, you need to check bounds when you don't want to wrap around; if indexing throws errors, you need to mod explicitly when you do want to wrap around.

Good summary. I agree that index-out-of-bound errors are the saner default.

-----