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