Quoting fixes in doc strings and diagnostics
[emacs.git] / lisp / emacs-lisp / map.el
blobec8d3d79d9f37abec05aea1da932f3eb560e34b1
1 ;;; map.el --- Map manipulation functions -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Keywords: convenience, map, hash-table, alist, array
7 ;; Version: 1.0
8 ;; Package: map
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/>.
27 ;;; Commentary:
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.
36 ;; TODO:
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
41 ;; be really useful
43 ;;; Code:
45 (require 'seq)
47 (pcase-defmacro map (&rest args)
48 "Build a `pcase' pattern matching map elements.
50 ARGS is a list of elements to be matched in the map.
52 Each element of ARGS can be of the form (KEY PAT), in which case KEY is
53 evaluated and searched for in the map. The match fails if for any KEY
54 found in the map, the corresponding PAT doesn't match the value
55 associated to the KEY.
57 Each element can also be a SYMBOL, which is an abbreviation of a (KEY
58 PAT) tuple of the form (\\='SYMBOL SYMBOL).
60 Keys in ARGS not found in the map are ignored, and the match doesn't
61 fail."
62 `(and (pred mapp)
63 ,@(map--make-pcase-bindings args)))
65 (defmacro map-let (keys map &rest body)
66 "Bind the variables in KEYS to the elements of MAP then evaluate BODY.
68 KEYS can be a list of symbols, in which case each element will be
69 bound to the looked up value in MAP.
71 KEYS can also be a list of (KEY VARNAME) pairs, in which case
72 KEY is an unquoted form.
74 MAP can be a list, hash-table or array."
75 (declare (indent 2) (debug t))
76 `(pcase-let ((,(map--make-pcase-patterns keys) ,map))
77 ,@body))
79 (eval-when-compile
80 (defmacro map--dispatch (map-var &rest args)
81 "Evaluate one of the forms specified by ARGS based on the type of MAP.
83 The following keyword types are meaningful: `:list',
84 `:hash-table' and `:array'.
86 An error is thrown if MAP is neither a list, hash-table nor array.
88 Return RESULT if non-nil or the result of evaluation of the form."
89 (declare (debug t) (indent 1))
90 `(cond ((listp ,map-var) ,(plist-get args :list))
91 ((hash-table-p ,map-var) ,(plist-get args :hash-table))
92 ((arrayp ,map-var) ,(plist-get args :array))
93 (t (error "Unsupported map: %s" ,map-var)))))
95 (defun map-elt (map key &optional default)
96 "Lookup KEY in MAP and return its associated value.
97 If KEY is not found, return DEFAULT which defaults to nil.
99 If MAP is a list, `eql' is used to lookup KEY.
101 MAP can be a list, hash-table or array."
102 (declare
103 (gv-expander
104 (lambda (do)
105 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
106 (macroexp-let2* nil
107 ;; Eval them once and for all in the right order.
108 ((key key) (default default))
109 `(if (listp ,mgetter)
110 ;; Special case the alist case, since it can't be handled by the
111 ;; map--put function.
112 ,(gv-get `(alist-get ,key (gv-synthetic-place
113 ,mgetter ,msetter)
114 ,default)
116 ,(funcall do `(map-elt ,mgetter ,key ,default)
117 (lambda (v) `(map--put ,mgetter ,key ,v)))))))))
118 (map--dispatch map
119 :list (alist-get key map default)
120 :hash-table (gethash key map default)
121 :array (if (and (>= key 0) (< key (seq-length map)))
122 (seq-elt map key)
123 default)))
125 (defmacro map-put (map key value)
126 "Associate KEY with VALUE in MAP and return MAP.
127 If KEY is already present in MAP, replace the associated value
128 with VALUE.
130 MAP can be a list, hash-table or array."
131 (macroexp-let2 nil map map
132 `(progn
133 (setf (map-elt ,map ,key) ,value)
134 ,map)))
136 (defmacro map-delete (map key)
137 "Delete KEY from MAP and return MAP.
138 No error is signaled if KEY is not a key of MAP. If MAP is an
139 array, store nil at the index KEY.
141 MAP can be a list, hash-table or array."
142 (declare (debug t))
143 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
144 (macroexp-let2 nil key key
145 `(if (not (listp ,mgetter))
146 (map--delete ,mgetter ,key)
147 ;; The alist case is special, since it can't be handled by the
148 ;; map--delete function.
149 (setf (alist-get ,key (gv-synthetic-place ,mgetter ,msetter)
150 nil t)
151 nil)
152 ,mgetter))))
154 (defun map-nested-elt (map keys &optional default)
155 "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil.
157 Map can be a nested map composed of alists, hash-tables and arrays."
158 (or (seq-reduce (lambda (acc key)
159 (when (mapp acc)
160 (map-elt acc key)))
161 keys
162 map)
163 default))
165 (defun map-keys (map)
166 "Return the list of keys in MAP.
168 MAP can be a list, hash-table or array."
169 (map-apply (lambda (key _) key) map))
171 (defun map-values (map)
172 "Return the list of values in MAP.
174 MAP can be a list, hash-table or array."
175 (map-apply (lambda (_ value) value) map))
177 (defun map-pairs (map)
178 "Return the elements of MAP as key/value association lists.
180 MAP can be a list, hash-table or array."
181 (map-apply #'cons map))
183 (defun map-length (map)
184 "Return the length of MAP.
186 MAP can be a list, hash-table or array."
187 (length (map-keys map)))
189 (defun map-copy (map)
190 "Return a copy of MAP.
192 MAP can be a list, hash-table or array."
193 (map--dispatch map
194 :list (seq-copy map)
195 :hash-table (copy-hash-table map)
196 :array (seq-copy map)))
198 (defun map-apply (function map)
199 "Apply FUNCTION to each element of MAP and return the result as a list.
200 FUNCTION is called with two arguments, the key and the value.
202 MAP can be a list, hash-table or array."
203 (funcall (map--dispatch map
204 :list #'map--apply-alist
205 :hash-table #'map--apply-hash-table
206 :array #'map--apply-array)
207 function
208 map))
210 (defun map-keys-apply (function map)
211 "Return the result of applying FUNCTION to each key of MAP.
213 MAP can be a list, hash-table or array."
214 (map-apply (lambda (key _)
215 (funcall function key))
216 map))
218 (defun map-values-apply (function map)
219 "Return the result of applying FUNCTION to each value of MAP.
221 MAP can be a list, hash-table or array."
222 (map-apply (lambda (_ val)
223 (funcall function val))
224 map))
226 (defun map-filter (pred map)
227 "Return an alist of key/val pairs for which (PRED key val) is non-nil in MAP.
229 MAP can be a list, hash-table or array."
230 (delq nil (map-apply (lambda (key val)
231 (if (funcall pred key val)
232 (cons key val)
233 nil))
234 map)))
236 (defun map-remove (pred map)
237 "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
239 MAP can be a list, hash-table or array."
240 (map-filter (lambda (key val) (not (funcall pred key val)))
241 map))
243 (defun mapp (map)
244 "Return non-nil if MAP is a map (list, hash-table or array)."
245 (or (listp map)
246 (hash-table-p map)
247 (arrayp map)))
249 (defun map-empty-p (map)
250 "Return non-nil if MAP is empty.
252 MAP can be a list, hash-table or array."
253 (map--dispatch map
254 :list (null map)
255 :array (seq-empty-p map)
256 :hash-table (zerop (hash-table-count map))))
258 (defun map-contains-key (map key &optional testfn)
259 "Return non-nil if MAP contain KEY, nil otherwise.
260 Equality is defined by TESTFN if non-nil or by `equal' if nil.
262 MAP can be a list, hash-table or array."
263 (seq-contains (map-keys map) key testfn))
265 (defun map-some (pred map)
266 "Return a non-nil if (PRED key val) is non-nil for any key/value pair in MAP.
268 MAP can be a list, hash-table or array."
269 (catch 'map--break
270 (map-apply (lambda (key value)
271 (let ((result (funcall pred key value)))
272 (when result
273 (throw 'map--break result))))
274 map)
275 nil))
277 (defun map-every-p (pred map)
278 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP.
280 MAP can be a list, hash-table or array."
281 (catch 'map--break
282 (map-apply (lambda (key value)
283 (or (funcall pred key value)
284 (throw 'map--break nil)))
285 map)
288 (defun map-merge (type &rest maps)
289 "Merge into a map of type TYPE all the key/value pairs in MAPS.
291 MAP can be a list, hash-table or array."
292 (let (result)
293 (while maps
294 (map-apply (lambda (key value)
295 (setf (map-elt result key) value))
296 (pop maps)))
297 (map-into result type)))
299 (defun map-merge-with (type function &rest maps)
300 "Merge into a map of type TYPE all the key/value pairs in MAPS.
301 When two maps contain the same key, call FUNCTION on the two
302 values and use the value returned by it.
303 MAP can be a list, hash-table or array."
304 (let (result)
305 (while maps
306 (map-apply (lambda (key value)
307 (setf (map-elt result key)
308 (if (map-contains-key result key)
309 (funcall function (map-elt result key) value)
310 value)))
311 (pop maps)))
312 (map-into result type)))
314 (defun map-into (map type)
315 "Convert the map MAP into a map of type TYPE.
317 TYPE can be one of the following symbols: list or hash-table.
318 MAP can be a list, hash-table or array."
319 (pcase type
320 (`list (map-pairs map))
321 (`hash-table (map--into-hash-table map))
322 (_ (error "Not a map type name: %S" type))))
324 (defun map--put (map key v)
325 (map--dispatch map
326 :list (let ((p (assoc key map)))
327 (if p (setcdr p v)
328 (error "No place to change the mapping for %S" key)))
329 :hash-table (puthash key v map)
330 :array (aset map key v)))
332 (defun map--apply-alist (function map)
333 "Private function used to apply FUNCTION over MAP, MAP being an alist."
334 (seq-map (lambda (pair)
335 (funcall function
336 (car pair)
337 (cdr pair)))
338 map))
340 (defun map--delete (map key)
341 (map--dispatch map
342 :list (error "No place to remove the mapping for %S" key)
343 :hash-table (remhash key map)
344 :array (and (>= key 0)
345 (<= key (seq-length map))
346 (aset map key nil)))
347 map)
349 (defun map--apply-hash-table (function map)
350 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
351 (let (result)
352 (maphash (lambda (key value)
353 (push (funcall function key value) result))
354 map)
355 (nreverse result)))
357 (defun map--apply-array (function map)
358 "Private function used to apply FUNCTION over MAP, MAP being an array."
359 (let ((index 0))
360 (seq-map (lambda (elt)
361 (prog1
362 (funcall function index elt)
363 (setq index (1+ index))))
364 map)))
366 (defun map--into-hash-table (map)
367 "Convert MAP into a hash-table."
368 (let ((ht (make-hash-table :size (map-length map)
369 :test 'equal)))
370 (map-apply (lambda (key value)
371 (setf (map-elt ht key) value))
372 map)
373 ht))
375 (defun map--make-pcase-bindings (args)
376 "Return a list of pcase bindings from ARGS to the elements of a map."
377 (seq-map (lambda (elt)
378 (if (consp elt)
379 `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))
380 `(app (pcase--flip map-elt ',elt) ,elt)))
381 args))
383 (defun map--make-pcase-patterns (args)
384 "Return a list of `(map ...)' pcase patterns built from ARGS."
385 (cons 'map
386 (seq-map (lambda (elt)
387 (if (and (consp elt) (eq 'map (car elt)))
388 (map--make-pcase-patterns elt)
389 elt))
390 args)))
392 (provide 'map)
393 ;;; map.el ends here