diff options
| -rw-r--r-- | lisp/emacs-lisp/map.el | 13 | ||||
| -rw-r--r-- | test/automated/map-tests.el | 6 |
2 files changed, 17 insertions, 2 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el index 3984b08c44e..ebf1fe9589a 100644 --- a/lisp/emacs-lisp/map.el +++ b/lisp/emacs-lisp/map.el | |||
| @@ -50,7 +50,7 @@ If KEY is not found, return DEFAULT which defaults to nil. | |||
| 50 | 50 | ||
| 51 | If MAP is a list, `equal' is used to lookup KEY." | 51 | If MAP is a list, `equal' is used to lookup KEY." |
| 52 | (map--dispatch map | 52 | (map--dispatch map |
| 53 | :list (or (cdr (assoc key map)) default) | 53 | :list (map--elt-list map key default) |
| 54 | :hash-table (gethash key map default) | 54 | :hash-table (gethash key map default) |
| 55 | :array (map--elt-array map key default))) | 55 | :array (map--elt-array map key default))) |
| 56 | 56 | ||
| @@ -253,8 +253,17 @@ form. | |||
| 253 | (setq index (1+ index)))) | 253 | (setq index (1+ index)))) |
| 254 | map))) | 254 | map))) |
| 255 | 255 | ||
| 256 | (defun map--elt-list (map key &optional default) | ||
| 257 | "Return the element of the list MAP at the index KEY. | ||
| 258 | If KEY is not found, return DEFAULT which defaults to nil." | ||
| 259 | (let ((pair (assoc key map))) | ||
| 260 | (if pair | ||
| 261 | (cdr (assoc key map)) | ||
| 262 | default))) | ||
| 263 | |||
| 256 | (defun map--elt-array (map key &optional default) | 264 | (defun map--elt-array (map key &optional default) |
| 257 | "Return the element of the arary MAP at the index KEY, or DEFAULT if nil." | 265 | "Return the element of the array MAP at the index KEY. |
| 266 | If KEY is not found, return DEFAULT which defaults to nil." | ||
| 258 | (let ((len (seq-length map))) | 267 | (let ((len (seq-length map))) |
| 259 | (or (and (>= key 0) | 268 | (or (and (>= key 0) |
| 260 | (<= key len) | 269 | (<= key len) |
diff --git a/test/automated/map-tests.el b/test/automated/map-tests.el index 5201116613e..e65af894275 100644 --- a/test/automated/map-tests.el +++ b/test/automated/map-tests.el | |||
| @@ -66,6 +66,12 @@ Evaluate BODY for each created map. | |||
| 66 | (with-maps-do map | 66 | (with-maps-do map |
| 67 | (assert (= 5 (map-elt map 7 5))))) | 67 | (assert (= 5 (map-elt map 7 5))))) |
| 68 | 68 | ||
| 69 | (ert-deftest test-map-elt-with-nil-value () | ||
| 70 | (assert (null (map-elt '((a . 1) | ||
| 71 | (b)) | ||
| 72 | 'b | ||
| 73 | '2)))) | ||
| 74 | |||
| 69 | (ert-deftest test-map-put () | 75 | (ert-deftest test-map-put () |
| 70 | (with-maps-do map | 76 | (with-maps-do map |
| 71 | (map-put map 2 'hello) | 77 | (map-put map 2 'hello) |