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