]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/map.el
Merge from origin/emacs-25
[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.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 "Build a `pcase' pattern matching map elements.
49
50 ARGS is a list of elements to be matched in the map.
51
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.
56
57 Each element can also be a SYMBOL, which is an abbreviation of a (KEY
58 PAT) tuple of the form (\\='SYMBOL SYMBOL).
59
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)))
64
65 (defmacro map-let (keys map &rest body)
66 "Bind the variables in KEYS to the elements of MAP then evaluate BODY.
67
68 KEYS can be a list of symbols, in which case each element will be
69 bound to the looked up value in MAP.
70
71 KEYS can also be a list of (KEY VARNAME) pairs, in which case
72 KEY is an unquoted form.
73
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))
78
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.
82
83 The following keyword types are meaningful: `:list',
84 `:hash-table' and `:array'.
85
86 An error is thrown if MAP is neither a list, hash-table nor array.
87
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)))))
94
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.
98
99 If MAP is a list, `eql' is used to lookup KEY.
100
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)
115 do)
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)))
124
125 (defmacro map-put (map key value)
126 "Associate KEY with VALUE in MAP and return VALUE.
127 If KEY is already present in MAP, replace the associated value
128 with VALUE.
129
130 MAP can be a list, hash-table or array."
131 `(setf (map-elt ,map ,key) ,value))
132
133 (defun map-delete (map key)
134 "Delete KEY from MAP and return MAP.
135 No error is signaled if KEY is not a key of MAP. If MAP is an
136 array, store nil at the index KEY.
137
138 MAP can be a list, hash-table or array."
139 (map--dispatch map
140 :list (setf (alist-get key map nil t) nil)
141 :hash-table (remhash key map)
142 :array (and (>= key 0)
143 (<= key (seq-length map))
144 (aset map key nil)))
145 map)
146
147 (defun map-nested-elt (map keys &optional default)
148 "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil.
149
150 Map can be a nested map composed of alists, hash-tables and arrays."
151 (or (seq-reduce (lambda (acc key)
152 (when (mapp acc)
153 (map-elt acc key)))
154 keys
155 map)
156 default))
157
158 (defun map-keys (map)
159 "Return the list of keys in MAP.
160
161 MAP can be a list, hash-table or array."
162 (map-apply (lambda (key _) key) map))
163
164 (defun map-values (map)
165 "Return the list of values in MAP.
166
167 MAP can be a list, hash-table or array."
168 (map-apply (lambda (_ value) value) map))
169
170 (defun map-pairs (map)
171 "Return the elements of MAP as key/value association lists.
172
173 MAP can be a list, hash-table or array."
174 (map-apply #'cons map))
175
176 (defun map-length (map)
177 "Return the length of MAP.
178
179 MAP can be a list, hash-table or array."
180 (length (map-keys map)))
181
182 (defun map-copy (map)
183 "Return a copy of MAP.
184
185 MAP can be a list, hash-table or array."
186 (map--dispatch map
187 :list (seq-copy map)
188 :hash-table (copy-hash-table map)
189 :array (seq-copy map)))
190
191 (defun map-apply (function map)
192 "Apply FUNCTION to each element of MAP and return the result as a list.
193 FUNCTION is called with two arguments, the key and the value.
194
195 MAP can be a list, hash-table or array."
196 (funcall (map--dispatch map
197 :list #'map--apply-alist
198 :hash-table #'map--apply-hash-table
199 :array #'map--apply-array)
200 function
201 map))
202
203 (defun map-keys-apply (function map)
204 "Return the result of applying FUNCTION to each key of MAP.
205
206 MAP can be a list, hash-table or array."
207 (map-apply (lambda (key _)
208 (funcall function key))
209 map))
210
211 (defun map-values-apply (function map)
212 "Return the result of applying FUNCTION to each value of MAP.
213
214 MAP can be a list, hash-table or array."
215 (map-apply (lambda (_ val)
216 (funcall function val))
217 map))
218
219 (defun map-filter (pred map)
220 "Return an alist of key/val pairs for which (PRED key val) is non-nil in MAP.
221
222 MAP can be a list, hash-table or array."
223 (delq nil (map-apply (lambda (key val)
224 (if (funcall pred key val)
225 (cons key val)
226 nil))
227 map)))
228
229 (defun map-remove (pred map)
230 "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
231
232 MAP can be a list, hash-table or array."
233 (map-filter (lambda (key val) (not (funcall pred key val)))
234 map))
235
236 (defun mapp (map)
237 "Return non-nil if MAP is a map (list, hash-table or array)."
238 (or (listp map)
239 (hash-table-p map)
240 (arrayp map)))
241
242 (defun map-empty-p (map)
243 "Return non-nil if MAP is empty.
244
245 MAP can be a list, hash-table or array."
246 (map--dispatch map
247 :list (null map)
248 :array (seq-empty-p map)
249 :hash-table (zerop (hash-table-count map))))
250
251 (defun map-contains-key (map key &optional testfn)
252 "Return non-nil if MAP contain KEY, nil otherwise.
253 Equality is defined by TESTFN if non-nil or by `equal' if nil.
254
255 MAP can be a list, hash-table or array."
256 (seq-contains (map-keys map) key testfn))
257
258 (defun map-some (pred map)
259 "Return a non-nil if (PRED key val) is non-nil for any key/value pair in MAP.
260
261 MAP can be a list, hash-table or array."
262 (catch 'map--break
263 (map-apply (lambda (key value)
264 (let ((result (funcall pred key value)))
265 (when result
266 (throw 'map--break result))))
267 map)
268 nil))
269
270 (defun map-every-p (pred map)
271 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP.
272
273 MAP can be a list, hash-table or array."
274 (catch 'map--break
275 (map-apply (lambda (key value)
276 (or (funcall pred key value)
277 (throw 'map--break nil)))
278 map)
279 t))
280
281 (defun map-merge (type &rest maps)
282 "Merge into a map of type TYPE all the key/value pairs in MAPS.
283
284 MAP can be a list, hash-table or array."
285 (let (result)
286 (while maps
287 (map-apply (lambda (key value)
288 (setf (map-elt result key) value))
289 (pop maps)))
290 (map-into result type)))
291
292 (defun map-merge-with (type function &rest maps)
293 "Merge into a map of type TYPE all the key/value pairs in MAPS.
294 When two maps contain the same key, call FUNCTION on the two
295 values and use the value returned by it.
296 MAP can be a list, hash-table or array."
297 (let (result)
298 (while maps
299 (map-apply (lambda (key value)
300 (setf (map-elt result key)
301 (if (map-contains-key result key)
302 (funcall function (map-elt result key) value)
303 value)))
304 (pop maps)))
305 (map-into result type)))
306
307 (defun map-into (map type)
308 "Convert the map MAP into a map of type TYPE.
309
310 TYPE can be one of the following symbols: list or hash-table.
311 MAP can be a list, hash-table or array."
312 (pcase type
313 (`list (map-pairs map))
314 (`hash-table (map--into-hash-table map))
315 (_ (error "Not a map type name: %S" type))))
316
317 (defun map--put (map key v)
318 (map--dispatch map
319 :list (let ((p (assoc key map)))
320 (if p (setcdr p v)
321 (error "No place to change the mapping for %S" key)))
322 :hash-table (puthash key v map)
323 :array (aset map key v)))
324
325 (defun map--apply-alist (function map)
326 "Private function used to apply FUNCTION over MAP, MAP being an alist."
327 (seq-map (lambda (pair)
328 (funcall function
329 (car pair)
330 (cdr pair)))
331 map))
332
333 (defun map--apply-hash-table (function map)
334 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
335 (let (result)
336 (maphash (lambda (key value)
337 (push (funcall function key value) result))
338 map)
339 (nreverse result)))
340
341 (defun map--apply-array (function map)
342 "Private function used to apply FUNCTION over MAP, MAP being an array."
343 (let ((index 0))
344 (seq-map (lambda (elt)
345 (prog1
346 (funcall function index elt)
347 (setq index (1+ index))))
348 map)))
349
350 (defun map--into-hash-table (map)
351 "Convert MAP into a hash-table."
352 (let ((ht (make-hash-table :size (map-length map)
353 :test 'equal)))
354 (map-apply (lambda (key value)
355 (setf (map-elt ht key) value))
356 map)
357 ht))
358
359 (defun map--make-pcase-bindings (args)
360 "Return a list of pcase bindings from ARGS to the elements of a map."
361 (seq-map (lambda (elt)
362 (if (consp elt)
363 `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))
364 `(app (pcase--flip map-elt ',elt) ,elt)))
365 args))
366
367 (defun map--make-pcase-patterns (args)
368 "Return a list of `(map ...)' pcase patterns built from ARGS."
369 (cons 'map
370 (seq-map (lambda (elt)
371 (if (and (consp elt) (eq 'map (car elt)))
372 (map--make-pcase-patterns elt)
373 elt))
374 args)))
375
376 (provide 'map)
377 ;;; map.el ends here