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 (defun map-elt (map key &optional default)
67 "Perform a lookup in MAP of KEY and return its associated value.
68 If KEY is not found, return DEFAULT which defaults to nil.
70 If MAP is a list, `equal' is used to lookup KEY.
72 MAP can be a list, hash-table or array."
74 :list (map--elt-list map key default)
75 :hash-table (gethash key map default)
76 :array (map--elt-array map key default)))
78 (defmacro map-put (map key value)
79 "In MAP, associate KEY with VALUE and return MAP.
80 If KEY is already present in MAP, replace the associated value
83 MAP can be a list, hash-table or array."
86 (map--dispatch (m ,map m)
87 :list (setq ,map (cons (cons ,key ,value) m))
88 :hash-table (puthash ,key ,value m)
89 :array (aset m ,key ,value))))
91 (defmacro map-delete (map key)
92 "In MAP, delete the key KEY if present and return MAP.
93 If MAP is an array, store nil at the index KEY.
95 MAP can be a list, hash-table or array."
98 (map--dispatch (m ,map m)
99 :list (setq ,map (map--delete-alist m ,key))
100 :hash-table (remhash ,key m)
101 :array (map--delete-array m ,key))))
103 (defun map-nested-elt (map keys &optional default)
104 "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil.
106 Map can be a nested map composed of alists, hash-tables and arrays."
107 (or (seq-reduce (lambda (acc key)
114 (defun map-keys (map)
115 "Return the list of keys in MAP.
117 MAP can be a list, hash-table or array."
118 (map-apply (lambda (key _) key) map))
120 (defun map-values (map)
121 "Return the list of values in MAP.
123 MAP can be a list, hash-table or array."
124 (map-apply (lambda (_ value) value) map))
126 (defun map-pairs (map)
127 "Return the elements of MAP as key/value association lists.
129 MAP can be a list, hash-table or array."
130 (map-apply #'cons map))
132 (defun map-length (map)
133 "Return the length of MAP.
135 MAP can be a list, hash-table or array."
136 (length (map-keys map)))
138 (defun map-copy (map)
139 "Return a copy of MAP.
141 MAP can be a list, hash-table or array."
144 :hash-table (copy-hash-table map)
145 :array (seq-copy map)))
147 (defun map-apply (function map)
148 "Apply FUNCTION to each element of MAP and return the result as a list.
149 FUNCTION is called with two arguments, the key and the value.
151 MAP can be a list, hash-table or array."
152 (funcall (map--dispatch map
153 :list #'map--apply-alist
154 :hash-table #'map--apply-hash-table
155 :array #'map--apply-array)
159 (defun map-keys-apply (function map)
160 "Return the result of applying FUNCTION to each key of MAP.
162 MAP can be a list, hash-table or array."
163 (map-apply (lambda (key _)
164 (funcall function key))
167 (defun map-values-apply (function map)
168 "Return the result of applying FUNCTION to each value of MAP.
170 MAP can be a list, hash-table or array."
171 (map-apply (lambda (_ val)
172 (funcall function val))
175 (defun map-filter (pred map)
176 "Return an alist of the key/val pairs for which (PRED key val) is non-nil in MAP.
178 MAP can be a list, hash-table or array."
179 (delq nil (map-apply (lambda (key val)
180 (if (funcall pred key val)
185 (defun map-remove (pred map)
186 "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
188 MAP can be a list, hash-table or array."
189 (map-filter (lambda (key val) (not (funcall pred key val)))
193 "Return non-nil if MAP is a map (list, hash-table or array)."
198 (defun map-empty-p (map)
199 "Return non-nil is MAP is empty.
201 MAP can be a list, hash-table or array."
204 :array (seq-empty-p map)
205 :hash-table (zerop (hash-table-count map))))
207 (defun map-contains-key-p (map key &optional testfn)
208 "Return non-nil if MAP contain the key KEY, nil otherwise.
209 Equality is defined by TESTFN if non-nil or by `equal' if nil.
211 MAP can be a list, hash-table or array."
212 (seq-contains-p (map-keys map) key testfn))
214 (defun map-some-p (pred map)
215 "Return a key/value pair for which (PRED key val) is non-nil in MAP.
217 MAP can be a list, hash-table or array."
219 (map-apply (lambda (key value)
220 (when (funcall pred key value)
221 (throw 'map--break (cons key value))))
225 (defun map-every-p (pred map)
226 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP.
228 MAP can be a list, hash-table or array."
230 (map-apply (lambda (key value)
231 (or (funcall pred key value)
232 (throw 'map--break nil)))
236 (defun map-merge (type &rest maps)
237 "Merge into a map of type TYPE all the key/value pairs in the maps MAPS.
239 MAP can be a list, hash-table or array."
242 (map-apply (lambda (key value)
243 (map-put result key value))
245 (map-into result type)))
247 (defun map-into (map type)
248 "Convert the map MAP into a map of type TYPE.
250 TYPE can be one of the following symbols: list or hash-table.
251 MAP can be a list, hash-table or array."
253 (`list (map-pairs map))
254 (`hash-table (map--into-hash-table map))
255 (t (error "Not a map type name: %S" type))))
257 (defmacro map--dispatch (spec &rest args)
258 "Evaluate one of the provided forms depending on the type of MAP.
260 SPEC can be a map or a list of the form (VAR MAP [RESULT]).
261 ARGS should have the form [TYPE FORM]...
263 The following keyword types are meaningful: `:list',
264 `:hash-table' and `array'.
266 An error is thrown if MAP is neither a list, hash-table nor array.
268 Return RESULT if non-nil or the result of evaluation of the
271 \(fn (VAR MAP [RESULT]) &rest ARGS)"
272 (declare (debug t) (indent 1))
274 (setq spec `(,spec ,spec)))
275 (let ((map-var (car spec))
276 (result-var (make-symbol "result")))
277 `(let ((,map-var ,(cadr spec))
280 (cond ((listp ,map-var) ,(plist-get args :list))
281 ((hash-table-p ,map-var) ,(plist-get args :hash-table))
282 ((arrayp ,map-var) ,(plist-get args :array))
283 (t (error "Unsupported map: %s" ,map-var))))
285 `((setq ,result-var ,@(cddr spec))))
288 (defun map--apply-alist (function map)
289 "Private function used to apply FUNCTION over MAP, MAP being an alist."
290 (seq-map (lambda (pair)
296 (defun map--apply-hash-table (function map)
297 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
299 (maphash (lambda (key value)
300 (push (funcall function key value) result))
304 (defun map--apply-array (function map)
305 "Private function used to apply FUNCTION over MAP, MAP being an array."
307 (seq-map (lambda (elt)
309 (funcall function index elt)
310 (setq index (1+ index))))
313 (defun map--elt-list (map key &optional default)
314 "Lookup, in the list MAP, the value associated with KEY and return it.
315 If KEY is not found, return DEFAULT which defaults to nil."
316 (let ((pair (assoc key map)))
321 (defun map--elt-array (map key &optional default)
322 "Return the element of the array MAP at the index KEY.
323 If KEY is not found, return DEFAULT which defaults to nil."
324 (let ((len (seq-length map)))
330 (defun map--delete-alist (map key)
331 "Return MAP with KEY removed."
332 (seq-remove (lambda (pair)
333 (equal key (car pair)))
336 (defun map--delete-array (map key)
337 "Set nil in the array MAP at the index KEY if present and return MAP."
338 (let ((len (seq-length map)))
344 (defun map--into-hash-table (map)
345 "Convert MAP into a hash-table."
346 (let ((ht (make-hash-table :size (map-length map)
348 (map-apply (lambda (key value)
349 (map-put ht key value))
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)
357 `(app (pcase--flip map-elt ',(car elt)) ,(cdr elt))
358 `(app (pcase--flip map-elt ',elt) ,elt)))
361 (defun map--make-pcase-patterns (args)
362 "Return a list of `(map ...)' pcase patterns built from ARGS."
364 (seq-map (lambda (elt)
365 (if (and (consp elt) (eq 'map (car elt)))
366 (map--make-pcase-patterns elt)