]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/map.el
Better docstrings in seq.el and map.el
[gnu-emacs] / lisp / emacs-lisp / map.el
1 ;;; map.el --- Map manipulation functions -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Keywords: convenience, map, hash-table, alist, array
7 ;; Version: 1.0
8 ;; Package: map
9
10 ;; Maintainer: emacs-devel@gnu.org
11
12 ;; This file is part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
29 ;; map.el provides map-manipulation functions that work on alists,
30 ;; hash-table and arrays. All functions are prefixed with "map-".
31 ;;
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.
35
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
42
43 ;;; Code:
44
45 (require 'seq)
46
47 (pcase-defmacro map (&rest args)
48 "pcase pattern matching map elements.
49
50 Matches if the object is a map (list, hash-table or array), and
51 each PATTERN matches the corresponding elements of the map.
52
53 Supernumerary elements of the map are ignore if less ARGS are
54 given, and the match does not fail.
55
56 ARGS can be a list of the form (KEY PAT), in which case KEY in an
57 unquoted form.
58
59 ARGS can also be a list of symbols, which stands for ('SYMBOL
60 SYMBOL)."
61 `(and (pred map-p)
62 ,@(map--make-pcase-bindings args)))
63
64 (defmacro map-let (keys map &rest body)
65 "Bind the variables in KEYS to the elements of MAP then evaluate BODY.
66
67 KEYS can be a list of symbols, in which case each element will be
68 bound to the looked up value in MAP.
69
70 KEYS can also be a list of (KEY VARNAME) pairs, in which case
71 KEY is an unquoted form.
72
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))
76 ,@body))
77
78 (eval-when-compile
79 (defmacro map--dispatch (map-var &rest args)
80 "Evaluate one of the forms specified by ARGS based on the type of MAP.
81
82 The following keyword types are meaningful: `:list',
83 `:hash-table' and `:array'.
84
85 An error is thrown if MAP is neither a list, hash-table nor array.
86
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)))))
93
94 (defun map-elt (map key &optional default)
95 "Perform a lookup in MAP of KEY and return its associated value.
96 If KEY is not found, return DEFAULT which defaults to nil.
97
98 If MAP is a list, `eql' is used to lookup KEY.
99
100 MAP can be a list, hash-table or array."
101 (declare
102 (gv-expander
103 (lambda (do)
104 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
105 (macroexp-let2* nil
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
112 ,mgetter ,msetter)
113 ,default)
114 do)
115 ,(funcall do `(map-elt ,mgetter ,key ,default)
116 (lambda (v) `(map--put ,mgetter ,key ,v)))))))))
117 (map--dispatch map
118 :list (alist-get key map default)
119 :hash-table (gethash key map default)
120 :array (if (and (>= key 0) (< key (seq-length map)))
121 (seq-elt map key)
122 default)))
123
124 (defmacro map-put (map key value)
125 "In MAP, associate KEY with VALUE and return MAP.
126 If KEY is already present in MAP, replace the associated value
127 with VALUE.
128
129 MAP can be a list, hash-table or array."
130 (macroexp-let2 nil map map
131 `(progn
132 (setf (map-elt ,map ,key) ,value)
133 ,map)))
134
135 (defmacro map-delete (map key)
136 "In MAP, delete the key KEY if present and return MAP.
137 If MAP is an array, store nil at the index KEY.
138
139 MAP can be a list, hash-table or array."
140 (declare (debug t))
141 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
142 (macroexp-let2 nil key key
143 `(if (not (listp ,mgetter))
144 (map--delete ,mgetter ,key)
145 ;; The alist case is special, since it can't be handled by the
146 ;; map--delete function.
147 (setf (alist-get ,key (gv-synthetic-place ,mgetter ,msetter)
148 nil t)
149 nil)
150 ,mgetter))))
151
152 (defun map-nested-elt (map keys &optional default)
153 "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil.
154
155 Map can be a nested map composed of alists, hash-tables and arrays."
156 (or (seq-reduce (lambda (acc key)
157 (when (map-p acc)
158 (map-elt acc key)))
159 keys
160 map)
161 default))
162
163 (defun map-keys (map)
164 "Return the list of keys in MAP.
165
166 MAP can be a list, hash-table or array."
167 (map-apply (lambda (key _) key) map))
168
169 (defun map-values (map)
170 "Return the list of values in MAP.
171
172 MAP can be a list, hash-table or array."
173 (map-apply (lambda (_ value) value) map))
174
175 (defun map-pairs (map)
176 "Return the elements of MAP as key/value association lists.
177
178 MAP can be a list, hash-table or array."
179 (map-apply #'cons map))
180
181 (defun map-length (map)
182 "Return the length of MAP.
183
184 MAP can be a list, hash-table or array."
185 (length (map-keys map)))
186
187 (defun map-copy (map)
188 "Return a copy of MAP.
189
190 MAP can be a list, hash-table or array."
191 (map--dispatch map
192 :list (seq-copy map)
193 :hash-table (copy-hash-table map)
194 :array (seq-copy map)))
195
196 (defun map-apply (function map)
197 "Apply FUNCTION to each element of MAP and return the result as a list.
198 FUNCTION is called with two arguments, the key and the value.
199
200 MAP can be a list, hash-table or array."
201 (funcall (map--dispatch map
202 :list #'map--apply-alist
203 :hash-table #'map--apply-hash-table
204 :array #'map--apply-array)
205 function
206 map))
207
208 (defun map-keys-apply (function map)
209 "Return the result of applying FUNCTION to each key of MAP.
210
211 MAP can be a list, hash-table or array."
212 (map-apply (lambda (key _)
213 (funcall function key))
214 map))
215
216 (defun map-values-apply (function map)
217 "Return the result of applying FUNCTION to each value of MAP.
218
219 MAP can be a list, hash-table or array."
220 (map-apply (lambda (_ val)
221 (funcall function val))
222 map))
223
224 (defun map-filter (pred map)
225 "Return an alist of key/val pairs for which (PRED key val) is non-nil in MAP.
226
227 MAP can be a list, hash-table or array."
228 (delq nil (map-apply (lambda (key val)
229 (if (funcall pred key val)
230 (cons key val)
231 nil))
232 map)))
233
234 (defun map-remove (pred map)
235 "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
236
237 MAP can be a list, hash-table or array."
238 (map-filter (lambda (key val) (not (funcall pred key val)))
239 map))
240
241 (defun map-p (map)
242 "Return non-nil if MAP is a map (list, hash-table or array)."
243 (or (listp map)
244 (hash-table-p map)
245 (arrayp map)))
246
247 (defun map-empty-p (map)
248 "Return non-nil is MAP is empty.
249
250 MAP can be a list, hash-table or array."
251 (map--dispatch map
252 :list (null map)
253 :array (seq-empty-p map)
254 :hash-table (zerop (hash-table-count map))))
255
256 (defun map-contains-key (map key &optional testfn)
257 "Return non-nil if MAP contain the key KEY, nil otherwise.
258 Equality is defined by TESTFN if non-nil or by `equal' if nil.
259
260 MAP can be a list, hash-table or array."
261 (seq-contains (map-keys map) key testfn))
262
263 (defun map-some (pred map)
264 "Return a non-nil if (PRED key val) is non-nil for any key/value pair in MAP.
265
266 MAP can be a list, hash-table or array."
267 (catch 'map--break
268 (map-apply (lambda (key value)
269 (let ((result (funcall pred key value)))
270 (when result
271 (throw 'map--break result))))
272 map)
273 nil))
274
275 (defun map-every-p (pred map)
276 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP.
277
278 MAP can be a list, hash-table or array."
279 (catch 'map--break
280 (map-apply (lambda (key value)
281 (or (funcall pred key value)
282 (throw 'map--break nil)))
283 map)
284 t))
285
286 (defun map-merge (type &rest maps)
287 "Merge into a map of type TYPE all the key/value pairs in the maps MAPS.
288
289 MAP can be a list, hash-table or array."
290 (let (result)
291 (while maps
292 (map-apply (lambda (key value)
293 (setf (map-elt result key) value))
294 (pop maps)))
295 (map-into result type)))
296
297 (defun map-into (map type)
298 "Convert the map MAP into a map of type TYPE.
299
300 TYPE can be one of the following symbols: list or hash-table.
301 MAP can be a list, hash-table or array."
302 (pcase type
303 (`list (map-pairs map))
304 (`hash-table (map--into-hash-table map))
305 (_ (error "Not a map type name: %S" type))))
306
307 (defun map--put (map key v)
308 (map--dispatch map
309 :list (let ((p (assoc key map)))
310 (if p (setcdr p v)
311 (error "No place to change the mapping for %S" key)))
312 :hash-table (puthash key v map)
313 :array (aset map key v)))
314
315 (defun map--apply-alist (function map)
316 "Private function used to apply FUNCTION over MAP, MAP being an alist."
317 (seq-map (lambda (pair)
318 (funcall function
319 (car pair)
320 (cdr pair)))
321 map))
322
323 (defun map--delete (map key)
324 (map--dispatch map
325 :list (error "No place to remove the mapping for %S" key)
326 :hash-table (remhash key map)
327 :array (and (>= key 0)
328 (<= key (seq-length map))
329 (aset map key nil)))
330 map)
331
332 (defun map--apply-hash-table (function map)
333 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
334 (let (result)
335 (maphash (lambda (key value)
336 (push (funcall function key value) result))
337 map)
338 (nreverse result)))
339
340 (defun map--apply-array (function map)
341 "Private function used to apply FUNCTION over MAP, MAP being an array."
342 (let ((index 0))
343 (seq-map (lambda (elt)
344 (prog1
345 (funcall function index elt)
346 (setq index (1+ index))))
347 map)))
348
349 (defun map--into-hash-table (map)
350 "Convert MAP into a hash-table."
351 (let ((ht (make-hash-table :size (map-length map)
352 :test 'equal)))
353 (map-apply (lambda (key value)
354 (setf (map-elt ht key) value))
355 map)
356 ht))
357
358 (defun map--make-pcase-bindings (args)
359 "Return a list of pcase bindings from ARGS to the elements of a map."
360 (seq-map (lambda (elt)
361 (if (consp elt)
362 `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))
363 `(app (pcase--flip map-elt ',elt) ,elt)))
364 args))
365
366 (defun map--make-pcase-patterns (args)
367 "Return a list of `(map ...)' pcase patterns built from ARGS."
368 (cons 'map
369 (seq-map (lambda (elt)
370 (if (and (consp elt) (eq 'map (car elt)))
371 (map--make-pcase-patterns elt)
372 elt))
373 args)))
374
375 (provide 'map)
376 ;;; map.el ends here