Fix a false negative in `map-elt' with alists and values being nil
[emacs.git] / lisp / emacs-lisp / map.el
blobebf1fe9589abccf7ac7719b4de67301c56c06a92
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
7 ;; Version: 1.0
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 <http://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)
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, `equal' is used to lookup KEY."
52 (map--dispatch map
53 :list (map--elt-list map key default)
54 :hash-table (gethash key map default)
55 :array (map--elt-array 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 the associated value
60 with VALUE."
61 (declare (debug t))
62 `(progn
63 (map--dispatch (m ,map m)
64 :list (setq ,map (cons (cons ,key ,value) m))
65 :hash-table (puthash ,key ,value m)
66 :array (aset m ,key ,value))))
68 (defmacro map-delete (map key)
69 "In MAP, delete the key KEY if present and return MAP.
70 If MAP is an array, store nil at the index KEY."
71 (declare (debug t))
72 `(progn
73 (map--dispatch (m ,map m)
74 :list (setq ,map (map--delete-alist m ,key))
75 :hash-table (remhash ,key m)
76 :array (map--delete-array m ,key))))
78 (defun map-nested-elt (map keys &optional default)
79 "Travserse MAP using KEYS and return the looked up value or DEFAULT if nil.
80 Map can be a nested map composed of alists, hash-tables and arrays."
81 (or (seq-reduce (lambda (acc key)
82 (when (map-p acc)
83 (map-elt acc key)))
84 keys
85 map)
86 default))
88 (defun map-keys (map)
89 "Return the list of keys in MAP."
90 (map-apply (lambda (key _) key) map))
92 (defun map-values (map)
93 "Return the list of values in MAP."
94 (map-apply (lambda (_ value) value) map))
96 (defun map-pairs (map)
97 "Return the elements of MAP as key/value association lists."
98 (map-apply (lambda (key value)
99 (cons key value))
100 map))
102 (defun map-length (map)
103 "Return the length of MAP."
104 (length (map-keys map)))
106 (defun map-copy (map)
107 "Return a copy of MAP."
108 (map--dispatch map
109 :list (seq-copy map)
110 :hash-table (copy-hash-table map)
111 :array (seq-copy map)))
113 (defun map-apply (function map)
114 "Return the result of applying FUNCTION to each element of MAP.
115 FUNCTION is called with two arguments, the key and the value."
116 (funcall (map--dispatch map
117 :list #'map--apply-alist
118 :hash-table #'map--apply-hash-table
119 :array #'map--apply-array)
120 function
121 map))
123 (defun map-keys-apply (function map)
124 "Return the result of applying FUNCTION to each key of MAP."
125 (map-apply (lambda (key _)
126 (funcall function key))
127 map))
129 (defun map-values-apply (function map)
130 "Return the result of applying FUNCTION to each value of MAP."
131 (map-apply (lambda (_ val)
132 (funcall function val))
133 map))
135 (defun map-filter (pred map)
136 "Return an alist of the key/val pairs of which (PRED key val) is non-nil in MAP."
137 (delq nil (map-apply (lambda (key val)
138 (if (funcall pred key val)
139 (cons key val)
140 nil))
141 map)))
143 (defun map-remove (pred map)
144 "Return an alist of the key/val pairs of which (PRED key val) is nil in MAP."
145 (map-filter (lambda (key val) (not (funcall pred key val)))
146 map))
148 (defun map-p (map)
149 "Return non-nil if MAP is a map (list, hash-table or array)."
150 (or (listp map)
151 (hash-table-p map)
152 (arrayp map)))
154 (defun map-empty-p (map)
155 "Return non-nil is MAP is empty.
156 MAP can be a list, hash-table or array."
157 (null (map-keys map)))
159 (defun map-contains-key-p (map key &optional testfn)
160 "Return non-nil if MAP contain the key KEY, nil otherwise.
161 Equality is defined by TESTFN if non-nil or by `equal' if nil.
162 MAP can be a list, hash-table or array."
163 (seq-contains-p (map-keys map) key testfn))
165 (defun map-some-p (pred map)
166 "Return any key/value pair for which (PRED key val) is non-nil is MAP."
167 (catch 'map--break
168 (map-apply (lambda (key value)
169 (when (funcall pred key value)
170 (throw 'map--break (cons key value))))
171 map)
172 nil))
174 (defun map-every-p (pred map)
175 "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP."
176 (catch 'map--break
177 (map-apply (lambda (key value)
178 (or (funcall pred key value)
179 (throw 'map--break nil)))
180 map)
183 (defun map-merge (type &rest maps)
184 "Merge into a map of type TYPE all the key/value pairs in the maps MAPS."
185 (let (result)
186 (while maps
187 (map-apply (lambda (key value)
188 (map-put result key value))
189 (pop maps)))
190 (map-into result type)))
192 (defun map-into (map type)
193 "Convert the map MAP into a map of type TYPE.
194 TYPE can be one of the following symbols: list or hash-table."
195 (pcase type
196 (`list (map-pairs map))
197 (`hash-table (map--into-hash-table map))
198 (t (error "Not a map type name: %S" type))))
200 (defmacro map--dispatch (spec &rest args)
201 "Evaluate one of the provided forms depending on the type of MAP.
203 SPEC can be a map or a list of the form (VAR MAP [RESULT]).
204 ARGS should have the form [TYPE FORM]...
206 The following keyword types are meaningful: `:list',
207 `:hash-table' and `array'.
209 An error is thrown if MAP is neither a list, hash-table nor array.
211 Return RESULT if non-nil or the result of evaluation of the
212 form.
214 \(fn (VAR MAP [RESULT]) &rest ARGS)"
215 (declare (debug t) (indent 1))
216 (unless (listp spec)
217 (setq spec `(,spec ,spec)))
218 (let ((map-var (car spec))
219 (result-var (make-symbol "result")))
220 `(let ((,map-var ,(cadr spec))
221 ,result-var)
222 (setq ,result-var
223 (cond ((listp ,map-var) ,(plist-get args :list))
224 ((hash-table-p ,map-var) ,(plist-get args :hash-table))
225 ((arrayp ,map-var) ,(plist-get args :array))
226 (t (error "Unsupported map: %s" ,map-var))))
227 ,@(when (cddr spec)
228 `((setq ,result-var ,@(cddr spec))))
229 ,result-var)))
231 (defun map--apply-alist (function map)
232 "Private function used to apply FUNCTION over MAP, MAP being an alist."
233 (seq-map (lambda (pair)
234 (funcall function
235 (car pair)
236 (cdr pair)))
237 map))
239 (defun map--apply-hash-table (function map)
240 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
241 (let (result)
242 (maphash (lambda (key value)
243 (push (funcall function key value) result))
244 map)
245 (nreverse result)))
247 (defun map--apply-array (function map)
248 "Private function used to apply FUNCTION over MAP, MAP being an array."
249 (let ((index 0))
250 (seq-map (lambda (elt)
251 (prog1
252 (funcall function index elt)
253 (setq index (1+ index))))
254 map)))
256 (defun map--elt-list (map key &optional default)
257 "Return the element of the list MAP at the index KEY.
258 If KEY is not found, return DEFAULT which defaults to nil."
259 (let ((pair (assoc key map)))
260 (if pair
261 (cdr (assoc key map))
262 default)))
264 (defun map--elt-array (map key &optional default)
265 "Return the element of the array MAP at the index KEY.
266 If KEY is not found, return DEFAULT which defaults to nil."
267 (let ((len (seq-length map)))
268 (or (and (>= key 0)
269 (<= key len)
270 (seq-elt map key))
271 default)))
273 (defun map--delete-alist (map key)
274 "Return MAP with KEY removed."
275 (seq-remove (lambda (pair)
276 (equal key (car pair)))
277 map))
279 (defun map--delete-array (map key)
280 "Set nil in the array MAP at the index KEY if present and return MAP."
281 (let ((len (seq-length map)))
282 (and (>= key 0)
283 (<= key len)
284 (aset m key nil)))
285 map)
287 (defun map--into-hash-table (map)
288 "Convert MAP into a hash-table."
289 (let ((ht (make-hash-table :size (map-length map)
290 :test 'equal)))
291 (map-apply (lambda (key value)
292 (map-put ht key value))
293 map)
294 ht))
296 (provide 'map)
297 ;;; map.el ends here