[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master 2aebb0d 2/4: Add new function map-do
From: |
Nicolas Petton |
Subject: |
[Emacs-diffs] master 2aebb0d 2/4: Add new function map-do |
Date: |
Sat, 18 Jun 2016 08:10:06 +0000 (UTC) |
branch: master
commit 2aebb0dd1fc66ba8cacef3f734e9a046cbc04ad2
Author: Nicolas Petton <address@hidden>
Commit: Nicolas Petton <address@hidden>
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.
---
lisp/emacs-lisp/map.el | 26 +++++++++++++++++++++++++-
test/lisp/emacs-lisp/map-tests.el | 8 ++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index b97d8b1..7c4afb9 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -4,7 +4,7 @@
;; Author: Nicolas Petton <address@hidden>
;; Keywords: convenience, map, hash-table, alist, array
-;; Version: 1.0
+;; Version: 1.1
;; Package: map
;; Maintainer: address@hidden
@@ -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)
diff --git a/test/lisp/emacs-lisp/map-tests.el
b/test/lisp/emacs-lisp/map-tests.el
index 20cb0f6..0af1c65 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.
(2 . b)
(3 . c))))))
+(ert-deftest test-map-do ()
+ (with-maps-do map
+ (let ((result nil))
+ (map-do (lambda (k v)
+ (add-to-list 'result (list (int-to-string k) v)))
+ map)
+ (should (equal result '(("2" 5) ("1" 4) ("0" 3)))))))
+
(ert-deftest test-map-keys-apply ()
(with-maps-do map
(should (equal (map-keys-apply (lambda (k) (int-to-string k))