1 ;;; map.el --- Map manipulation functions -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Keywords: convenience, map, hash-table, alist, array
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 <http://www.gnu.org/licenses/>.
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.
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
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 the corresponding element of the map.
52 ARGS can be a list elements of the form (KEY . PAT) or elements
53 of the form SYMBOL, which stands for (SYMBOL . SYMBOL)."
55 ,@(map--make-pcase-bindings args)))
57 (defmacro map-let (args map &rest body)
58 "Bind the variables in ARGS to the elements of MAP then evaluate BODY.
60 ARGS can be an alist of key/binding pairs or a list of keys. MAP
61 can be a list, hash-table or array."
62 (declare (indent 2) (debug t))
63 `(pcase-let ((,(map--make-pcase-patterns args) ,map))
66 (defmacro map--dispatch (spec &rest args)
67 "Evaluate one of the provided forms depending on the type of MAP.
69 SPEC can be a map or a list of the form (VAR MAP [RESULT]).
70 ARGS should have the form [TYPE FORM]...
72 The following keyword types are meaningful: `:list',
73 `:hash-table' and `array'.
75 An error is thrown if MAP is neither a list, hash-table nor array.
77 Return RESULT if non-nil or the result of evaluation of the
80 \(fn (VAR MAP [RESULT]) &rest ARGS)"
81 (declare (debug t) (indent 1))
83 (setq spec `(,spec ,spec)))
84 (let ((map-var (car spec))
85 (result-var (make-symbol "result")))
86 `(let ((,map-var ,(cadr spec))
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))))
94 `((setq ,result-var ,@(cddr spec))))
97 (defun map-elt (map key &optional default)
98 "Perform a lookup in MAP of KEY and return its associated value.
99 If KEY is not found, return DEFAULT which defaults to nil.
101 If MAP is a list, `equal' is used to lookup KEY.
103 MAP can be a list, hash-table or array."
105 :list (map--elt-list map key default)
106 :hash-table (gethash key map default)
107 :array (map--elt-array map key default)))
109 (defmacro map-put (map key value)
110 "In MAP, associate KEY with VALUE and return MAP.
111 If KEY is already present in MAP, replace the associated value
114 MAP can be a list, hash-table or array."
116 (let ((symbol (symbolp map)))
118 (map--dispatch (m ,map m)
120 (setq ,map (cons (cons ,key ,value) m))
121 (error "Literal lists are not allowed, %s must be a symbol" ',map))
122 :hash-table (puthash ,key ,value m)
123 :array (aset m ,key ,value)))))
125 (defmacro map-delete (map key)
126 "In MAP, delete the key KEY if present and return MAP.
127 If MAP is an array, store nil at the index KEY.
129 MAP can be a list, hash-table or array."
131 (let ((symbol (symbolp map)))
133 (map--dispatch (m ,map m)
135 (setq ,map (map--delete-alist m ,key))
136 (error "Literal lists are not allowed, %s must be a symbol" ',map))
137 :hash-table (remhash ,key m)
138 :array (map--delete-array m ,key)))))
140 (defun map-nested-elt (map keys &optional default)
141 "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil.
143 Map can be a nested map composed of alists, hash-tables and arrays."
144 (or (seq-reduce (lambda (acc key)
151 (defun map-keys (map)
152 "Return the list of keys in MAP.
154 MAP can be a list, hash-table or array."
155 (map-apply (lambda (key _) key) map))
157 (defun map-values (map)
158 "Return the list of values in MAP.
160 MAP can be a list, hash-table or array."
161 (map-apply (lambda (_ value) value) map))
163 (defun map-pairs (map)
164 "Return the elements of MAP as key/value association lists.
166 MAP can be a list, hash-table or array."
167 (map-apply #'cons map))
169 (defun map-length (map)
170 "Return the length of MAP.
172 MAP can be a list, hash-table or array."
173 (length (map-keys map)))
175 (defun map-copy (map)
176 "Return a copy of MAP.
178 MAP can be a list, hash-table or array."
181 :hash-table (copy-hash-table map)
182 :array (seq-copy map)))
184 (defun map-apply (function map)
185 "Apply FUNCTION to each element of MAP and return the result as a list.
186 FUNCTION is called with two arguments, the key and the value.
188 MAP can be a list, hash-table or array."
189 (funcall (map--dispatch map
190 :list #'map--apply-alist
191 :hash-table #'map--apply-hash-table
192 :array #'map--apply-array)
196 (defun map-keys-apply (function map)
197 "Return the result of applying FUNCTION to each key of MAP.
199 MAP can be a list, hash-table or array."
200 (map-apply (lambda (key _)
201 (funcall function key))
204 (defun map-values-apply (function map)
205 "Return the result of applying FUNCTION to each value of MAP.
207 MAP can be a list, hash-table or array."
208 (map-apply (lambda (_ val)
209 (funcall function val))
212 (defun map-filter (pred map)
213 "Return an alist of the key/val pairs for which (PRED key val) is non-nil in MAP.
215 MAP can be a list, hash-table or array."
216 (delq nil (map-apply (lambda (key val)
217 (if (funcall pred key val)
222 (defun map-remove (pred map)
223 "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
225 MAP can be a list, hash-table or array."
226 (map-filter (lambda (key val) (not (funcall pred key val)))
230 "Return non-nil if MAP is a map (list, hash-table or array)."
235 (defun map-empty-p (map)
236 "Return non-nil is MAP is empty.
238 MAP can be a list, hash-table or array."
241 :array (seq-empty-p map)
242 :hash-table (zerop (hash-table-count map))))
244 (defun map-contains-key-p (map key &optional testfn)
245 "Return non-nil if MAP contain the key KEY, nil otherwise.
246 Equality is defined by TESTFN if non-nil or by `equal' if nil.
248 MAP can be a list, hash-table or array."
249 (seq-contains-p (map-keys map) key testfn))
251 (defun map-some-p (pred map)
252 "Return a key/value pair for which (PRED key val) is non-nil in MAP.
254 MAP can be a list, hash-table or array."
256 (map-apply (lambda (key value)
257 (when (funcall pred key value)
258 (throw 'map--break (cons key value))))
262 (defun map-every-p (pred map)
263 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP.
265 MAP can be a list, hash-table or array."
267 (map-apply (lambda (key value)
268 (or (funcall pred key value)
269 (throw 'map--break nil)))
273 (defun map-merge (type &rest maps)
274 "Merge into a map of type TYPE all the key/value pairs in the maps MAPS.
276 MAP can be a list, hash-table or array."
279 (map-apply (lambda (key value)
280 (map-put result key value))
282 (map-into result type)))
284 (defun map-into (map type)
285 "Convert the map MAP into a map of type TYPE.
287 TYPE can be one of the following symbols: list or hash-table.
288 MAP can be a list, hash-table or array."
290 (`list (map-pairs map))
291 (`hash-table (map--into-hash-table map))
292 (t (error "Not a map type name: %S" type))))
294 (defun map--apply-alist (function map)
295 "Private function used to apply FUNCTION over MAP, MAP being an alist."
296 (seq-map (lambda (pair)
302 (defun map--apply-hash-table (function map)
303 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
305 (maphash (lambda (key value)
306 (push (funcall function key value) result))
310 (defun map--apply-array (function map)
311 "Private function used to apply FUNCTION over MAP, MAP being an array."
313 (seq-map (lambda (elt)
315 (funcall function index elt)
316 (setq index (1+ index))))
319 (defun map--elt-list (map key &optional default)
320 "Lookup, in the list MAP, the value associated with KEY and return it.
321 If KEY is not found, return DEFAULT which defaults to nil."
322 (let ((pair (assoc key map)))
327 (defun map--elt-array (map key &optional default)
328 "Return the element of the array MAP at the index KEY.
329 If KEY is not found, return DEFAULT which defaults to nil."
330 (let ((len (seq-length map)))
336 (defun map--delete-alist (map key)
337 "Return MAP with KEY removed."
338 (seq-remove (lambda (pair)
339 (equal key (car pair)))
342 (defun map--delete-array (map key)
343 "Set nil in the array MAP at the index KEY if present and return MAP."
344 (let ((len (seq-length map)))
350 (defun map--into-hash-table (map)
351 "Convert MAP into a hash-table."
352 (let ((ht (make-hash-table :size (map-length map)
354 (map-apply (lambda (key value)
355 (map-put ht key value))
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)
363 `(app (pcase--flip map-elt ',(car elt)) ,(cdr elt))
364 `(app (pcase--flip map-elt ',elt) ,elt)))
367 (defun map--make-pcase-patterns (args)
368 "Return a list of `(map ...)' pcase patterns built from ARGS."
370 (seq-map (lambda (elt)
371 (if (and (consp elt) (eq 'map (car elt)))
372 (map--make-pcase-patterns elt)