]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/map.el
Rename map-contains-key-p and map-some-p
[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 Matches if the object is a map (list, hash-table or array), and
50 binds values from ARGS to their corresponding elements of the map.
51
52 ARGS can be a list elements of the form (KEY PAT), in which case
53 KEY in an unquoted form.
54
55 ARGS can also be a list of symbols, which stands for ('SYMBOL
56 SYMBOL)."
57 `(and (pred map-p)
58 ,@(map--make-pcase-bindings args)))
59
60 (defmacro map-let (keys map &rest body)
61 "Bind the variables in KEYS to the elements of MAP then evaluate BODY.
62
63 KEYS can be a list of symbols, in which case each element will be
64 bound to the looked up value in MAP.
65
66 KEYS can also be a list of (KEY VARNAME) pairs, in which case
67 KEY is an unquoted form.
68
69 MAP can be a list, hash-table or array."
70 (declare (indent 2) (debug t))
71 `(pcase-let ((,(map--make-pcase-patterns keys) ,map))
72 ,@body))
73
74 (eval-when-compile
75 (defmacro map--dispatch (map-var &rest args)
76 "Evaluate one of the forms specified by ARGS based on the type of MAP.
77
78 The following keyword types are meaningful: `:list',
79 `:hash-table' and `:array'.
80
81 An error is thrown if MAP is neither a list, hash-table nor array.
82
83 Return RESULT if non-nil or the result of evaluation of the form."
84 (declare (debug t) (indent 1))
85 `(cond ((listp ,map-var) ,(plist-get args :list))
86 ((hash-table-p ,map-var) ,(plist-get args :hash-table))
87 ((arrayp ,map-var) ,(plist-get args :array))
88 (t (error "Unsupported map: %s" ,map-var)))))
89
90 (defun map-elt (map key &optional default)
91 "Perform a lookup in MAP of KEY and return its associated value.
92 If KEY is not found, return DEFAULT which defaults to nil.
93
94 If MAP is a list, `eql' is used to lookup KEY.
95
96 MAP can be a list, hash-table or array."
97 (declare
98 (gv-expander
99 (lambda (do)
100 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
101 (macroexp-let2* nil
102 ;; Eval them once and for all in the right order.
103 ((key key) (default default))
104 `(if (listp ,mgetter)
105 ;; Special case the alist case, since it can't be handled by the
106 ;; map--put function.
107 ,(gv-get `(alist-get ,key (gv-synthetic-place
108 ,mgetter ,msetter)
109 ,default)
110 do)
111 ,(funcall do `(map-elt ,mgetter ,key ,default)
112 (lambda (v) `(map--put ,mgetter ,key ,v)))))))))
113 (map--dispatch map
114 :list (alist-get key map default)
115 :hash-table (gethash key map default)
116 :array (if (and (>= key 0) (< key (seq-length map)))
117 (seq-elt map key)
118 default)))
119
120 (defmacro map-put (map key value)
121 "In MAP, associate KEY with VALUE and return MAP.
122 If KEY is already present in MAP, replace the associated value
123 with VALUE.
124
125 MAP can be a list, hash-table or array."
126 (macroexp-let2 nil map map
127 `(progn
128 (setf (map-elt ,map ,key) ,value)
129 ,map)))
130
131 (defmacro map-delete (map key)
132 "In MAP, delete the key KEY if present and return MAP.
133 If MAP is an array, store nil at the index KEY.
134
135 MAP can be a list, hash-table or array."
136 (declare (debug t))
137 (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
138 (macroexp-let2 nil key key
139 `(if (not (listp ,mgetter))
140 (map--delete ,mgetter ,key)
141 ;; The alist case is special, since it can't be handled by the
142 ;; map--delete function.
143 (setf (alist-get ,key (gv-synthetic-place ,mgetter ,msetter)
144 nil t)
145 nil)
146 ,mgetter))))
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 (map-p 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-keys-apply (function map)
205 "Return the result of applying FUNCTION to each key of MAP.
206
207 MAP can be a list, hash-table or array."
208 (map-apply (lambda (key _)
209 (funcall function key))
210 map))
211
212 (defun map-values-apply (function map)
213 "Return the result of applying FUNCTION to each value of MAP.
214
215 MAP can be a list, hash-table or array."
216 (map-apply (lambda (_ val)
217 (funcall function val))
218 map))
219
220 (defun map-filter (pred map)
221 "Return an alist of key/val pairs for which (PRED key val) is non-nil in MAP.
222
223 MAP can be a list, hash-table or array."
224 (delq nil (map-apply (lambda (key val)
225 (if (funcall pred key val)
226 (cons key val)
227 nil))
228 map)))
229
230 (defun map-remove (pred map)
231 "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
232
233 MAP can be a list, hash-table or array."
234 (map-filter (lambda (key val) (not (funcall pred key val)))
235 map))
236
237 (defun map-p (map)
238 "Return non-nil if MAP is a map (list, hash-table or array)."
239 (or (listp map)
240 (hash-table-p map)
241 (arrayp map)))
242
243 (defun map-empty-p (map)
244 "Return non-nil is MAP is empty.
245
246 MAP can be a list, hash-table or array."
247 (map--dispatch map
248 :list (null map)
249 :array (seq-empty-p map)
250 :hash-table (zerop (hash-table-count map))))
251
252 (defun map-contains-key (map key &optional testfn)
253 "Return non-nil if MAP contain the key KEY, nil otherwise.
254 Equality is defined by TESTFN if non-nil or by `equal' if nil.
255
256 MAP can be a list, hash-table or array."
257 (seq-contains (map-keys map) key testfn))
258
259 (defun map-some (pred map)
260 "Return a non-nil if (PRED key val) is non-nil for any key/value pair in MAP.
261
262 MAP can be a list, hash-table or array."
263 (catch 'map--break
264 (map-apply (lambda (key value)
265 (when (funcall pred key value)
266 (throw 'map--break (cons key value))))
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 the maps 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-into (map type)
293 "Convert the map MAP into a map of type TYPE.
294
295 TYPE can be one of the following symbols: list or hash-table.
296 MAP can be a list, hash-table or array."
297 (pcase type
298 (`list (map-pairs map))
299 (`hash-table (map--into-hash-table map))
300 (_ (error "Not a map type name: %S" type))))
301
302 (defun map--put (map key v)
303 (map--dispatch map
304 :list (let ((p (assoc key map)))
305 (if p (setcdr p v)
306 (error "No place to change the mapping for %S" key)))
307 :hash-table (puthash key v map)
308 :array (aset map key v)))
309
310 (defun map--apply-alist (function map)
311 "Private function used to apply FUNCTION over MAP, MAP being an alist."
312 (seq-map (lambda (pair)
313 (funcall function
314 (car pair)
315 (cdr pair)))
316 map))
317
318 (defun map--delete (map key)
319 (map--dispatch map
320 :list (error "No place to remove the mapping for %S" key)
321 :hash-table (remhash key map)
322 :array (and (>= key 0)
323 (<= key (seq-length map))
324 (aset map key nil)))
325 map)
326
327 (defun map--apply-hash-table (function map)
328 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
329 (let (result)
330 (maphash (lambda (key value)
331 (push (funcall function key value) result))
332 map)
333 (nreverse result)))
334
335 (defun map--apply-array (function map)
336 "Private function used to apply FUNCTION over MAP, MAP being an array."
337 (let ((index 0))
338 (seq-map (lambda (elt)
339 (prog1
340 (funcall function index elt)
341 (setq index (1+ index))))
342 map)))
343
344 (defun map--into-hash-table (map)
345 "Convert MAP into a hash-table."
346 (let ((ht (make-hash-table :size (map-length map)
347 :test 'equal)))
348 (map-apply (lambda (key value)
349 (setf (map-elt ht key) value))
350 map)
351 ht))
352
353 (defun map--make-pcase-bindings (args)
354 "Return a list of pcase bindings from ARGS to the elements of a map."
355 (seq-map (lambda (elt)
356 (if (consp elt)
357 `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))
358 `(app (pcase--flip map-elt ',elt) ,elt)))
359 args))
360
361 (defun map--make-pcase-patterns (args)
362 "Return a list of `(map ...)' pcase patterns built from ARGS."
363 (cons 'map
364 (seq-map (lambda (elt)
365 (if (and (consp elt) (eq 'map (car elt)))
366 (map--make-pcase-patterns elt)
367 elt))
368 args)))
369
370 (provide 'map)
371 ;;; map.el ends here