Function: map-merge-with

map-merge-with is a byte-compiled function defined in map.el.gz.

Signature

(map-merge-with TYPE FUNCTION &rest MAPS)

Documentation

Merge into a map of 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 FUNCTION returns. Each of MAPS can be an alist, plist, hash-table, or array. See map-into for all supported values of TYPE. This does not modify any of the MAPS.

Other relevant functions are documented in the map group.

Probably introduced at or before Emacs version 28.1.

Shortdoc

;; map
(map-merge-with 'alist #'max '(1 2 3 4) #s(hash-table data (1 1 3 5)))
    => ((1 . 2) (3 . 5))
  (map-merge-with 'alist #'min '(1 2 3 4) #s(hash-table data (1 1 3 5)))
    => ((1 . 1) (3 . 4))
  (map-merge-with 'hash-table #'min '(1 2 3 4) #s(hash-table data (1 1 3 5)))
    => #s(hash-table test equal data (1 1 3 4))

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/map.el.gz
(defun map-merge-with (type function &rest maps)
  "Merge into a map of 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 FUNCTION returns.
Each of MAPS can be an alist, plist, hash-table, or array.
See `map-into' for all supported values of TYPE.
This does not modify any of the MAPS."
  (let ((not-found (list nil)))
    (apply #'map--merge
           (lambda (result key value)
             (cl-callf (lambda (old)
                         (if (eql old not-found)
                             value
                           (funcall function old value)))
                 (map-elt result key not-found))
             result)
           type maps)))