diff options
| author | Nicolas Petton | 2015-05-16 11:30:12 +0200 |
|---|---|---|
| committer | Nicolas Petton | 2015-05-16 11:30:12 +0200 |
| commit | 3fe404ca668c10763b1fcb1af3e56b7989d163a0 (patch) | |
| tree | 71ed4ef01239ec16870c835f1c8429eff73f9132 | |
| parent | a5237a049981dbad2ecc3b17d47257ce164a8e70 (diff) | |
| download | emacs-3fe404ca668c10763b1fcb1af3e56b7989d163a0.tar.gz emacs-3fe404ca668c10763b1fcb1af3e56b7989d163a0.zip | |
Improve the docstring of functions in map.el
Since a map is not a data structure but a concept, adding information
about the possible types of maps can be useful information.
* lisp/emacs-lisp/map.el: Add documentation about the type of MAP to
each public function.
| -rw-r--r-- | lisp/emacs-lisp/map.el | 77 |
1 files changed, 58 insertions, 19 deletions
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el index 18d2963f46c..8801b2aba7a 100644 --- a/lisp/emacs-lisp/map.el +++ b/lisp/emacs-lisp/map.el | |||
| @@ -48,7 +48,9 @@ | |||
| 48 | "Perform a lookup in MAP of KEY and return its associated value. | 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. | 49 | If KEY is not found, return DEFAULT which defaults to nil. |
| 50 | 50 | ||
| 51 | If MAP is a list, `equal' is used to lookup KEY." | 51 | If MAP is a list, `equal' is used to lookup KEY. |
| 52 | |||
| 53 | MAP can be a list, hash-table or array." | ||
| 52 | (map--dispatch map | 54 | (map--dispatch map |
| 53 | :list (map--elt-list map key default) | 55 | :list (map--elt-list map key default) |
| 54 | :hash-table (gethash key map default) | 56 | :hash-table (gethash key map default) |
| @@ -57,7 +59,9 @@ If MAP is a list, `equal' is used to lookup KEY." | |||
| 57 | (defmacro map-put (map key value) | 59 | (defmacro map-put (map key value) |
| 58 | "In MAP, associate KEY with VALUE and return MAP. | 60 | "In MAP, associate KEY with VALUE and return MAP. |
| 59 | If KEY is already present in MAP, replace the associated value | 61 | If KEY is already present in MAP, replace the associated value |
| 60 | with VALUE." | 62 | with VALUE. |
| 63 | |||
| 64 | MAP can be a list, hash-table or array." | ||
| 61 | (declare (debug t)) | 65 | (declare (debug t)) |
| 62 | `(progn | 66 | `(progn |
| 63 | (map--dispatch (m ,map m) | 67 | (map--dispatch (m ,map m) |
| @@ -67,7 +71,9 @@ with VALUE." | |||
| 67 | 71 | ||
| 68 | (defmacro map-delete (map key) | 72 | (defmacro map-delete (map key) |
| 69 | "In MAP, delete the key KEY if present and return MAP. | 73 | "In MAP, delete the key KEY if present and return MAP. |
| 70 | If MAP is an array, store nil at the index KEY." | 74 | If MAP is an array, store nil at the index KEY. |
| 75 | |||
| 76 | MAP can be a list, hash-table or array." | ||
| 71 | (declare (debug t)) | 77 | (declare (debug t)) |
| 72 | `(progn | 78 | `(progn |
| 73 | (map--dispatch (m ,map m) | 79 | (map--dispatch (m ,map m) |
| @@ -77,6 +83,7 @@ If MAP is an array, store nil at the index KEY." | |||
| 77 | 83 | ||
| 78 | (defun map-nested-elt (map keys &optional default) | 84 | (defun map-nested-elt (map keys &optional default) |
| 79 | "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil. | 85 | "Traverse MAP using KEYS and return the looked up value or DEFAULT if nil. |
| 86 | |||
| 80 | Map can be a nested map composed of alists, hash-tables and arrays." | 87 | Map can be a nested map composed of alists, hash-tables and arrays." |
| 81 | (or (seq-reduce (lambda (acc key) | 88 | (or (seq-reduce (lambda (acc key) |
| 82 | (when (map-p acc) | 89 | (when (map-p acc) |
| @@ -86,23 +93,33 @@ Map can be a nested map composed of alists, hash-tables and arrays." | |||
| 86 | default)) | 93 | default)) |
| 87 | 94 | ||
| 88 | (defun map-keys (map) | 95 | (defun map-keys (map) |
| 89 | "Return the list of keys in MAP." | 96 | "Return the list of keys in MAP. |
| 97 | |||
| 98 | MAP can be a list, hash-table or array." | ||
| 90 | (map-apply (lambda (key _) key) map)) | 99 | (map-apply (lambda (key _) key) map)) |
| 91 | 100 | ||
| 92 | (defun map-values (map) | 101 | (defun map-values (map) |
| 93 | "Return the list of values in MAP." | 102 | "Return the list of values in MAP. |
| 103 | |||
| 104 | MAP can be a list, hash-table or array." | ||
| 94 | (map-apply (lambda (_ value) value) map)) | 105 | (map-apply (lambda (_ value) value) map)) |
| 95 | 106 | ||
| 96 | (defun map-pairs (map) | 107 | (defun map-pairs (map) |
| 97 | "Return the elements of MAP as key/value association lists." | 108 | "Return the elements of MAP as key/value association lists. |
| 109 | |||
| 110 | MAP can be a list, hash-table or array." | ||
| 98 | (map-apply #'cons map)) | 111 | (map-apply #'cons map)) |
| 99 | 112 | ||
| 100 | (defun map-length (map) | 113 | (defun map-length (map) |
| 101 | "Return the length of MAP." | 114 | "Return the length of MAP. |
| 115 | |||
| 116 | MAP can be a list, hash-table or array." | ||
| 102 | (length (map-keys map))) | 117 | (length (map-keys map))) |
| 103 | 118 | ||
| 104 | (defun map-copy (map) | 119 | (defun map-copy (map) |
| 105 | "Return a copy of MAP." | 120 | "Return a copy of MAP. |
| 121 | |||
| 122 | MAP can be a list, hash-table or array." | ||
| 106 | (map--dispatch map | 123 | (map--dispatch map |
| 107 | :list (seq-copy map) | 124 | :list (seq-copy map) |
| 108 | :hash-table (copy-hash-table map) | 125 | :hash-table (copy-hash-table map) |
| @@ -110,7 +127,9 @@ Map can be a nested map composed of alists, hash-tables and arrays." | |||
| 110 | 127 | ||
| 111 | (defun map-apply (function map) | 128 | (defun map-apply (function map) |
| 112 | "Apply FUNCTION to each element of MAP and return the result as a list. | 129 | "Apply FUNCTION to each element of MAP and return the result as a list. |
| 113 | FUNCTION is called with two arguments, the key and the value." | 130 | FUNCTION is called with two arguments, the key and the value. |
| 131 | |||
| 132 | MAP can be a list, hash-table or array." | ||
| 114 | (funcall (map--dispatch map | 133 | (funcall (map--dispatch map |
| 115 | :list #'map--apply-alist | 134 | :list #'map--apply-alist |
| 116 | :hash-table #'map--apply-hash-table | 135 | :hash-table #'map--apply-hash-table |
| @@ -119,19 +138,25 @@ FUNCTION is called with two arguments, the key and the value." | |||
| 119 | map)) | 138 | map)) |
| 120 | 139 | ||
| 121 | (defun map-keys-apply (function map) | 140 | (defun map-keys-apply (function map) |
| 122 | "Return the result of applying FUNCTION to each key of MAP." | 141 | "Return the result of applying FUNCTION to each key of MAP. |
| 142 | |||
| 143 | MAP can be a list, hash-table or array." | ||
| 123 | (map-apply (lambda (key _) | 144 | (map-apply (lambda (key _) |
| 124 | (funcall function key)) | 145 | (funcall function key)) |
| 125 | map)) | 146 | map)) |
| 126 | 147 | ||
| 127 | (defun map-values-apply (function map) | 148 | (defun map-values-apply (function map) |
| 128 | "Return the result of applying FUNCTION to each value of MAP." | 149 | "Return the result of applying FUNCTION to each value of MAP. |
| 150 | |||
| 151 | MAP can be a list, hash-table or array." | ||
| 129 | (map-apply (lambda (_ val) | 152 | (map-apply (lambda (_ val) |
| 130 | (funcall function val)) | 153 | (funcall function val)) |
| 131 | map)) | 154 | map)) |
| 132 | 155 | ||
| 133 | (defun map-filter (pred map) | 156 | (defun map-filter (pred map) |
| 134 | "Return an alist of the key/val pairs for which (PRED key val) is non-nil in MAP." | 157 | "Return an alist of the key/val pairs for which (PRED key val) is non-nil in MAP. |
| 158 | |||
| 159 | MAP can be a list, hash-table or array." | ||
| 135 | (delq nil (map-apply (lambda (key val) | 160 | (delq nil (map-apply (lambda (key val) |
| 136 | (if (funcall pred key val) | 161 | (if (funcall pred key val) |
| 137 | (cons key val) | 162 | (cons key val) |
| @@ -139,7 +164,9 @@ FUNCTION is called with two arguments, the key and the value." | |||
| 139 | map))) | 164 | map))) |
| 140 | 165 | ||
| 141 | (defun map-remove (pred map) | 166 | (defun map-remove (pred map) |
| 142 | "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP." | 167 | "Return an alist of the key/val pairs for which (PRED key val) is nil in MAP. |
| 168 | |||
| 169 | MAP can be a list, hash-table or array." | ||
| 143 | (map-filter (lambda (key val) (not (funcall pred key val))) | 170 | (map-filter (lambda (key val) (not (funcall pred key val))) |
| 144 | map)) | 171 | map)) |
| 145 | 172 | ||
| @@ -150,7 +177,9 @@ FUNCTION is called with two arguments, the key and the value." | |||
| 150 | (arrayp map))) | 177 | (arrayp map))) |
| 151 | 178 | ||
| 152 | (defun map-empty-p (map) | 179 | (defun map-empty-p (map) |
| 153 | "Return non-nil is MAP is empty." | 180 | "Return non-nil is MAP is empty. |
| 181 | |||
| 182 | MAP can be a list, hash-table or array." | ||
| 154 | (map--dispatch map | 183 | (map--dispatch map |
| 155 | :list (null map) | 184 | :list (null map) |
| 156 | :array (seq-empty-p map) | 185 | :array (seq-empty-p map) |
| @@ -158,11 +187,15 @@ FUNCTION is called with two arguments, the key and the value." | |||
| 158 | 187 | ||
| 159 | (defun map-contains-key-p (map key &optional testfn) | 188 | (defun map-contains-key-p (map key &optional testfn) |
| 160 | "Return non-nil if MAP contain the key KEY, nil otherwise. | 189 | "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." | 190 | Equality is defined by TESTFN if non-nil or by `equal' if nil. |
| 191 | |||
| 192 | MAP can be a list, hash-table or array." | ||
| 162 | (seq-contains-p (map-keys map) key testfn)) | 193 | (seq-contains-p (map-keys map) key testfn)) |
| 163 | 194 | ||
| 164 | (defun map-some-p (pred map) | 195 | (defun map-some-p (pred map) |
| 165 | "Return a key/value pair for which (PRED key val) is non-nil in MAP." | 196 | "Return a key/value pair for which (PRED key val) is non-nil in MAP. |
| 197 | |||
| 198 | MAP can be a list, hash-table or array." | ||
| 166 | (catch 'map--break | 199 | (catch 'map--break |
| 167 | (map-apply (lambda (key value) | 200 | (map-apply (lambda (key value) |
| 168 | (when (funcall pred key value) | 201 | (when (funcall pred key value) |
| @@ -171,7 +204,9 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." | |||
| 171 | nil)) | 204 | nil)) |
| 172 | 205 | ||
| 173 | (defun map-every-p (pred map) | 206 | (defun map-every-p (pred map) |
| 174 | "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP." | 207 | "Return non-nil if (PRED key val) is non-nil for all elements of the map MAP. |
| 208 | |||
| 209 | MAP can be a list, hash-table or array." | ||
| 175 | (catch 'map--break | 210 | (catch 'map--break |
| 176 | (map-apply (lambda (key value) | 211 | (map-apply (lambda (key value) |
| 177 | (or (funcall pred key value) | 212 | (or (funcall pred key value) |
| @@ -180,7 +215,9 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." | |||
| 180 | t)) | 215 | t)) |
| 181 | 216 | ||
| 182 | (defun map-merge (type &rest maps) | 217 | (defun map-merge (type &rest maps) |
| 183 | "Merge into a map of type TYPE all the key/value pairs in the maps MAPS." | 218 | "Merge into a map of type TYPE all the key/value pairs in the maps MAPS. |
| 219 | |||
| 220 | MAP can be a list, hash-table or array." | ||
| 184 | (let (result) | 221 | (let (result) |
| 185 | (while maps | 222 | (while maps |
| 186 | (map-apply (lambda (key value) | 223 | (map-apply (lambda (key value) |
| @@ -190,7 +227,9 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." | |||
| 190 | 227 | ||
| 191 | (defun map-into (map type) | 228 | (defun map-into (map type) |
| 192 | "Convert the map MAP into a map of type TYPE. | 229 | "Convert the map MAP into a map of type TYPE. |
| 193 | TYPE can be one of the following symbols: list or hash-table." | 230 | |
| 231 | TYPE can be one of the following symbols: list or hash-table. | ||
| 232 | MAP can be a list, hash-table or array." | ||
| 194 | (pcase type | 233 | (pcase type |
| 195 | (`list (map-pairs map)) | 234 | (`list (map-pairs map)) |
| 196 | (`hash-table (map--into-hash-table map)) | 235 | (`hash-table (map--into-hash-table map)) |