summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/map.el
diff options
context:
space:
mode:
authorNicolas Petton <nicolas@petton.fr>2015-04-24 19:06:27 +0200
committerNicolas Petton <nicolas@petton.fr>2015-04-24 19:10:45 +0200
commitf37e265ea992f5799f1bf30a03509444c976df1d (patch)
tree18946d61e195148201b9aa18e6e6550a6bd8dd53 /lisp/emacs-lisp/map.el
parent89baf163324c6820ca17e91cda9dc8b162a59eab (diff)
downloademacs-f37e265ea992f5799f1bf30a03509444c976df1d.tar.gz
Minor improvement in map-elt.
* lisp/emacs-lisp/map.el (map-elt): Do not use `ignore-errors' when doing a lookup in arrays, but check the boundaries of the array instead. * test/automated/map-tests.el: Adds a test for `map-elt' with arrays and a negative integer as key.
Diffstat (limited to 'lisp/emacs-lisp/map.el')
-rw-r--r--lisp/emacs-lisp/map.el13
1 files changed, 11 insertions, 2 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 06fd7ad2957..2c95f35569c 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -48,11 +48,11 @@
"Perform a lookup in MAP of KEY and return its associated value.
If KEY is not found, return DEFAULT which defaults to nil.
-If MAP is a list, `assoc' is used to lookup KEY."
+If MAP is a list, `equal' is used to lookup KEY."
(map--dispatch map
:list (or (cdr (assoc key map)) default)
:hash-table (gethash key map default)
- :array (or (ignore-errors (elt map key)) default)))
+ :array (map--elt-array map key default)))
(defmacro map-put (map key value)
"In MAP, associate KEY with VALUE and return MAP.
@@ -252,6 +252,15 @@ form.
(setq index (1+ index))))
map)))
+(defun map--elt-array (map key &optional default)
+ "Return the element of the arary MAP at the index KEY, or DEFAULT if nil."
+ (let ((len (seq-length map)))
+ (or (and (>= key 0)
+ (<= key len)
+ (seq-elt map key))
+ default)))
+
+
(defun map--delete-alist (map key)
"Return MAP with KEY removed."
(seq-remove (lambda (pair)