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.
This commit is contained in:
Nicolas Petton 2015-04-24 19:29:59 +02:00
parent 79d9757c23
commit d75151a671
2 changed files with 13 additions and 3 deletions

View file

@ -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)