1 ;;; map.el --- Map manipulation functions -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Keywords: convenience, map, hash-table, alist, array
10 ;; Maintainer: emacs-devel@gnu.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
29 ;; map.el provides map-manipulation functions that work on alists,
30 ;; hash-table and arrays. All functions are prefixed with "map-".
32 ;; Functions taking a predicate or iterating over a map using a
33 ;; function take the function as their first argument. All other
34 ;; functions take the map as their first argument.
37 ;; - Add support for char-tables
38 ;; - Maybe add support for gv?
39 ;; - See if we can integrate text-properties
40 ;; - A macro similar to let-alist but working on any type of map could
47 (pcase-defmacro map (&rest args)
48 "Build a `pcase' pattern matching map elements.
50 The `pcase' pattern will match each element of PATTERN against
51 the corresponding elements of the map.
53 Extra elements of the map are ignored if fewer ARGS are
54 given, and the match does not fail.
56 ARGS can be a list of the form (KEY PAT), in which case KEY in an
59 ARGS can also be a list of symbols, which stands for ('SYMBOL
62 ,@(map--make-pcase-bindings args)))
64 (defmacro map-let (keys map &rest body)
65 "Bind the variables in KEYS to the elements of MAP then evaluate BODY.
67 KEYS can be a list of symbols, in which case each element will be
68 bound to the looked up value in MAP.
70 KEYS can also be a list of (KEY VARNAME) pairs, in which case
71 KEY is an unquoted form.
73 MAP can be a list, hash-table or array."
74 (declare (indent 2) (debug t))
75 `(pcase-let ((,(map--make-pcase-patterns keys) ,map))
79 (defmacro map--dispatch (map-var &rest args)
80 "Evaluate one of the forms specified by ARGS based on the type of MAP.
82 The following keyword types are meaningful: `:list',
83 `:hash-table' and `:array'.
85 An error is thrown if MAP is neither a list, hash-table nor array.
87 Return RESULT if non-nil or the result of evaluation of the form."
88 (declare (debug t) (indent 1))
89 `(cond ((listp ,map-var) ,(plist-get args :list))
90 ((hash-table-p ,map-var) ,(plist-get args :hash-table))
91 ((arrayp ,map-var) ,(plist-get args :array))
92 (t (error "Unsupported map: %s" ,map-var)))))
94 (defun map-elt (map key &optional default)
95 "Lookup KEY in MAP and return its associated value.
96 If KEY is not found, return DEFAULT which defaults to nil.
98 If MAP is a list, `eql' is used to lookup KEY.
100 MAP can be a list, hash-table or array."
104 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
106 ;; Eval them once and for all in the right order.
107 ((key key) (default default))
108 `(if (listp ,mgetter)
109 ;; Special case the alist case, since it can't be handled by the
110 ;; map--put function.
111 ,(gv-get `(alist-get ,key (gv-synthetic-place
115 ,(funcall do `(map-elt ,mgetter ,key ,default)
116 (lambda (v) `(map--put ,mgetter ,key ,v)))))))))
118 :list (alist-get key map default)
119 :hash-table (gethash key map default)
120 :array (if (and (>= key 0) (< key (seq-length map)))
124 (defmacro map-put (map key value)
125 "Associate KEY with VALUE in MAP and return MAP.
126 If KEY is already present in MAP, replace the associated value
129 MAP can be a list, hash-table or array."
130 (macroexp-let2 nil map map
132 (setf (map-elt ,map ,key) ,value)
135 (defmacro map-delete (map key)
136 "Delete KEY from MAP and return MAP.
137 No error is signaled if KEY is not a key of MAP. If MAP is an
138 array, store nil at the index KEY.
140 MAP can be a list, hash-table or array."
142 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
143 (macroexp-let2 nil key key
144 `(if (not (listp ,mgetter))
145 (map--delete ,mgetter ,key)
146 ;; The alist case is special, since it can't be handled by the
147 ;; map--delete function.
148 (setf (alist-get ,key (gv-synthetic-place ,mgetter ,msetter)
153 (defun map-nested-elt (map keys &optional default)
154 "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil.
156 Map can be a nested map composed of alists, hash-tables and arrays."
157 (or (seq-reduce (lambda (acc key)
164 (defun map-keys (map)
165 "Return the list of keys in MAP.
167 MAP can be a list, hash-table or array."
168 (map-apply (lambda (key _) key) map))
170 (defun map-values (map)
171 "Return the list of values in MAP.
173 MAP can be a list, hash-table or array."
174 (map-apply (lambda (_ value) value) map))
176 (defun map-pairs (map)
177 "Return the elements of MAP as key/value association lists.
179 MAP can be a list, hash-table or array."
180 (map-apply #'cons map))
182 (defun map-length (map)
183 "Return the length of MAP.
185 MAP can be a list, hash-table or array."
186 (length (map-keys map)))
188 (defun map-copy (map)
189 "Return a copy of MAP.
191 MAP can be a list, hash-table or array."
194 :hash-table (copy-hash-table map)
195 :array (seq-copy map)))
197 (defun map-apply (function map)
198 "Apply FUNCTION to each element of MAP and return the result as a list.
199 FUNCTION is called with two arguments, the key and the value.
201 MAP can be a list, hash-table or array."
202 (funcall (map--dispatch map
203 :list #'map--apply-alist
204 :hash-table #'map--apply-hash-table
205 :array #'map--apply-array)
209 (defun map-keys-apply (function map)
210 "Return the result of applying FUNCTION to each key of MAP.
212 MAP can be a list, hash-table or array."
213 (map-apply (lambda (key _)
214 (funcall function key))
217 (defun map-values-apply (function map)
218 "Return the result of applying FUNCTION to each value of MAP.
220 MAP can be a list, hash-table or array."
221 (map-apply (lambda (_ val)
222 (funcall function val))
225 (defun map-filter (pred map)
226 "Return an alist of key/val pairs for which (PRED key val) is non-nil in MAP.
228 MAP can be a list, hash-table or array."
229 (delq nil (map-apply (lambda (key val)
230 (if (funcall pred key val)
235 (defun map-remove (pred map)
236 "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
238 MAP can be a list, hash-table or array."
239 (map-filter (lambda (key val) (not (funcall pred key val)))
243 "Return non-nil if MAP is a map (list, hash-table or array)."
248 (defun map-empty-p (map)
249 "Return non-nil if MAP is empty.
251 MAP can be a list, hash-table or array."
254 :array (seq-empty-p map)
255 :hash-table (zerop (hash-table-count map))))
257 (defun map-contains-key (map key &optional testfn)
258 "Return non-nil if MAP contain KEY, nil otherwise.
259 Equality is defined by TESTFN if non-nil or by `equal' if nil.
261 MAP can be a list, hash-table or array."
262 (seq-contains (map-keys map) key testfn))
264 (defun map-some (pred map)
265 "Return a non-nil if (PRED key val) is non-nil for any key/value pair in MAP.
267 MAP can be a list, hash-table or array."
269 (map-apply (lambda (key value)
270 (let ((result (funcall pred key value)))
272 (throw 'map--break result))))
276 (defun map-every-p (pred map)
277 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP.
279 MAP can be a list, hash-table or array."
281 (map-apply (lambda (key value)
282 (or (funcall pred key value)
283 (throw 'map--break nil)))
287 (defun map-merge (type &rest maps)
288 "Merge into a map of type TYPE all the key/value pairs in MAPS.
290 MAP can be a list, hash-table or array."
293 (map-apply (lambda (key value)
294 (setf (map-elt result key) value))
296 (map-into result type)))
298 (defun map-merge-with (type function &rest maps)
299 "Merge into a map of type TYPE all the key/value pairs in MAPS.
300 When two maps contain the same key, call FUNCTION on the two
301 values and use the value returned by it.
302 MAP can be a list, hash-table or array."
305 (map-apply (lambda (key value)
306 (setf (map-elt result key)
307 (if (map-contains-key result key)
308 (funcall function (map-elt result key) value)
311 (map-into result type)))
313 (defun map-into (map type)
314 "Convert the map MAP into a map of type TYPE.
316 TYPE can be one of the following symbols: list or hash-table.
317 MAP can be a list, hash-table or array."
319 (`list (map-pairs map))
320 (`hash-table (map--into-hash-table map))
321 (_ (error "Not a map type name: %S" type))))
323 (defun map--put (map key v)
325 :list (let ((p (assoc key map)))
327 (error "No place to change the mapping for %S" key)))
328 :hash-table (puthash key v map)
329 :array (aset map key v)))
331 (defun map--apply-alist (function map)
332 "Private function used to apply FUNCTION over MAP, MAP being an alist."
333 (seq-map (lambda (pair)
339 (defun map--delete (map key)
341 :list (error "No place to remove the mapping for %S" key)
342 :hash-table (remhash key map)
343 :array (and (>= key 0)
344 (<= key (seq-length map))
348 (defun map--apply-hash-table (function map)
349 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
351 (maphash (lambda (key value)
352 (push (funcall function key value) result))
356 (defun map--apply-array (function map)
357 "Private function used to apply FUNCTION over MAP, MAP being an array."
359 (seq-map (lambda (elt)
361 (funcall function index elt)
362 (setq index (1+ index))))
365 (defun map--into-hash-table (map)
366 "Convert MAP into a hash-table."
367 (let ((ht (make-hash-table :size (map-length map)
369 (map-apply (lambda (key value)
370 (setf (map-elt ht key) value))
374 (defun map--make-pcase-bindings (args)
375 "Return a list of pcase bindings from ARGS to the elements of a map."
376 (seq-map (lambda (elt)
378 `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))
379 `(app (pcase--flip map-elt ',elt) ,elt)))
382 (defun map--make-pcase-patterns (args)
383 "Return a list of `(map ...)' pcase patterns built from ARGS."
385 (seq-map (lambda (elt)
386 (if (and (consp elt) (eq 'map (car elt)))
387 (map--make-pcase-patterns elt)