]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/map.el
Throw an error when converting a map into an unknown map type
[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 (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.
50
51 If MAP is a list, `assoc' is used to lookup KEY."
52 (map--dispatch map
53 :list (or (cdr (assoc key map)) default)
54 :hash-table (gethash key map default)
55 :array (or (ignore-errors (elt map key)) default)))
56
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."
60 (declare (debug t))
61 `(progn
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))))
66
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."
70 (declare (debug t))
71 `(progn
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))))
76
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)
81 (when (map-p acc)
82 (map-elt acc key)))
83 keys
84 map)
85 default))
86
87 (defun map-keys (map)
88 "Return the list of keys in MAP."
89 (map-apply (lambda (key value) key) map))
90
91 (defun map-values (map)
92 "Return the list of values in MAP."
93 (map-apply (lambda (key value) value) map))
94
95 (defun map-pairs (map)
96 "Return the elements of MAP as key/value association lists."
97 (map-apply (lambda (key value)
98 (cons key value))
99 map))
100
101 (defun map-length (map)
102 "Return the length of MAP."
103 (length (map-keys map)))
104
105 (defun map-copy (map)
106 "Return a copy of MAP."
107 (map--dispatch map
108 :list (seq-copy map)
109 :hash-table (copy-hash-table map)
110 :array (seq-copy map)))
111
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)
119 function
120 map))
121
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))
126 map))
127
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))
132 map))
133
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)
138 (cons key val)
139 nil))
140 map)))
141
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)))
145 map))
146
147 (defun map-p (map)
148 "Return non-nil if MAP is a map (list, hash-table or array)."
149 (or (listp map)
150 (hash-table-p map)
151 (arrayp map)))
152
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)))
157
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))
163
164 (defun map-some-p (pred map)
165 "Return any key/value pair for which (PRED key val) is non-nil is MAP."
166 (catch 'map--break
167 (map-apply (lambda (key value)
168 (when (funcall pred key value)
169 (throw 'map--break (cons key value))))
170 map)
171 nil))
172
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."
175 (catch 'map--break
176 (map-apply (lambda (key value)
177 (or (funcall pred key value)
178 (throw 'map--break nil)))
179 map)
180 t))
181
182 (defun map-merge (type &rest maps)
183 "Merge into a map of type TYPE all the key/value pairs in the maps MAPS."
184 (let (result)
185 (while maps
186 (map-apply (lambda (key value)
187 (map-put result key value))
188 (pop maps)))
189 (map-into result type)))
190
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."
194 (pcase type
195 (`list (map-pairs map))
196 (`hash-table (map--into-hash-table map))
197 (t (error "Not a map type name: %s" type))))
198
199 (defmacro map--dispatch (spec &rest args)
200 "Evaluate one of the provided forms depending on the type of MAP.
201
202 SPEC can be a map or a list of the form (VAR MAP [RESULT]).
203 ARGS should have the form [TYPE FORM]...
204
205 The following keyword types are meaningful: `:list',
206 `:hash-table' and `array'.
207
208 An error is thrown if MAP is neither a list, hash-table or array.
209
210 Return RESULT if non-nil or the result of evaluation of the
211 form.
212
213 \(fn (VAR MAP [RESULT]) &rest ARGS)"
214 (declare (debug t) (indent 1))
215 (unless (listp spec)
216 (setq spec `(,spec ,spec)))
217 (let ((map-var (car spec))
218 (result-var (make-symbol "result")))
219 `(let ((,map-var ,(cadr spec))
220 ,result-var)
221 (setq ,result-var
222 (cond ((listp ,map-var) ,(plist-get args :list))
223 ((hash-table-p ,map-var) ,(plist-get args :hash-table))
224 ((arrayp ,map-var) ,(plist-get args :array))
225 (t (error "Unsupported map: %s" ,map-var))))
226 ,@(when (cddr spec)
227 `((setq ,result-var ,@(cddr spec))))
228 ,result-var)))
229
230 (defun map--apply-alist (function map)
231 "Private function used to apply FUNCTION over MAP, MAP being an alist."
232 (seq-map (lambda (pair)
233 (funcall function
234 (car pair)
235 (cdr pair)))
236 map))
237
238 (defun map--apply-hash-table (function map)
239 "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
240 (let (result)
241 (maphash (lambda (key value)
242 (push (funcall function key value) result))
243 map)
244 (nreverse result)))
245
246 (defun map--apply-array (function map)
247 "Private function used to apply FUNCTION over MAP, MAP being an array."
248 (let ((index 0))
249 (seq-map (lambda (elt)
250 (prog1
251 (funcall function index elt)
252 (setq index (1+ index))))
253 map)))
254
255 (defun map--delete-alist (map key)
256 "Return MAP with KEY removed."
257 (seq-remove (lambda (pair)
258 (equal key (car pair)))
259 map))
260
261 (defun map--into-hash-table (map)
262 "Convert MAP into a hash-table."
263 (let ((ht (make-hash-table :size (map-length map)
264 :test 'equal)))
265 (map-apply (lambda (key value)
266 (map-put ht key value))
267 map)
268 ht))
269
270 (provide 'map)
271 ;;; map.el ends here