vseloved / rutils

Radical Utilities for Common Lisp
Other
246 stars 36 forks source link

generic-elt with negative keys (list/vector) #47

Closed Kyo91 closed 4 years ago

Kyo91 commented 4 years ago

generic-elt on lists & vectors has special logic for handling negative keys:

(when (minusp key) (setf key (- (length obj) key)))

My understanding is that this is for "Python-style" access where we can access the last element of a list with:

my_list[-1]

However the above doesn't seem to work in practice:

RTL-USER> (? '(foo bar) -1)
NIL
RTL-USER> (? (make-array 10) -1)
;; Debugger: Invalid index 11 for (SIMPLE-VECTOR 10), should be a non-negative integer below 10.

Because the key is negative, generic-elt should be adding the key to the length rather than subtracting it (equivalent to adding the abs val of key).