summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/map.el
diff options
context:
space:
mode:
authorNicolas Petton <nicolas@petton.fr>2016-06-18 09:42:09 +0200
committerNicolas Petton <nicolas@petton.fr>2016-06-18 10:10:00 +0200
commit2aebb0dd1fc66ba8cacef3f734e9a046cbc04ad2 (patch)
tree148912479fccd2d13d3799b3d84a697c100a57c0 /lisp/emacs-lisp/map.el
parent9726856f297522b1773e8aadaec34dd1a8e68a14 (diff)
downloademacs-2aebb0dd1fc66ba8cacef3f734e9a046cbc04ad2.tar.gz
emacs-2aebb0dd1fc66ba8cacef3f734e9a046cbc04ad2.tar.bz2
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.
Diffstat (limited to 'lisp/emacs-lisp/map.el')
-rw-r--r--lisp/emacs-lisp/map.el26
1 files changed, 25 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 @@
;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: convenience, map, hash-table, alist, array
-;; Version: 1.0
+;; Version: 1.1
;; Package: map
;; Maintainer: emacs-devel@gnu.org
@@ -201,6 +201,16 @@ MAP can be a list, hash-table or array."
function
map))
+(defun map-do (function map)
+ "Apply FUNCTION to each element of MAP and return nil.
+FUNCTION.is called with two arguments, the key and the value."
+ (funcall (map--dispatch map
+ :list #'map--do-alist
+ :hash-table #'maphash
+ :array #'map--do-array)
+ function
+ map))
+
(defun map-keys-apply (function map)
"Return the result of applying FUNCTION to each key of MAP.
@@ -354,6 +364,20 @@ MAP can be a list, hash-table or array."
(setq index (1+ index))))
map)))
+(defun map--do-alist (function alist)
+ "Private function used to iterate over ALIST using FUNCTION."
+ (seq-do (lambda (pair)
+ (funcall function
+ (car pair)
+ (cdr pair)))
+ alist))
+
+(defun map--do-array (function array)
+ "Private function usde to iterate over ARRAY using FUNCTION."
+ (seq-do-indexed (lambda (elt index)
+ (funcall function index elt))
+ array))
+
(defun map--into-hash-table (map)
"Convert MAP into a hash-table."
(let ((ht (make-hash-table :size (map-length map)