map.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ;;; map.el --- Map manipulation functions -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Petton <nicolas@petton.fr>
  4. ;; Keywords: convenience, map, hash-table, alist, array
  5. ;; Version: 1.0
  6. ;; Package: map
  7. ;; Maintainer: emacs-devel@gnu.org
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; map.el provides map-manipulation functions that work on alists,
  21. ;; hash-table and arrays. All functions are prefixed with "map-".
  22. ;;
  23. ;; Functions taking a predicate or iterating over a map using a
  24. ;; function take the function as their first argument. All other
  25. ;; functions take the map as their first argument.
  26. ;; TODO:
  27. ;; - Add support for char-tables
  28. ;; - Maybe add support for gv?
  29. ;; - See if we can integrate text-properties
  30. ;; - A macro similar to let-alist but working on any type of map could
  31. ;; be really useful
  32. ;;; Code:
  33. (require 'seq)
  34. (pcase-defmacro map (&rest args)
  35. "pcase pattern matching map elements.
  36. Matches if the object is a map (list, hash-table or array), and
  37. binds values from ARGS to their corresponding elements of the map.
  38. ARGS can be a list elements of the form (KEY PAT), in which case
  39. KEY in an unquoted form.
  40. ARGS can also be a list of symbols, which stands for ('SYMBOL
  41. SYMBOL)."
  42. `(and (pred map-p)
  43. ,@(map--make-pcase-bindings args)))
  44. (defmacro map-let (keys map &rest body)
  45. "Bind the variables in KEYS to the elements of MAP then evaluate BODY.
  46. KEYS can be a list of symbols, in which case each element will be
  47. bound to the looked up value in MAP.
  48. KEYS can also be a list of (KEY VARNAME) pairs, in which case
  49. KEY is an unquoted form.
  50. MAP can be a list, hash-table or array."
  51. (declare (indent 2) (debug t))
  52. `(pcase-let ((,(map--make-pcase-patterns keys) ,map))
  53. ,@body))
  54. (eval-when-compile
  55. (defmacro map--dispatch (map-var &rest args)
  56. "Evaluate one of the forms specified by ARGS based on the type of MAP.
  57. The following keyword types are meaningful: `:list',
  58. `:hash-table' and `:array'.
  59. An error is thrown if MAP is neither a list, hash-table nor array.
  60. Return RESULT if non-nil or the result of evaluation of the form."
  61. (declare (debug t) (indent 1))
  62. `(cond ((listp ,map-var) ,(plist-get args :list))
  63. ((hash-table-p ,map-var) ,(plist-get args :hash-table))
  64. ((arrayp ,map-var) ,(plist-get args :array))
  65. (t (error "Unsupported map: %s" ,map-var)))))
  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.
  69. If MAP is a list, `eql' is used to lookup KEY.
  70. MAP can be a list, hash-table or array."
  71. (declare
  72. (gv-expander
  73. (lambda (do)
  74. (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
  75. (macroexp-let2* nil
  76. ;; Eval them once and for all in the right order.
  77. ((key key) (default default))
  78. `(if (listp ,mgetter)
  79. ;; Special case the alist case, since it can't be handled by the
  80. ;; map--put function.
  81. ,(gv-get `(alist-get ,key (gv-synthetic-place
  82. ,mgetter ,msetter)
  83. ,default)
  84. do)
  85. ,(funcall do `(map-elt ,mgetter ,key ,default)
  86. (lambda (v) `(map--put ,mgetter ,key ,v)))))))))
  87. (map--dispatch map
  88. :list (alist-get key map default)
  89. :hash-table (gethash key map default)
  90. :array (if (and (>= key 0) (< key (seq-length map)))
  91. (seq-elt map key)
  92. default)))
  93. (defmacro map-put (map key value)
  94. "In MAP, associate KEY with VALUE and return MAP.
  95. If KEY is already present in MAP, replace the associated value
  96. with VALUE.
  97. MAP can be a list, hash-table or array."
  98. (macroexp-let2 nil map map
  99. `(progn
  100. (setf (map-elt ,map ,key) ,value)
  101. ,map)))
  102. (defmacro map-delete (map key)
  103. "In MAP, delete the key KEY if present and return MAP.
  104. If MAP is an array, store nil at the index KEY.
  105. MAP can be a list, hash-table or array."
  106. (declare (debug t))
  107. (gv-letplace (mgetter msetter) `(gv-delay-error ,map)
  108. (macroexp-let2 nil key key
  109. `(if (not (listp ,mgetter))
  110. (map--delete ,mgetter ,key)
  111. ;; The alist case is special, since it can't be handled by the
  112. ;; map--delete function.
  113. (setf (alist-get ,key (gv-synthetic-place ,mgetter ,msetter)
  114. nil t)
  115. nil)
  116. ,mgetter))))
  117. (defun map-nested-elt (map keys &optional default)
  118. "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil.
  119. Map can be a nested map composed of alists, hash-tables and arrays."
  120. (or (seq-reduce (lambda (acc key)
  121. (when (map-p acc)
  122. (map-elt acc key)))
  123. keys
  124. map)
  125. default))
  126. (defun map-keys (map)
  127. "Return the list of keys in MAP.
  128. MAP can be a list, hash-table or array."
  129. (map-apply (lambda (key _) key) map))
  130. (defun map-values (map)
  131. "Return the list of values in MAP.
  132. MAP can be a list, hash-table or array."
  133. (map-apply (lambda (_ value) value) map))
  134. (defun map-pairs (map)
  135. "Return the elements of MAP as key/value association lists.
  136. MAP can be a list, hash-table or array."
  137. (map-apply #'cons map))
  138. (defun map-length (map)
  139. "Return the length of MAP.
  140. MAP can be a list, hash-table or array."
  141. (length (map-keys map)))
  142. (defun map-copy (map)
  143. "Return a copy of MAP.
  144. MAP can be a list, hash-table or array."
  145. (map--dispatch map
  146. :list (seq-copy map)
  147. :hash-table (copy-hash-table map)
  148. :array (seq-copy map)))
  149. (defun map-apply (function map)
  150. "Apply FUNCTION to each element of MAP and return the result as a list.
  151. FUNCTION is called with two arguments, the key and the value.
  152. MAP can be a list, hash-table or array."
  153. (funcall (map--dispatch map
  154. :list #'map--apply-alist
  155. :hash-table #'map--apply-hash-table
  156. :array #'map--apply-array)
  157. function
  158. map))
  159. (defun map-keys-apply (function map)
  160. "Return the result of applying FUNCTION to each key of MAP.
  161. MAP can be a list, hash-table or array."
  162. (map-apply (lambda (key _)
  163. (funcall function key))
  164. map))
  165. (defun map-values-apply (function map)
  166. "Return the result of applying FUNCTION to each value of MAP.
  167. MAP can be a list, hash-table or array."
  168. (map-apply (lambda (_ val)
  169. (funcall function val))
  170. map))
  171. (defun map-filter (pred map)
  172. "Return an alist of key/val pairs for which (PRED key val) is non-nil in MAP.
  173. MAP can be a list, hash-table or array."
  174. (delq nil (map-apply (lambda (key val)
  175. (if (funcall pred key val)
  176. (cons key val)
  177. nil))
  178. map)))
  179. (defun map-remove (pred map)
  180. "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP.
  181. MAP can be a list, hash-table or array."
  182. (map-filter (lambda (key val) (not (funcall pred key val)))
  183. map))
  184. (defun map-p (map)
  185. "Return non-nil if MAP is a map (list, hash-table or array)."
  186. (or (listp map)
  187. (hash-table-p map)
  188. (arrayp map)))
  189. (defun map-empty-p (map)
  190. "Return non-nil is MAP is empty.
  191. MAP can be a list, hash-table or array."
  192. (map--dispatch map
  193. :list (null map)
  194. :array (seq-empty-p map)
  195. :hash-table (zerop (hash-table-count map))))
  196. (defun map-contains-key-p (map key &optional testfn)
  197. "Return non-nil if MAP contain the key KEY, nil otherwise.
  198. Equality is defined by TESTFN if non-nil or by `equal' if nil.
  199. MAP can be a list, hash-table or array."
  200. (seq-contains-p (map-keys map) key testfn))
  201. (defun map-some-p (pred map)
  202. "Return a key/value pair for which (PRED key val) is non-nil in MAP.
  203. MAP can be a list, hash-table or array."
  204. (catch 'map--break
  205. (map-apply (lambda (key value)
  206. (when (funcall pred key value)
  207. (throw 'map--break (cons key value))))
  208. map)
  209. nil))
  210. (defun map-every-p (pred map)
  211. "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP.
  212. MAP can be a list, hash-table or array."
  213. (catch 'map--break
  214. (map-apply (lambda (key value)
  215. (or (funcall pred key value)
  216. (throw 'map--break nil)))
  217. map)
  218. t))
  219. (defun map-merge (type &rest maps)
  220. "Merge into a map of type TYPE all the key/value pairs in the maps MAPS.
  221. MAP can be a list, hash-table or array."
  222. (let (result)
  223. (while maps
  224. (map-apply (lambda (key value)
  225. (setf (map-elt result key) value))
  226. (pop maps)))
  227. (map-into result type)))
  228. (defun map-into (map type)
  229. "Convert the map MAP into a map of type TYPE.
  230. TYPE can be one of the following symbols: list or hash-table.
  231. MAP can be a list, hash-table or array."
  232. (pcase type
  233. (`list (map-pairs map))
  234. (`hash-table (map--into-hash-table map))
  235. (_ (error "Not a map type name: %S" type))))
  236. (defun map--put (map key v)
  237. (map--dispatch map
  238. :list (let ((p (assoc key map)))
  239. (if p (setcdr p v)
  240. (error "No place to change the mapping for %S" key)))
  241. :hash-table (puthash key v map)
  242. :array (aset map key v)))
  243. (defun map--apply-alist (function map)
  244. "Private function used to apply FUNCTION over MAP, MAP being an alist."
  245. (seq-map (lambda (pair)
  246. (funcall function
  247. (car pair)
  248. (cdr pair)))
  249. map))
  250. (defun map--delete (map key)
  251. (map--dispatch map
  252. :list (error "No place to remove the mapping for %S" key)
  253. :hash-table (remhash key map)
  254. :array (and (>= key 0)
  255. (<= key (seq-length map))
  256. (aset map key nil)))
  257. map)
  258. (defun map--apply-hash-table (function map)
  259. "Private function used to apply FUNCTION over MAP, MAP being a hash-table."
  260. (let (result)
  261. (maphash (lambda (key value)
  262. (push (funcall function key value) result))
  263. map)
  264. (nreverse result)))
  265. (defun map--apply-array (function map)
  266. "Private function used to apply FUNCTION over MAP, MAP being an array."
  267. (let ((index 0))
  268. (seq-map (lambda (elt)
  269. (prog1
  270. (funcall function index elt)
  271. (setq index (1+ index))))
  272. map)))
  273. (defun map--into-hash-table (map)
  274. "Convert MAP into a hash-table."
  275. (let ((ht (make-hash-table :size (map-length map)
  276. :test 'equal)))
  277. (map-apply (lambda (key value)
  278. (setf (map-elt ht key) value))
  279. map)
  280. ht))
  281. (defun map--make-pcase-bindings (args)
  282. "Return a list of pcase bindings from ARGS to the elements of a map."
  283. (seq-map (lambda (elt)
  284. (if (consp elt)
  285. `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))
  286. `(app (pcase--flip map-elt ',elt) ,elt)))
  287. args))
  288. (defun map--make-pcase-patterns (args)
  289. "Return a list of `(map ...)' pcase patterns built from ARGS."
  290. (cons 'map
  291. (seq-map (lambda (elt)
  292. (if (and (consp elt) (eq 'map (car elt)))
  293. (map--make-pcase-patterns elt)
  294. elt))
  295. args)))
  296. (provide 'map)
  297. ;;; map.el ends here