summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/map.el
diff options
context:
space:
mode:
authorNicolas Petton <nicolas@petton.fr>2015-04-24 19:29:59 +0200
committerNicolas Petton <nicolas@petton.fr>2015-04-24 19:29:59 +0200
commitd75151a671dcdc1cac8c6ab1a47520bae4872d70 (patch)
tree0b680ef21bb81cf0f3e0bce37426545f5f1d056b /lisp/emacs-lisp/map.el
parent79d9757c2334364a78a2e40b75d8d4e96161a911 (diff)
downloademacs-d75151a671dcdc1cac8c6ab1a47520bae4872d70.tar.gz
Do not signal an error when trying to delete a key from an array
* lisp/emacs-lisp/map.el (map-delete): When map is an array, check if the key is present to avoid signaling an error. * test/automated/map-tests.el: Add a test for deleting non-existing keys from maps.
Diffstat (limited to 'lisp/emacs-lisp/map.el')
-rw-r--r--lisp/emacs-lisp/map.el11
1 files changed, 9 insertions, 2 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 621c37f2b76..087ab286ac3 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -73,7 +73,7 @@ If MAP is an array, store nil at the index KEY."
(map--dispatch (m ,map m)
:list (setq ,map (map--delete-alist m ,key))
:hash-table (remhash ,key m)
- :array (aset m ,key nil))))
+ :array (map--delete-array m ,key))))
(defun map-nested-elt (map keys &optional default)
"Travserse MAP using KEYS and return the looked up value or DEFAULT if nil.
@@ -261,13 +261,20 @@ form.
(seq-elt map key))
default)))
-
(defun map--delete-alist (map key)
"Return MAP with KEY removed."
(seq-remove (lambda (pair)
(equal key (car pair)))
map))
+(defun map--delete-array (map key)
+ "Set nil in the array MAP at the index KEY if present and return MAP."
+ (let ((len (seq-length map)))
+ (and (>= key 0)
+ (<= key len)
+ (aset m key nil)))
+ map)
+
(defun map--into-hash-table (map)
"Convert MAP into a hash-table."
(let ((ht (make-hash-table :size (map-length map)