diff options
| author | Nicolas Petton | 2016-06-18 09:42:09 +0200 |
|---|---|---|
| committer | Nicolas Petton | 2016-06-18 10:10:00 +0200 |
| commit | 2aebb0dd1fc66ba8cacef3f734e9a046cbc04ad2 (patch) | |
| tree | 148912479fccd2d13d3799b3d84a697c100a57c0 | |
| parent | 9726856f297522b1773e8aadaec34dd1a8e68a14 (diff) | |
| download | emacs-2aebb0dd1fc66ba8cacef3f734e9a046cbc04ad2.tar.gz emacs-2aebb0dd1fc66ba8cacef3f734e9a046cbc04ad2.zip | |
Add new function map-do
* lisp/emacs-lisp/map.el (map-do, map--do-alist, map--do-array): New
functions.
* test/lisp/emacs-lisp/map-tests.el: Add a unit test for map-do.
| -rw-r--r-- | lisp/emacs-lisp/map.el | 26 | ||||
| -rw-r--r-- | test/lisp/emacs-lisp/map-tests.el | 8 |
2 files changed, 33 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el index b97d8b1ca64..7c4afb91304 100644 --- a/lisp/emacs-lisp/map.el +++ b/lisp/emacs-lisp/map.el | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | ;; Author: Nicolas Petton <nicolas@petton.fr> | 5 | ;; Author: Nicolas Petton <nicolas@petton.fr> |
| 6 | ;; Keywords: convenience, map, hash-table, alist, array | 6 | ;; Keywords: convenience, map, hash-table, alist, array |
| 7 | ;; Version: 1.0 | 7 | ;; Version: 1.1 |
| 8 | ;; Package: map | 8 | ;; Package: map |
| 9 | 9 | ||
| 10 | ;; Maintainer: emacs-devel@gnu.org | 10 | ;; Maintainer: emacs-devel@gnu.org |
| @@ -201,6 +201,16 @@ MAP can be a list, hash-table or array." | |||
| 201 | function | 201 | function |
| 202 | map)) | 202 | map)) |
| 203 | 203 | ||
| 204 | (defun map-do (function map) | ||
| 205 | "Apply FUNCTION to each element of MAP and return nil. | ||
| 206 | FUNCTION.is called with two arguments, the key and the value." | ||
| 207 | (funcall (map--dispatch map | ||
| 208 | :list #'map--do-alist | ||
| 209 | :hash-table #'maphash | ||
| 210 | :array #'map--do-array) | ||
| 211 | function | ||
| 212 | map)) | ||
| 213 | |||
| 204 | (defun map-keys-apply (function map) | 214 | (defun map-keys-apply (function map) |
| 205 | "Return the result of applying FUNCTION to each key of MAP. | 215 | "Return the result of applying FUNCTION to each key of MAP. |
| 206 | 216 | ||
| @@ -354,6 +364,20 @@ MAP can be a list, hash-table or array." | |||
| 354 | (setq index (1+ index)))) | 364 | (setq index (1+ index)))) |
| 355 | map))) | 365 | map))) |
| 356 | 366 | ||
| 367 | (defun map--do-alist (function alist) | ||
| 368 | "Private function used to iterate over ALIST using FUNCTION." | ||
| 369 | (seq-do (lambda (pair) | ||
| 370 | (funcall function | ||
| 371 | (car pair) | ||
| 372 | (cdr pair))) | ||
| 373 | alist)) | ||
| 374 | |||
| 375 | (defun map--do-array (function array) | ||
| 376 | "Private function usde to iterate over ARRAY using FUNCTION." | ||
| 377 | (seq-do-indexed (lambda (elt index) | ||
| 378 | (funcall function index elt)) | ||
| 379 | array)) | ||
| 380 | |||
| 357 | (defun map--into-hash-table (map) | 381 | (defun map--into-hash-table (map) |
| 358 | "Convert MAP into a hash-table." | 382 | "Convert MAP into a hash-table." |
| 359 | (let ((ht (make-hash-table :size (map-length map) | 383 | (let ((ht (make-hash-table :size (map-length map) |
diff --git a/test/lisp/emacs-lisp/map-tests.el b/test/lisp/emacs-lisp/map-tests.el index 20cb0f6b399..0af1c656e09 100644 --- a/test/lisp/emacs-lisp/map-tests.el +++ b/test/lisp/emacs-lisp/map-tests.el | |||
| @@ -192,6 +192,14 @@ Evaluate BODY for each created map. | |||
| 192 | (2 . b) | 192 | (2 . b) |
| 193 | (3 . c)))))) | 193 | (3 . c)))))) |
| 194 | 194 | ||
| 195 | (ert-deftest test-map-do () | ||
| 196 | (with-maps-do map | ||
| 197 | (let ((result nil)) | ||
| 198 | (map-do (lambda (k v) | ||
| 199 | (add-to-list 'result (list (int-to-string k) v))) | ||
| 200 | map) | ||
| 201 | (should (equal result '(("2" 5) ("1" 4) ("0" 3))))))) | ||
| 202 | |||
| 195 | (ert-deftest test-map-keys-apply () | 203 | (ert-deftest test-map-keys-apply () |
| 196 | (with-maps-do map | 204 | (with-maps-do map |
| 197 | (should (equal (map-keys-apply (lambda (k) (int-to-string k)) | 205 | (should (equal (map-keys-apply (lambda (k) (int-to-string k)) |