summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2015-11-07 12:45:18 +0000
committerArtur Malabarba <bruce.connor.am@gmail.com>2015-11-10 13:04:30 +0000
commitcbaa04014e0c9efdfc6393bccde0e6579b5d7051 (patch)
treea0fc99cbe4225fc9ff5b5ed04fd5f07441e9cbc8 /lisp/emacs-lisp
parentcbc51211f9e4f8f3d4b8a1feaa6cbfd2fd4ac1ca (diff)
downloademacs-cbaa04014e0c9efdfc6393bccde0e6579b5d7051.tar.gz
emacs-cbaa04014e0c9efdfc6393bccde0e6579b5d7051.tar.bz2
emacs-cbaa04014e0c9efdfc6393bccde0e6579b5d7051.zip
* lisp/emacs-lisp/map.el (map-merge-with): New function
* test/automated/map-tests.el (test-map-merge-with): New test
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r--lisp/emacs-lisp/map.el25
1 files changed, 20 insertions, 5 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 5ef51f12d96..7ff9031b08d 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -279,9 +279,9 @@ MAP can be a list, hash-table or array."
MAP can be a list, hash-table or array."
(catch 'map--break
(map-apply (lambda (key value)
- (or (funcall pred key value)
- (throw 'map--break nil)))
- map)
+ (or (funcall pred key value)
+ (throw 'map--break nil)))
+ map)
t))
(defun map-merge (type &rest maps)
@@ -291,8 +291,23 @@ MAP can be a list, hash-table or array."
(let (result)
(while maps
(map-apply (lambda (key value)
- (setf (map-elt result key) value))
- (pop maps)))
+ (setf (map-elt result key) value))
+ (pop maps)))
+ (map-into result type)))
+
+(defun map-merge-with (type function &rest maps)
+ "Merge into a map of type TYPE all the key/value pairs in MAPS.
+When two maps contain the same key, call FUNCTION on the two
+values and use the value returned by it.
+MAP can be a list, hash-table or array."
+ (let (result)
+ (while maps
+ (map-apply (lambda (key value)
+ (setf (map-elt result key)
+ (if (map-contains-key result key)
+ (funcall function (map-elt result key) value)
+ value)))
+ (pop maps)))
(map-into result type)))
(defun map-into (map type)