Klimi's new dotfiles with stow.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

347 рядки
12 KiB

4 роки тому
  1. ;;; a.el --- Associative data structure functions -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 Arne Brasseur
  3. ;; Author: Arne Brasseur <arne@arnebrasseur.net>
  4. ;; URL: https://github.com/plexus/a.el
  5. ;; Package-Version: 20180907.953
  6. ;; Keywords: lisp
  7. ;; Version: 0.1.1
  8. ;; Package-Requires: ((emacs "25"))
  9. ;; This file is not part of GNU Emacs.
  10. ;; This file is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; This file is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to
  20. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Library for dealing with associative data structures: alists, hash-maps, and
  24. ;; vectors (for vectors, the indices are treated as keys).
  25. ;;
  26. ;; This library is largely inspired by Clojure, it has many of the functions
  27. ;; found in clojure.core, prefixed with `a-'. All functions treat their
  28. ;; arguments as immutable, so e.g. `a-assoc' will clone the hash-table or alist
  29. ;; it is given. Keep this in mind when writing performance sensitive code.
  30. ;;; Code:
  31. (eval-when-compile (require 'subr-x)) ;; for things like hash-table-keys
  32. (require 'cl-lib)
  33. (require 'seq)
  34. (defun a-associative-p (obj)
  35. (or (not obj)
  36. (hash-table-p obj)
  37. (and (consp obj) (consp (car obj)))))
  38. (defalias 'a-associative? 'a-associative-p)
  39. (defun a-get (map key &optional not-found)
  40. "Return the value MAP mapped to KEY, NOT-FOUND or nil if key not present."
  41. (cond
  42. ;; own implementation instead of alist-get so keys are checked with equal
  43. ;; instead of eq
  44. ((listp map)
  45. (a--alist-get map key not-found))
  46. ((vectorp map)
  47. (if (a-has-key? map key)
  48. (aref map key)
  49. not-found))
  50. ((hash-table-p map)
  51. (gethash key map not-found))
  52. (t (user-error "Not associative: %S" map))))
  53. (defun a--alist-get (map key &optional not-found)
  54. "Like alist-get, but uses equal instead of eq to look up in map MAP key KEY.
  55. Returns NOT-FOUND if the key is not present, or `nil' if
  56. NOT-FOUND is not specified."
  57. (cl-block nil
  58. (seq-doseq (pair map)
  59. (when (equal (car pair) key)
  60. (cl-return (cdr pair))))
  61. not-found))
  62. (defun a-get-in (m ks &optional not-found)
  63. "Look up a value in a nested associative structure.
  64. Given a data structure M, and a sequence of keys KS, find the
  65. value found by using each key in turn to do a lookup in the next
  66. \"layer\". Return `nil' if the key is not present, or the NOT-FOUND
  67. value if supplied."
  68. (let ((result m))
  69. (cl-block nil
  70. (seq-doseq (k ks)
  71. (if (a-has-key? result k)
  72. (setq result (a-get result k))
  73. (cl-return not-found)))
  74. result)))
  75. (defmacro a-get* (&rest keys)
  76. "Look up a value in a nested associative structure.
  77. Like a-get-in, but takes the key sequence KEYS directly as vararg
  78. arguments, rather than as a single sequence."
  79. (cl-labels ((rec (keys)
  80. `(a-get ,(if (and (consp (cdr keys))
  81. (cddr keys))
  82. (rec (cdr keys))
  83. (cadr keys))
  84. ,(car keys))))
  85. (rec (nreverse keys))))
  86. (defun a-has-key (coll k)
  87. "Check if the given associative collection COLL has a certain key K."
  88. (cond
  89. ((listp coll) (not (eq (a--alist-get coll k :not-found) :not-found)))
  90. ((vectorp coll) (and (integerp k) (< -1 k (length coll))))
  91. ((hash-table-p coll) (not (eq (gethash k coll :not-found) :not-found)))
  92. (t (user-error "Not associative: %S" coll))))
  93. (defalias 'a-has-key? 'a-has-key)
  94. (defun a-assoc-1 (coll k v)
  95. "Like `a-assoc', (in COLL assoc K with V) but only takes a single k-v pair.
  96. Internal helper function."
  97. (cond
  98. ((listp coll)
  99. (if (a-has-key? coll k)
  100. (mapcar (lambda (entry)
  101. (if (equal (car entry) k)
  102. (cons k v)
  103. entry))
  104. coll)
  105. (cons (cons k v) coll)))
  106. ((vectorp coll)
  107. (if (and (integerp k) (> k 0))
  108. (if (< k (length coll))
  109. (let ((copy (copy-sequence coll)))
  110. (aset copy k v)
  111. copy)
  112. (vconcat coll (make-list (- k (length coll)) nil) (list v)))))
  113. ((hash-table-p coll)
  114. (let ((copy (copy-hash-table coll)))
  115. (puthash k v copy)
  116. copy))))
  117. (defun a-assoc (coll &rest kvs)
  118. "Return an updated collection COLL, associating values with keys KVS."
  119. (when (not (cl-evenp (a-count kvs)))
  120. (user-error "a-assoc requires an even number of arguments!"))
  121. (seq-reduce (lambda (coll kv)
  122. (seq-let [k v] kv
  123. (a-assoc-1 coll k v)))
  124. (seq-partition kvs 2)
  125. coll))
  126. (defun a-keys (coll)
  127. "Return the keys in the collection COLL."
  128. (cond
  129. ((listp coll)
  130. (mapcar #'car coll))
  131. ((hash-table-p coll)
  132. (hash-table-keys coll))))
  133. (defun a-vals (coll)
  134. "Return the values in the collection COLL."
  135. (cond
  136. ((listp coll)
  137. (mapcar #'cdr coll))
  138. ((hash-table-p coll)
  139. (hash-table-values coll))))
  140. (defun a-reduce-kv (fn from coll)
  141. "Reduce with FN starting from FROM the collection COLL.
  142. Reduce an associative collection COLL, starting with an initial
  143. value of FROM. The reducing function FN receives the intermediate
  144. value, key, and value."
  145. (seq-reduce (lambda (acc key)
  146. (funcall fn acc key (a-get coll key)))
  147. (a-keys coll)
  148. from))
  149. (defun a-count (coll)
  150. "Count the number of key-value pairs in COLL.
  151. Like length, but can also return the length of hash tables."
  152. (cond
  153. ((seqp coll)
  154. (length coll))
  155. ((hash-table-p coll)
  156. (hash-table-count coll))))
  157. (defun a-equal (a b)
  158. "Compare collections A, B for value equality.
  159. Associative collections (hash tables and a-lists) are considered
  160. equal if they contain equal key-value pairs, regardless of order.
  161. Sequences (lists or vectors) are considered equal if they contain
  162. the same elements in the same order.
  163. Collection elements are compared using `a-equal'. In other words,
  164. the equality check is recursive, resulting in a \"deep\" equality
  165. check.
  166. Anything that isn't associative or a sequence is compared with
  167. `equal'."
  168. (cond
  169. ((and (a-associative? a) (a-associative? b))
  170. (or (equal a b)
  171. (when (eq (a-count a) (a-count b))
  172. (cl-block nil
  173. (seq-doseq (k (a-keys a))
  174. (when (not (a-equal (a-get a k) (a-get b k)))
  175. (cl-return nil)))
  176. t))))
  177. ((and (sequencep a) (sequencep b))
  178. (and (eq (length a) (length b))
  179. (or (and (seq-empty-p a) (seq-empty-p b))
  180. (and (a-equal (elt a 0) (elt b 0))
  181. (a-equal (seq-drop a 1) (seq-drop b 1))))))
  182. (t
  183. (equal a b))))
  184. (defalias 'a-equal? 'a-equal)
  185. (defun a-merge (&rest colls)
  186. "Merge multiple associative collections.
  187. Return the type of the first collection COLLS."
  188. (seq-reduce (lambda (this that)
  189. (a-reduce-kv (lambda (coll k v)
  190. (a-assoc coll k v))
  191. this
  192. that))
  193. (cdr colls)
  194. (car colls)))
  195. (defun a-merge-with (f &rest colls)
  196. "Merge multiple associative collections.
  197. Return the type of the first collection COLLS. If a key exists in
  198. both, then combine the associated values by calling f on them."
  199. (seq-reduce (lambda (this that)
  200. (a-reduce-kv (lambda (coll k v)
  201. (a-assoc coll k (if (a-has-key coll k)
  202. (funcall f v (a-get coll k))
  203. v)))
  204. this
  205. that))
  206. (cdr colls)
  207. (car colls)))
  208. (defun a-alist (&rest kvs)
  209. "Create an association list from the given keys and values KVS.
  210. Arguments are simply provided in sequence, rather than as lists or cons cells.
  211. For example: (a-alist :foo 123 :bar 456)"
  212. (mapcar (lambda (kv) (cons (car kv) (cadr kv))) (seq-partition kvs 2)))
  213. (defalias 'a-list 'a-alist)
  214. (defun a-hash-table (&rest kvs)
  215. "Create a hash table from the given keys and values KVS.
  216. Arguments are simply provided in sequence, rather than as lists
  217. or cons cells. As \"test\" for the hash table, equal is used. The
  218. hash table is created without extra storage space, so with a size
  219. equal to amount of key-value pairs, since it is assumed to be
  220. treated as immutable.
  221. For example: (a-hash-table :foo 123 :bar 456)"
  222. (let* ((kv-pairs (seq-partition kvs 2))
  223. (hash-map (make-hash-table :test 'equal :size (length kv-pairs))))
  224. (seq-do (lambda (pair)
  225. (puthash (car pair) (cadr pair) hash-map))
  226. kv-pairs)
  227. hash-map))
  228. (defun a-assoc-in (coll keys value)
  229. "In collection COLL, at location KEYS, associate value VALUE.
  230. Associates a value in a nested associative collection COLL, where
  231. KEYS is a sequence of keys and VALUE is the new value and returns
  232. a new nested structure. If any levels do not exist, association
  233. lists will be created."
  234. (cl-case (length keys)
  235. (0 coll)
  236. (1 (a-assoc-1 coll (elt keys 0) value))
  237. (t (a-assoc-1 coll
  238. (elt keys 0)
  239. (a-assoc-in (a-get coll (elt keys 0))
  240. (seq-drop keys 1)
  241. value)))))
  242. (defun a-dissoc--list (list keys)
  243. "Return updated LIST with KEYS removed.
  244. Internal helper. Use `a-dissoc' instead."
  245. (a-reduce-kv (lambda (res k v)
  246. (if (member k keys)
  247. res
  248. (cons (cons k v) res)))
  249. nil
  250. list))
  251. (defun a-dissoc--hash-table (table keys)
  252. "Return updated TABLE with KEYS removed.
  253. Internal helper. Use `a-dissoc' instead."
  254. (let ((new-table (make-hash-table :size (hash-table-count table)
  255. :test (hash-table-test table)))
  256. (rest-keys (seq-remove (lambda (k)
  257. (member k keys))
  258. (a-keys table))))
  259. (seq-doseq (k rest-keys)
  260. (puthash k (gethash k table) new-table))
  261. new-table))
  262. (defun a-dissoc (coll &rest keys)
  263. "Return an updated version of collection COLL with the KEY removed."
  264. (cond
  265. ((listp coll) (a-dissoc--list coll keys))
  266. ((hash-table-p coll) (a-dissoc--hash-table coll keys))))
  267. (defun a-update (coll key fn &rest args)
  268. "In collection COLL, at location KEY, apply FN with extra args ARGS.
  269. 'Updates' a value in an associative collection COLL, where KEY is
  270. a key and FN is a function that will take the old value and any
  271. supplied args and return the new value, and returns a new
  272. structure. If the key does not exist, nil is passed as the old
  273. value."
  274. (a-assoc-1 coll
  275. key
  276. (apply #'funcall fn (a-get coll key) args)))
  277. (defun a-update-in (coll keys fn &rest args)
  278. "In collection COLL, at location KEYS, apply FN with extra args ARGS.
  279. 'Updates' a value in a nested associative collection COLL, where
  280. KEYS is a sequence of keys and FN is a function that will take
  281. the old value and any supplied ARGS and return the new value, and
  282. returns a new nested structure. If any levels do not exist,
  283. association lists will be created."
  284. (cl-case (length keys)
  285. (0 coll)
  286. (1 (apply #'a-update coll (elt keys 0) fn args))
  287. (t (a-assoc-1 coll
  288. (elt keys 0)
  289. (apply #'a-update-in
  290. (a-get coll (elt keys 0))
  291. (seq-drop keys 1)
  292. fn
  293. args)))))
  294. (provide 'a)
  295. ;;; a.el ends here