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 (defun map-elt (map key &optional default)
48 "Perform a lookup in MAP of KEY and return its associated value.
49 If KEY is not found, return DEFAULT which defaults to nil.
51 If MAP is a list, `assoc' is used to lookup KEY."
53 :list (or (cdr (assoc key map)) default)
54 :hash-table (gethash key map default)
55 :array (or (ignore-errors (elt map key)) default)))
57 (defmacro map-put (map key value)
58 "In MAP, associate KEY with VALUE and return MAP.
59 If KEY is already present in MAP, replace its value with VALUE."
62 (map--dispatch (m ,map m)
63 :list (setq ,map (cons (cons ,key ,value) m))
64 :hash-table (puthash ,key ,value m)
65 :array (aset m ,key ,value))))
67 (defmacro map-delete (map key)
68 "In MAP, delete the key KEY if present and return MAP.
69 If MAP is an array, store nil at the index KEY."
72 (map--dispatch (m ,map m)
73 :list (setq ,map (map--delete-alist m ,key))
74 :hash-table (remhash ,key m)
75 :array (aset m ,key nil))))
77 (defun map-nested-elt (map keys &optional default)
78 "Travserse MAP using KEYS and return the looked up value or DEFAULT if nil.
79 Map can be a nested map composed of alists, hash-tables and arrays."
80 (or (seq-reduce (lambda (acc key)
88 "Return the list of keys in MAP."
89 (map-apply (lambda (key value) key) map))
91 (defun map-values (map)
92 "Return the list of values in MAP."
93 (map-apply (lambda (key value) value) map))
95 (defun map-pairs (map)
96 "Return the elements of MAP as key/value association lists."
97 (map-apply (lambda (key value)
101 (defun map-length (map)
102 "Return the length of MAP."
103 (length (map-keys map)))
105 (defun map-copy (map)
106 "Return a copy of MAP."
109 :hash-table (copy-hash-table map)
110 :array (seq-copy map)))
112 (defun map-apply (function map)
113 "Return the result of applying FUNCTION to each element of MAP.
114 FUNCTION is called with two arguments, the key and the value."
115 (funcall (map--dispatch map
116 :list #'map--apply-alist
117 :hash-table #'map--apply-hash-table
118 :array #'map--apply-array)
122 (defun map-keys-apply (function map)
123 "Return the result of applying FUNCTION to each key of MAP."
124 (map-apply (lambda (key val)
125 (funcall function key))
128 (defun map-values-apply (function map)
129 "Return the result of applying FUNCTION to each value of MAP."
130 (map-apply (lambda (key val)
131 (funcall function val))
134 (defun map-filter (pred map)
135 "Return an alist of the key/val pairs of which (PRED key val) is non-nil in MAP."
136 (delq nil (map-apply (lambda (key val)
137 (if (funcall pred key val)
142 (defun map-remove (pred map)
143 "Return an alist of the key/val pairs of which (PRED key val) is nil in MAP."
144 (map-filter (lambda (key val) (not (funcall pred key val)))
148 "Return non-nil if MAP is a map (list, hash-table or array)."
153 (defun map-empty-p (map)
154 "Return non-nil is MAP is empty.
155 MAP can be a list, hash-table or array."
156 (null (map-keys map)))
158 (defun map-contains-key-p (map key &optional testfn)
159 "Return non-nil if MAP contain the key KEY, nil otherwise.
160 Equality is defined by TESTFN if non-nil or by `equal' if nil.
161 MAP can be a list, hash-table or array."
162 (seq-contains-p (map-keys map) key testfn))
164 (defun map-some-p (pred map)
165 "Return any key/value pair for which (PRED key val) is non-nil is MAP."
167 (map-apply (lambda (key value)
168 (when (funcall pred key value)
169 (throw 'map--break (cons key value))))
173 (defun map-every-p (pred map)
174 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP."
176 (map-apply (lambda (key value)
177 (or (funcall pred key value)
178 (throw 'map--break nil)))
182 (defun map-merge (type &rest maps)
183 "Merge into a map of type TYPE all the key/value pairs in the maps MAPS."
186 (map-apply (lambda (key value)
187 (map-put result key value))
189 (map-into result type)))
191 (defun map-into (map type)
192 "Convert the map MAP into a map of type TYPE.
193 TYPE can be one of the following symbols: list or hash-table."
195 (`list (map-pairs map))
196 (`hash-table (map--into-hash-table map))))
198 (defmacro map--dispatch (spec &rest args)
199 "Evaluate one of the provided forms depending on the type of MAP.
201 SPEC can be a map or a list of the form (VAR MAP [RESULT]).
202 ARGS should have the form [TYPE FORM]...
204 The following keyword types are meaningful: `:list',
205 `:hash-table' and `array'.
207 An error is thrown if MAP is neither a list, hash-table or array.
209 Return RESULT if non-nil or the result of evaluation of the
212 \(fn (VAR MAP [RESULT]) &rest ARGS)"
213 (declare (debug t) (indent 1))
215 (setq spec `(,spec ,spec)))
216 (let ((map-var (car spec))
217 (result-var (make-symbol "result")))
218 `(let ((,map-var ,(cadr spec))
221 (cond ((listp ,map-var) ,(plist-get args :list))
222 ((hash-table-p ,map-var) ,(plist-get args :hash-table))
223 ((arrayp ,map-var) ,(plist-get args :array))
224 (t (error "Unsupported map: %s" ,map-var))))
226 `((setq ,result-var ,@(cddr spec))))
229 (defun map--apply-alist (function map)
230 "Private function used to apply FUNCTION over MAP, MAP being an alist."
231 (seq-map (lambda (pair)
237 (defun map--apply-hash-table (function map)
238 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
240 (maphash (lambda (key value)
241 (push (funcall function key value) result))
245 (defun map--apply-array (function map)
246 "Private function used to apply FUNCTION over MAP, MAP being an array."
248 (seq-map (lambda (elt)
250 (funcall function index elt)
251 (setq index (1+ index))))
254 (defun map--delete-alist (map key)
255 "Return MAP with KEY removed."
256 (seq-remove (lambda (pair)
257 (equal key (car pair)))
260 (defun map--into-hash-table (map)
261 "Convert MAP into a hash-table."
262 (let ((ht (make-hash-table :size (map-length map)
264 (map-apply (lambda (key value)
265 (map-put ht key value))