README.md (104039B)
1 [![CI](https://github.com/magnars/dash.el/actions/workflows/test.yml/badge.svg)](https://github.com/magnars/dash.el/actions/workflows/test.yml) 2 [![GNU ELPA](https://elpa.gnu.org/packages/dash.svg)](https://elpa.gnu.org/packages/dash.html) 3 [![GNU-devel ELPA](https://elpa.gnu.org/devel/dash.svg)](https://elpa.gnu.org/devel/dash.html) 4 [![MELPA Stable](https://stable.melpa.org/packages/dash-badge.svg)](https://stable.melpa.org/#/dash) 5 [![MELPA](https://melpa.org/packages/dash-badge.svg)](https://melpa.org/#/dash) 6 7 # <img align="right" src="rainbow-dash.png"> dash.el 8 9 A modern list API for Emacs. No 10 [`'cl`](https://gnu.org/software/emacs/manual/html_node/cl/) required. 11 12 See the end of the file for license conditions. 13 14 ## Contents 15 16 * [Change log](#change-log) 17 * [Upcoming breaking change!](#upcoming-breaking-change) 18 * [Installation](#installation) 19 * [Functions](#functions) 20 * [Contribute](#contribute) 21 * [Contributors](#contributors) 22 * [License](#license) 23 24 ## Change log 25 26 See the [`NEWS.md`](NEWS.md) file. 27 28 ### Upcoming breaking change! 29 30 - For backward compatibility reasons, `-zip` when called with two 31 lists returns a list of cons cells, rather than a list of proper 32 lists. This is a clunky API, and may be changed in a future release 33 to always return a list of proper lists, as `-zip-lists` currently 34 does. 35 36 **N.B.:** Do not rely on the current behavior of `-zip` for two 37 lists. Instead, use `-zip-pair` for a list of cons cells, and 38 `-zip-lists` for a list of proper lists. 39 40 ## Installation 41 42 Dash is available on [GNU ELPA](https://elpa.gnu.org/), [GNU-devel 43 ELPA](https://elpa.gnu.org/devel/), and [MELPA](https://melpa.org/), 44 and can be installed with the standard command `package-install`: 45 46 M-x package-install RET dash RET 47 48 See [`(info "(emacs) Package 49 Installation")`](https://gnu.org/software/emacs/manual/html_node/emacs/Package-Installation.html). 50 51 Alternatively, you can just dump `dash.el` in your `load-path` 52 somewhere. See [`(info "(emacs) Lisp 53 Libraries")`](https://gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html). 54 55 ### Using in a package 56 57 Add something like this to the library's headers: 58 59 ;; Package-Requires: ((dash "2.19.1")) 60 61 See [`(info "(elisp) Library 62 Headers")`](https://gnu.org/software/emacs/manual/html_node/elisp/Library-Headers.html). 63 64 ### Fontification of special variables 65 66 Font lock of special Dash variables (`it`, `acc`, etc.) in Emacs Lisp 67 buffers can optionally be enabled with the autoloaded minor mode 68 `dash-fontify-mode`. In older Emacs versions which do not dynamically 69 detect macros, the minor mode also fontifies Dash macro calls. 70 71 To automatically enable the minor mode in all Emacs Lisp buffers, just 72 call its autoloaded global counterpart `global-dash-fontify-mode`, 73 either interactively or from your `user-init-file`: 74 75 ```el 76 (global-dash-fontify-mode) 77 ``` 78 79 ### Info symbol lookup 80 81 While editing Elisp files, you can use `C-h S` (`info-lookup-symbol`) 82 to look up Elisp symbols in the relevant Info manuals (see [`(emacs) 83 Info 84 Lookup`](https://gnu.org/software/emacs/manual/html_node/emacs/Info-Lookup.html)). 85 To enable the same for Dash symbols, use the command 86 `dash-register-info-lookup`. It can be called directly when needed, 87 or automatically from your `user-init-file`. For example: 88 89 ```el 90 (with-eval-after-load 'info-look 91 (dash-register-info-lookup)) 92 ``` 93 94 ## Functions 95 96 All functions and constructs in the library use a dash (`-`) prefix. 97 98 The library also provides anaphoric macro versions of functions where 99 that makes sense. The names of these macros are prefixed with two 100 dashes (`--`) instead of one. 101 102 While `-map` applies a function to each element of a list, its 103 anaphoric counterpart `--map` evaluates a form with the local variable 104 `it` temporarily bound to the current list element instead. For 105 example: 106 107 ```el 108 (-map (lambda (n) (* n n)) '(1 2 3 4)) ; Normal version. 109 (--map (* it it) '(1 2 3 4)) ; Anaphoric version. 110 ``` 111 112 The normal version can of course also be written as follows: 113 114 ```el 115 (defun my-square (n) 116 "Return N multiplied by itself." 117 (* n n)) 118 119 (-map #'my-square '(1 2 3 4)) 120 ``` 121 122 This demonstrates the utility of both versions. 123 124 ### Maps 125 126 Functions in this category take a transforming function, which 127 is then applied sequentially to each or selected elements of the 128 input list. The results are collected in order and returned as a 129 new list. 130 131 * [`-map`](#-map-fn-list) `(fn list)` 132 * [`-map-when`](#-map-when-pred-rep-list) `(pred rep list)` 133 * [`-map-first`](#-map-first-pred-rep-list) `(pred rep list)` 134 * [`-map-last`](#-map-last-pred-rep-list) `(pred rep list)` 135 * [`-map-indexed`](#-map-indexed-fn-list) `(fn list)` 136 * [`-annotate`](#-annotate-fn-list) `(fn list)` 137 * [`-splice`](#-splice-pred-fun-list) `(pred fun list)` 138 * [`-splice-list`](#-splice-list-pred-new-list-list) `(pred new-list list)` 139 * [`-mapcat`](#-mapcat-fn-list) `(fn list)` 140 * [`-copy`](#-copy-list) `(list)` 141 142 ### Sublist selection 143 144 Functions returning a sublist of the original list. 145 146 * [`-filter`](#-filter-pred-list) `(pred list)` 147 * [`-remove`](#-remove-pred-list) `(pred list)` 148 * [`-remove-first`](#-remove-first-pred-list) `(pred list)` 149 * [`-remove-last`](#-remove-last-pred-list) `(pred list)` 150 * [`-remove-item`](#-remove-item-item-list) `(item list)` 151 * [`-non-nil`](#-non-nil-list) `(list)` 152 * [`-slice`](#-slice-list-from-optional-to-step) `(list from &optional to step)` 153 * [`-take`](#-take-n-list) `(n list)` 154 * [`-take-last`](#-take-last-n-list) `(n list)` 155 * [`-drop`](#-drop-n-list) `(n list)` 156 * [`-drop-last`](#-drop-last-n-list) `(n list)` 157 * [`-take-while`](#-take-while-pred-list) `(pred list)` 158 * [`-drop-while`](#-drop-while-pred-list) `(pred list)` 159 * [`-select-by-indices`](#-select-by-indices-indices-list) `(indices list)` 160 * [`-select-columns`](#-select-columns-columns-table) `(columns table)` 161 * [`-select-column`](#-select-column-column-table) `(column table)` 162 163 ### List to list 164 165 Functions returning a modified copy of the input list. 166 167 * [`-keep`](#-keep-fn-list) `(fn list)` 168 * [`-concat`](#-concat-rest-lists) `(&rest lists)` 169 * [`-flatten`](#-flatten-l) `(l)` 170 * [`-flatten-n`](#-flatten-n-num-list) `(num list)` 171 * [`-replace`](#-replace-old-new-list) `(old new list)` 172 * [`-replace-first`](#-replace-first-old-new-list) `(old new list)` 173 * [`-replace-last`](#-replace-last-old-new-list) `(old new list)` 174 * [`-insert-at`](#-insert-at-n-x-list) `(n x list)` 175 * [`-replace-at`](#-replace-at-n-x-list) `(n x list)` 176 * [`-update-at`](#-update-at-n-func-list) `(n func list)` 177 * [`-remove-at`](#-remove-at-n-list) `(n list)` 178 * [`-remove-at-indices`](#-remove-at-indices-indices-list) `(indices list)` 179 180 ### Reductions 181 182 Functions reducing lists to a single value (which may also be a list). 183 184 * [`-reduce-from`](#-reduce-from-fn-init-list) `(fn init list)` 185 * [`-reduce-r-from`](#-reduce-r-from-fn-init-list) `(fn init list)` 186 * [`-reduce`](#-reduce-fn-list) `(fn list)` 187 * [`-reduce-r`](#-reduce-r-fn-list) `(fn list)` 188 * [`-reductions-from`](#-reductions-from-fn-init-list) `(fn init list)` 189 * [`-reductions-r-from`](#-reductions-r-from-fn-init-list) `(fn init list)` 190 * [`-reductions`](#-reductions-fn-list) `(fn list)` 191 * [`-reductions-r`](#-reductions-r-fn-list) `(fn list)` 192 * [`-count`](#-count-pred-list) `(pred list)` 193 * [`-sum`](#-sum-list) `(list)` 194 * [`-running-sum`](#-running-sum-list) `(list)` 195 * [`-product`](#-product-list) `(list)` 196 * [`-running-product`](#-running-product-list) `(list)` 197 * [`-inits`](#-inits-list) `(list)` 198 * [`-tails`](#-tails-list) `(list)` 199 * [`-common-prefix`](#-common-prefix-rest-lists) `(&rest lists)` 200 * [`-common-suffix`](#-common-suffix-rest-lists) `(&rest lists)` 201 * [`-min`](#-min-list) `(list)` 202 * [`-min-by`](#-min-by-comparator-list) `(comparator list)` 203 * [`-max`](#-max-list) `(list)` 204 * [`-max-by`](#-max-by-comparator-list) `(comparator list)` 205 206 ### Unfolding 207 208 Operations dual to reductions, building lists from a seed 209 value rather than consuming a list to produce a single value. 210 211 * [`-iterate`](#-iterate-fun-init-n) `(fun init n)` 212 * [`-unfold`](#-unfold-fun-seed) `(fun seed)` 213 214 ### Predicates 215 216 Reductions of one or more lists to a boolean value. 217 218 * [`-some`](#-some-pred-list) `(pred list)` 219 * [`-every`](#-every-pred-list) `(pred list)` 220 * [`-any?`](#-any-pred-list) `(pred list)` 221 * [`-all?`](#-all-pred-list) `(pred list)` 222 * [`-none?`](#-none-pred-list) `(pred list)` 223 * [`-only-some?`](#-only-some-pred-list) `(pred list)` 224 * [`-contains?`](#-contains-list-element) `(list element)` 225 * [`-same-items?`](#-same-items-list-list2) `(list list2)` 226 * [`-is-prefix?`](#-is-prefix-prefix-list) `(prefix list)` 227 * [`-is-suffix?`](#-is-suffix-suffix-list) `(suffix list)` 228 * [`-is-infix?`](#-is-infix-infix-list) `(infix list)` 229 * [`-cons-pair?`](#-cons-pair-obj) `(obj)` 230 231 ### Partitioning 232 233 Functions partitioning the input list into a list of lists. 234 235 * [`-split-at`](#-split-at-n-list) `(n list)` 236 * [`-split-with`](#-split-with-pred-list) `(pred list)` 237 * [`-split-on`](#-split-on-item-list) `(item list)` 238 * [`-split-when`](#-split-when-fn-list) `(fn list)` 239 * [`-separate`](#-separate-pred-list) `(pred list)` 240 * [`-partition`](#-partition-n-list) `(n list)` 241 * [`-partition-all`](#-partition-all-n-list) `(n list)` 242 * [`-partition-in-steps`](#-partition-in-steps-n-step-list) `(n step list)` 243 * [`-partition-all-in-steps`](#-partition-all-in-steps-n-step-list) `(n step list)` 244 * [`-partition-by`](#-partition-by-fn-list) `(fn list)` 245 * [`-partition-by-header`](#-partition-by-header-fn-list) `(fn list)` 246 * [`-partition-after-pred`](#-partition-after-pred-pred-list) `(pred list)` 247 * [`-partition-before-pred`](#-partition-before-pred-pred-list) `(pred list)` 248 * [`-partition-before-item`](#-partition-before-item-item-list) `(item list)` 249 * [`-partition-after-item`](#-partition-after-item-item-list) `(item list)` 250 * [`-group-by`](#-group-by-fn-list) `(fn list)` 251 252 ### Indexing 253 254 Functions retrieving or sorting based on list indices and 255 related predicates. 256 257 * [`-elem-index`](#-elem-index-elem-list) `(elem list)` 258 * [`-elem-indices`](#-elem-indices-elem-list) `(elem list)` 259 * [`-find-index`](#-find-index-pred-list) `(pred list)` 260 * [`-find-last-index`](#-find-last-index-pred-list) `(pred list)` 261 * [`-find-indices`](#-find-indices-pred-list) `(pred list)` 262 * [`-grade-up`](#-grade-up-comparator-list) `(comparator list)` 263 * [`-grade-down`](#-grade-down-comparator-list) `(comparator list)` 264 265 ### Set operations 266 267 Operations pretending lists are sets. 268 269 * [`-union`](#-union-list-list2) `(list list2)` 270 * [`-difference`](#-difference-list-list2) `(list list2)` 271 * [`-intersection`](#-intersection-list-list2) `(list list2)` 272 * [`-powerset`](#-powerset-list) `(list)` 273 * [`-permutations`](#-permutations-list) `(list)` 274 * [`-distinct`](#-distinct-list) `(list)` 275 276 ### Other list operations 277 278 Other list functions not fit to be classified elsewhere. 279 280 * [`-rotate`](#-rotate-n-list) `(n list)` 281 * [`-repeat`](#-repeat-n-x) `(n x)` 282 * [`-cons*`](#-cons-rest-args) `(&rest args)` 283 * [`-snoc`](#-snoc-list-elem-rest-elements) `(list elem &rest elements)` 284 * [`-interpose`](#-interpose-sep-list) `(sep list)` 285 * [`-interleave`](#-interleave-rest-lists) `(&rest lists)` 286 * [`-iota`](#-iota-count-optional-start-step) `(count &optional start step)` 287 * [`-zip-with`](#-zip-with-fn-list1-list2) `(fn list1 list2)` 288 * [`-zip`](#-zip-rest-lists) `(&rest lists)` 289 * [`-zip-lists`](#-zip-lists-rest-lists) `(&rest lists)` 290 * [`-zip-fill`](#-zip-fill-fill-value-rest-lists) `(fill-value &rest lists)` 291 * [`-unzip`](#-unzip-lists) `(lists)` 292 * [`-cycle`](#-cycle-list) `(list)` 293 * [`-pad`](#-pad-fill-value-rest-lists) `(fill-value &rest lists)` 294 * [`-table`](#-table-fn-rest-lists) `(fn &rest lists)` 295 * [`-table-flat`](#-table-flat-fn-rest-lists) `(fn &rest lists)` 296 * [`-first`](#-first-pred-list) `(pred list)` 297 * [`-last`](#-last-pred-list) `(pred list)` 298 * [`-first-item`](#-first-item-list) `(list)` 299 * [`-second-item`](#-second-item-list) `(list)` 300 * [`-third-item`](#-third-item-list) `(list)` 301 * [`-fourth-item`](#-fourth-item-list) `(list)` 302 * [`-fifth-item`](#-fifth-item-list) `(list)` 303 * [`-last-item`](#-last-item-list) `(list)` 304 * [`-butlast`](#-butlast-list) `(list)` 305 * [`-sort`](#-sort-comparator-list) `(comparator list)` 306 * [`-list`](#-list-arg) `(arg)` 307 * [`-fix`](#-fix-fn-list) `(fn list)` 308 309 ### Tree operations 310 311 Functions pretending lists are trees. 312 313 * [`-tree-seq`](#-tree-seq-branch-children-tree) `(branch children tree)` 314 * [`-tree-map`](#-tree-map-fn-tree) `(fn tree)` 315 * [`-tree-map-nodes`](#-tree-map-nodes-pred-fun-tree) `(pred fun tree)` 316 * [`-tree-reduce`](#-tree-reduce-fn-tree) `(fn tree)` 317 * [`-tree-reduce-from`](#-tree-reduce-from-fn-init-value-tree) `(fn init-value tree)` 318 * [`-tree-mapreduce`](#-tree-mapreduce-fn-folder-tree) `(fn folder tree)` 319 * [`-tree-mapreduce-from`](#-tree-mapreduce-from-fn-folder-init-value-tree) `(fn folder init-value tree)` 320 * [`-clone`](#-clone-list) `(list)` 321 322 ### Threading macros 323 324 Macros that conditionally combine sequential forms for brevity 325 or readability. 326 327 * [`->`](#--x-optional-form-rest-more) `(x &optional form &rest more)` 328 * [`->>`](#--x-optional-form-rest-more) `(x &optional form &rest more)` 329 * [`-->`](#---x-rest-forms) `(x &rest forms)` 330 * [`-as->`](#-as--value-variable-rest-forms) `(value variable &rest forms)` 331 * [`-some->`](#-some--x-optional-form-rest-more) `(x &optional form &rest more)` 332 * [`-some->>`](#-some--x-optional-form-rest-more) `(x &optional form &rest more)` 333 * [`-some-->`](#-some---expr-rest-forms) `(expr &rest forms)` 334 * [`-doto`](#-doto-init-rest-forms) `(init &rest forms)` 335 336 ### Binding 337 338 Macros that combine `let` and `let*` with destructuring and flow control. 339 340 * [`-when-let`](#-when-let-var-val-rest-body) `((var val) &rest body)` 341 * [`-when-let*`](#-when-let-vars-vals-rest-body) `(vars-vals &rest body)` 342 * [`-if-let`](#-if-let-var-val-then-rest-else) `((var val) then &rest else)` 343 * [`-if-let*`](#-if-let-vars-vals-then-rest-else) `(vars-vals then &rest else)` 344 * [`-let`](#-let-varlist-rest-body) `(varlist &rest body)` 345 * [`-let*`](#-let-varlist-rest-body) `(varlist &rest body)` 346 * [`-lambda`](#-lambda-match-form-rest-body) `(match-form &rest body)` 347 * [`-setq`](#-setq-match-form-val) `([match-form val] ...)` 348 349 ### Side effects 350 351 Functions iterating over lists for side effect only. 352 353 * [`-each`](#-each-list-fn) `(list fn)` 354 * [`-each-while`](#-each-while-list-pred-fn) `(list pred fn)` 355 * [`-each-indexed`](#-each-indexed-list-fn) `(list fn)` 356 * [`-each-r`](#-each-r-list-fn) `(list fn)` 357 * [`-each-r-while`](#-each-r-while-list-pred-fn) `(list pred fn)` 358 * [`-dotimes`](#-dotimes-num-fn) `(num fn)` 359 360 ### Destructive operations 361 362 Macros that modify variables holding lists. 363 364 * [`!cons`](#cons-car-cdr) `(car cdr)` 365 * [`!cdr`](#cdr-list) `(list)` 366 367 ### Function combinators 368 369 Functions that manipulate and compose other functions. 370 371 * [`-partial`](#-partial-fun-rest-args) `(fun &rest args)` 372 * [`-rpartial`](#-rpartial-fn-rest-args) `(fn &rest args)` 373 * [`-juxt`](#-juxt-rest-fns) `(&rest fns)` 374 * [`-compose`](#-compose-rest-fns) `(&rest fns)` 375 * [`-applify`](#-applify-fn) `(fn)` 376 * [`-on`](#-on-op-trans) `(op trans)` 377 * [`-flip`](#-flip-fn) `(fn)` 378 * [`-rotate-args`](#-rotate-args-n-fn) `(n fn)` 379 * [`-const`](#-const-c) `(c)` 380 * [`-cut`](#-cut-rest-params) `(&rest params)` 381 * [`-not`](#-not-pred) `(pred)` 382 * [`-orfn`](#-orfn-rest-preds) `(&rest preds)` 383 * [`-andfn`](#-andfn-rest-preds) `(&rest preds)` 384 * [`-iteratefn`](#-iteratefn-fn-n) `(fn n)` 385 * [`-fixfn`](#-fixfn-fn-optional-equal-test-halt-test) `(fn &optional equal-test halt-test)` 386 * [`-prodfn`](#-prodfn-rest-fns) `(&rest fns)` 387 388 ## Maps 389 390 Functions in this category take a transforming function, which 391 is then applied sequentially to each or selected elements of the 392 input list. The results are collected in order and returned as a 393 new list. 394 395 #### -map `(fn list)` 396 397 Apply `fn` to each item in `list` and return the list of results. 398 399 This function's anaphoric counterpart is `--map`. 400 401 ```el 402 (-map (lambda (num) (* num num)) '(1 2 3 4)) ;; => (1 4 9 16) 403 (-map #'1+ '(1 2 3 4)) ;; => (2 3 4 5) 404 (--map (* it it) '(1 2 3 4)) ;; => (1 4 9 16) 405 ``` 406 407 #### -map-when `(pred rep list)` 408 409 Return a new list where the elements in `list` that do not match the `pred` function 410 are unchanged, and where the elements in `list` that do match the `pred` function are mapped 411 through the `rep` function. 412 413 Alias: `-replace-where` 414 415 See also: [`-update-at`](#-update-at-n-func-list) 416 417 ```el 418 (-map-when 'even? 'square '(1 2 3 4)) ;; => (1 4 3 16) 419 (--map-when (> it 2) (* it it) '(1 2 3 4)) ;; => (1 2 9 16) 420 (--map-when (= it 2) 17 '(1 2 3 4)) ;; => (1 17 3 4) 421 ``` 422 423 #### -map-first `(pred rep list)` 424 425 Replace first item in `list` satisfying `pred` with result of `rep` called on this item. 426 427 See also: [`-map-when`](#-map-when-pred-rep-list), [`-replace-first`](#-replace-first-old-new-list) 428 429 ```el 430 (-map-first 'even? 'square '(1 2 3 4)) ;; => (1 4 3 4) 431 (--map-first (> it 2) (* it it) '(1 2 3 4)) ;; => (1 2 9 4) 432 (--map-first (= it 2) 17 '(1 2 3 2)) ;; => (1 17 3 2) 433 ``` 434 435 #### -map-last `(pred rep list)` 436 437 Replace last item in `list` satisfying `pred` with result of `rep` called on this item. 438 439 See also: [`-map-when`](#-map-when-pred-rep-list), [`-replace-last`](#-replace-last-old-new-list) 440 441 ```el 442 (-map-last 'even? 'square '(1 2 3 4)) ;; => (1 2 3 16) 443 (--map-last (> it 2) (* it it) '(1 2 3 4)) ;; => (1 2 3 16) 444 (--map-last (= it 2) 17 '(1 2 3 2)) ;; => (1 2 3 17) 445 ``` 446 447 #### -map-indexed `(fn list)` 448 449 Apply `fn` to each index and item in `list` and return the list of results. 450 This is like [`-map`](#-map-fn-list), but `fn` takes two arguments: the index of the 451 current element within `list`, and the element itself. 452 453 This function's anaphoric counterpart is `--map-indexed`. 454 455 For a side-effecting variant, see also [`-each-indexed`](#-each-indexed-list-fn). 456 457 ```el 458 (-map-indexed (lambda (index item) (- item index)) '(1 2 3 4)) ;; => (1 1 1 1) 459 (--map-indexed (- it it-index) '(1 2 3 4)) ;; => (1 1 1 1) 460 (-map-indexed #'* '(1 2 3 4)) ;; => (0 2 6 12) 461 ``` 462 463 #### -annotate `(fn list)` 464 465 Return a list of cons cells where each cell is `fn` applied to each 466 element of `list` paired with the unmodified element of `list`. 467 468 ```el 469 (-annotate '1+ '(1 2 3)) ;; => ((2 . 1) (3 . 2) (4 . 3)) 470 (-annotate 'length '(("h" "e" "l" "l" "o") ("hello" "world"))) ;; => ((5 "h" "e" "l" "l" "o") (2 "hello" "world")) 471 (--annotate (< 1 it) '(0 1 2 3)) ;; => ((nil . 0) (nil . 1) (t . 2) (t . 3)) 472 ``` 473 474 #### -splice `(pred fun list)` 475 476 Splice lists generated by `fun` in place of elements matching `pred` in `list`. 477 478 `fun` takes the element matching `pred` as input. 479 480 This function can be used as replacement for `,@` in case you 481 need to splice several lists at marked positions (for example 482 with keywords). 483 484 See also: [`-splice-list`](#-splice-list-pred-new-list-list), [`-insert-at`](#-insert-at-n-x-list) 485 486 ```el 487 (-splice 'even? (lambda (x) (list x x)) '(1 2 3 4)) ;; => (1 2 2 3 4 4) 488 (--splice 't (list it it) '(1 2 3 4)) ;; => (1 1 2 2 3 3 4 4) 489 (--splice (equal it :magic) '((list of) (magical) (code)) '((foo) (bar) :magic (baz))) ;; => ((foo) (bar) (list of) (magical) (code) (baz)) 490 ``` 491 492 #### -splice-list `(pred new-list list)` 493 494 Splice `new-list` in place of elements matching `pred` in `list`. 495 496 See also: [`-splice`](#-splice-pred-fun-list), [`-insert-at`](#-insert-at-n-x-list) 497 498 ```el 499 (-splice-list 'keywordp '(a b c) '(1 :foo 2)) ;; => (1 a b c 2) 500 (-splice-list 'keywordp nil '(1 :foo 2)) ;; => (1 2) 501 (--splice-list (keywordp it) '(a b c) '(1 :foo 2)) ;; => (1 a b c 2) 502 ``` 503 504 #### -mapcat `(fn list)` 505 506 Return the concatenation of the result of mapping `fn` over `list`. 507 Thus function `fn` should return a list. 508 509 ```el 510 (-mapcat 'list '(1 2 3)) ;; => (1 2 3) 511 (-mapcat (lambda (item) (list 0 item)) '(1 2 3)) ;; => (0 1 0 2 0 3) 512 (--mapcat (list 0 it) '(1 2 3)) ;; => (0 1 0 2 0 3) 513 ``` 514 515 #### -copy `(list)` 516 517 Create a shallow copy of `list`. 518 519 ```el 520 (-copy '(1 2 3)) ;; => (1 2 3) 521 (let ((a '(1 2 3))) (eq a (-copy a))) ;; => nil 522 ``` 523 524 ## Sublist selection 525 526 Functions returning a sublist of the original list. 527 528 #### -filter `(pred list)` 529 530 Return a new list of the items in `list` for which `pred` returns non-nil. 531 532 Alias: `-select`. 533 534 This function's anaphoric counterpart is `--filter`. 535 536 For similar operations, see also [`-keep`](#-keep-fn-list) and [`-remove`](#-remove-pred-list). 537 538 ```el 539 (-filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) ;; => (2 4) 540 (-filter #'natnump '(-2 -1 0 1 2)) ;; => (0 1 2) 541 (--filter (= 0 (% it 2)) '(1 2 3 4)) ;; => (2 4) 542 ``` 543 544 #### -remove `(pred list)` 545 546 Return a new list of the items in `list` for which `pred` returns nil. 547 548 Alias: `-reject`. 549 550 This function's anaphoric counterpart is `--remove`. 551 552 For similar operations, see also [`-keep`](#-keep-fn-list) and [`-filter`](#-filter-pred-list). 553 554 ```el 555 (-remove (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) ;; => (1 3) 556 (-remove #'natnump '(-2 -1 0 1 2)) ;; => (-2 -1) 557 (--remove (= 0 (% it 2)) '(1 2 3 4)) ;; => (1 3) 558 ``` 559 560 #### -remove-first `(pred list)` 561 562 Remove the first item from `list` for which `pred` returns non-nil. 563 This is a non-destructive operation, but only the front of `list` 564 leading up to the removed item is a copy; the rest is `list`'s 565 original tail. If no item is removed, then the result is a 566 complete copy. 567 568 Alias: `-reject-first`. 569 570 This function's anaphoric counterpart is `--remove-first`. 571 572 See also [`-map-first`](#-map-first-pred-rep-list), [`-remove-item`](#-remove-item-item-list), and [`-remove-last`](#-remove-last-pred-list). 573 574 ```el 575 (-remove-first #'natnump '(-2 -1 0 1 2)) ;; => (-2 -1 1 2) 576 (-remove-first #'stringp '(1 2 "first" "second")) ;; => (1 2 "second") 577 (--remove-first (> it 3) '(1 2 3 4 5 6)) ;; => (1 2 3 5 6) 578 ``` 579 580 #### -remove-last `(pred list)` 581 582 Remove the last item from `list` for which `pred` returns non-nil. 583 The result is a copy of `list` regardless of whether an element is 584 removed. 585 586 Alias: `-reject-last`. 587 588 This function's anaphoric counterpart is `--remove-last`. 589 590 See also [`-map-last`](#-map-last-pred-rep-list), [`-remove-item`](#-remove-item-item-list), and [`-remove-first`](#-remove-first-pred-list). 591 592 ```el 593 (-remove-last #'natnump '(1 3 5 4 7 8 10 -11)) ;; => (1 3 5 4 7 8 -11) 594 (-remove-last #'stringp '(1 2 "last" "second")) ;; => (1 2 "last") 595 (--remove-last (> it 3) '(1 2 3 4 5 6 7 8 9 10)) ;; => (1 2 3 4 5 6 7 8 9) 596 ``` 597 598 #### -remove-item `(item list)` 599 600 Return a copy of `list` with all occurrences of `item` removed. 601 The comparison is done with `equal`. 602 603 ```el 604 (-remove-item 3 '(1 2 3 2 3 4 5 3)) ;; => (1 2 2 4 5) 605 (-remove-item 'foo '(foo bar baz foo)) ;; => (bar baz) 606 (-remove-item "bob" '("alice" "bob" "eve" "bob")) ;; => ("alice" "eve") 607 ``` 608 609 #### -non-nil `(list)` 610 611 Return a copy of `list` with all nil items removed. 612 613 ```el 614 (-non-nil '(nil 1 nil 2 nil nil 3 4 nil 5 nil)) ;; => (1 2 3 4 5) 615 (-non-nil '((nil))) ;; => ((nil)) 616 (-non-nil ()) ;; => () 617 ``` 618 619 #### -slice `(list from &optional to step)` 620 621 Return copy of `list`, starting from index `from` to index `to`. 622 623 `from` or `to` may be negative. These values are then interpreted 624 modulo the length of the list. 625 626 If `step` is a number, only each `step`th item in the resulting 627 section is returned. Defaults to 1. 628 629 ```el 630 (-slice '(1 2 3 4 5) 1) ;; => (2 3 4 5) 631 (-slice '(1 2 3 4 5) 0 3) ;; => (1 2 3) 632 (-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2) ;; => (2 4 6 8) 633 ``` 634 635 #### -take `(n list)` 636 637 Return a copy of the first `n` items in `list`. 638 Return a copy of `list` if it contains `n` items or fewer. 639 Return nil if `n` is zero or less. 640 641 See also: [`-take-last`](#-take-last-n-list). 642 643 ```el 644 (-take 3 '(1 2 3 4 5)) ;; => (1 2 3) 645 (-take 17 '(1 2 3 4 5)) ;; => (1 2 3 4 5) 646 (-take 0 '(1 2 3 4 5)) ;; => () 647 ``` 648 649 #### -take-last `(n list)` 650 651 Return a copy of the last `n` items of `list` in order. 652 Return a copy of `list` if it contains `n` items or fewer. 653 Return nil if `n` is zero or less. 654 655 See also: [`-take`](#-take-n-list). 656 657 ```el 658 (-take-last 3 '(1 2 3 4 5)) ;; => (3 4 5) 659 (-take-last 17 '(1 2 3 4 5)) ;; => (1 2 3 4 5) 660 (-take-last 1 '(1 2 3 4 5)) ;; => (5) 661 ``` 662 663 #### -drop `(n list)` 664 665 Return the tail (not a copy) of `list` without the first `n` items. 666 Return nil if `list` contains `n` items or fewer. 667 Return `list` if `n` is zero or less. 668 669 For another variant, see also [`-drop-last`](#-drop-last-n-list). 670 671 ```el 672 (-drop 3 '(1 2 3 4 5)) ;; => (4 5) 673 (-drop 17 '(1 2 3 4 5)) ;; => () 674 (-drop 0 '(1 2 3 4 5)) ;; => (1 2 3 4 5) 675 ``` 676 677 #### -drop-last `(n list)` 678 679 Return a copy of `list` without its last `n` items. 680 Return a copy of `list` if `n` is zero or less. 681 Return nil if `list` contains `n` items or fewer. 682 683 See also: [`-drop`](#-drop-n-list). 684 685 ```el 686 (-drop-last 3 '(1 2 3 4 5)) ;; => (1 2) 687 (-drop-last 17 '(1 2 3 4 5)) ;; => () 688 (-drop-last 0 '(1 2 3 4 5)) ;; => (1 2 3 4 5) 689 ``` 690 691 #### -take-while `(pred list)` 692 693 Take successive items from `list` for which `pred` returns non-nil. 694 `pred` is a function of one argument. Return a new list of the 695 successive elements from the start of `list` for which `pred` returns 696 non-nil. 697 698 This function's anaphoric counterpart is `--take-while`. 699 700 For another variant, see also [`-drop-while`](#-drop-while-pred-list). 701 702 ```el 703 (-take-while #'even? '(1 2 3 4)) ;; => () 704 (-take-while #'even? '(2 4 5 6)) ;; => (2 4) 705 (--take-while (< it 4) '(1 2 3 4 3 2 1)) ;; => (1 2 3) 706 ``` 707 708 #### -drop-while `(pred list)` 709 710 Drop successive items from `list` for which `pred` returns non-nil. 711 `pred` is a function of one argument. Return the tail (not a copy) 712 of `list` starting from its first element for which `pred` returns 713 nil. 714 715 This function's anaphoric counterpart is `--drop-while`. 716 717 For another variant, see also [`-take-while`](#-take-while-pred-list). 718 719 ```el 720 (-drop-while #'even? '(1 2 3 4)) ;; => (1 2 3 4) 721 (-drop-while #'even? '(2 4 5 6)) ;; => (5 6) 722 (--drop-while (< it 4) '(1 2 3 4 3 2 1)) ;; => (4 3 2 1) 723 ``` 724 725 #### -select-by-indices `(indices list)` 726 727 Return a list whose elements are elements from `list` selected 728 as `(nth i list)` for all i from `indices`. 729 730 ```el 731 (-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p" "t" "o" "r")) ;; => ("c" "o" "l" "o" "r") 732 (-select-by-indices '(2 1 0) '("a" "b" "c")) ;; => ("c" "b" "a") 733 (-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) ;; => ("f" "a" "r" "f" "a" "l" "l" "a") 734 ``` 735 736 #### -select-columns `(columns table)` 737 738 Select `columns` from `table`. 739 740 `table` is a list of lists where each element represents one row. 741 It is assumed each row has the same length. 742 743 Each row is transformed such that only the specified `columns` are 744 selected. 745 746 See also: [`-select-column`](#-select-column-column-table), [`-select-by-indices`](#-select-by-indices-indices-list) 747 748 ```el 749 (-select-columns '(0 2) '((1 2 3) (a b c) (:a :b :c))) ;; => ((1 3) (a c) (:a :c)) 750 (-select-columns '(1) '((1 2 3) (a b c) (:a :b :c))) ;; => ((2) (b) (:b)) 751 (-select-columns nil '((1 2 3) (a b c) (:a :b :c))) ;; => (nil nil nil) 752 ``` 753 754 #### -select-column `(column table)` 755 756 Select `column` from `table`. 757 758 `table` is a list of lists where each element represents one row. 759 It is assumed each row has the same length. 760 761 The single selected column is returned as a list. 762 763 See also: [`-select-columns`](#-select-columns-columns-table), [`-select-by-indices`](#-select-by-indices-indices-list) 764 765 ```el 766 (-select-column 1 '((1 2 3) (a b c) (:a :b :c))) ;; => (2 b :b) 767 ``` 768 769 ## List to list 770 771 Functions returning a modified copy of the input list. 772 773 #### -keep `(fn list)` 774 775 Return a new list of the non-nil results of applying `fn` to each item in `list`. 776 Like [`-filter`](#-filter-pred-list), but returns the non-nil results of `fn` instead of 777 the corresponding elements of `list`. 778 779 Its anaphoric counterpart is `--keep`. 780 781 ```el 782 (-keep #'cdr '((1 2 3) (4 5) (6))) ;; => ((2 3) (5)) 783 (-keep (lambda (n) (and (> n 3) (* 10 n))) '(1 2 3 4 5 6)) ;; => (40 50 60) 784 (--keep (and (> it 3) (* 10 it)) '(1 2 3 4 5 6)) ;; => (40 50 60) 785 ``` 786 787 #### -concat `(&rest lists)` 788 789 Return a new list with the concatenation of the elements in the supplied `lists`. 790 791 ```el 792 (-concat '(1)) ;; => (1) 793 (-concat '(1) '(2)) ;; => (1 2) 794 (-concat '(1) '(2 3) '(4)) ;; => (1 2 3 4) 795 ``` 796 797 #### -flatten `(l)` 798 799 Take a nested list `l` and return its contents as a single, flat list. 800 801 Note that because `nil` represents a list of zero elements (an 802 empty list), any mention of nil in `l` will disappear after 803 flattening. If you need to preserve nils, consider [`-flatten-n`](#-flatten-n-num-list) 804 or map them to some unique symbol and then map them back. 805 806 Conses of two atoms are considered "terminals", that is, they 807 aren't flattened further. 808 809 See also: [`-flatten-n`](#-flatten-n-num-list) 810 811 ```el 812 (-flatten '((1))) ;; => (1) 813 (-flatten '((1 (2 3) (((4 (5))))))) ;; => (1 2 3 4 5) 814 (-flatten '(1 2 (3 . 4))) ;; => (1 2 (3 . 4)) 815 ``` 816 817 #### -flatten-n `(num list)` 818 819 Flatten `num` levels of a nested `list`. 820 821 See also: [`-flatten`](#-flatten-l) 822 823 ```el 824 (-flatten-n 1 '((1 2) ((3 4) ((5 6))))) ;; => (1 2 (3 4) ((5 6))) 825 (-flatten-n 2 '((1 2) ((3 4) ((5 6))))) ;; => (1 2 3 4 (5 6)) 826 (-flatten-n 3 '((1 2) ((3 4) ((5 6))))) ;; => (1 2 3 4 5 6) 827 ``` 828 829 #### -replace `(old new list)` 830 831 Replace all `old` items in `list` with `new`. 832 833 Elements are compared using `equal`. 834 835 See also: [`-replace-at`](#-replace-at-n-x-list) 836 837 ```el 838 (-replace 1 "1" '(1 2 3 4 3 2 1)) ;; => ("1" 2 3 4 3 2 "1") 839 (-replace "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo")) ;; => ("a" "nice" "bar" "sentence" "about" "bar") 840 (-replace 1 2 nil) ;; => nil 841 ``` 842 843 #### -replace-first `(old new list)` 844 845 Replace the first occurrence of `old` with `new` in `list`. 846 847 Elements are compared using `equal`. 848 849 See also: [`-map-first`](#-map-first-pred-rep-list) 850 851 ```el 852 (-replace-first 1 "1" '(1 2 3 4 3 2 1)) ;; => ("1" 2 3 4 3 2 1) 853 (-replace-first "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo")) ;; => ("a" "nice" "bar" "sentence" "about" "foo") 854 (-replace-first 1 2 nil) ;; => nil 855 ``` 856 857 #### -replace-last `(old new list)` 858 859 Replace the last occurrence of `old` with `new` in `list`. 860 861 Elements are compared using `equal`. 862 863 See also: [`-map-last`](#-map-last-pred-rep-list) 864 865 ```el 866 (-replace-last 1 "1" '(1 2 3 4 3 2 1)) ;; => (1 2 3 4 3 2 "1") 867 (-replace-last "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo")) ;; => ("a" "nice" "foo" "sentence" "about" "bar") 868 (-replace-last 1 2 nil) ;; => nil 869 ``` 870 871 #### -insert-at `(n x list)` 872 873 Return a list with `x` inserted into `list` at position `n`. 874 875 See also: [`-splice`](#-splice-pred-fun-list), [`-splice-list`](#-splice-list-pred-new-list-list) 876 877 ```el 878 (-insert-at 1 'x '(a b c)) ;; => (a x b c) 879 (-insert-at 12 'x '(a b c)) ;; => (a b c x) 880 ``` 881 882 #### -replace-at `(n x list)` 883 884 Return a list with element at `n`th position in `list` replaced with `x`. 885 886 See also: [`-replace`](#-replace-old-new-list) 887 888 ```el 889 (-replace-at 0 9 '(0 1 2 3 4 5)) ;; => (9 1 2 3 4 5) 890 (-replace-at 1 9 '(0 1 2 3 4 5)) ;; => (0 9 2 3 4 5) 891 (-replace-at 4 9 '(0 1 2 3 4 5)) ;; => (0 1 2 3 9 5) 892 ``` 893 894 #### -update-at `(n func list)` 895 896 Return a list with element at `n`th position in `list` replaced with `(func (nth n list))`. 897 898 See also: [`-map-when`](#-map-when-pred-rep-list) 899 900 ```el 901 (-update-at 0 (lambda (x) (+ x 9)) '(0 1 2 3 4 5)) ;; => (9 1 2 3 4 5) 902 (-update-at 1 (lambda (x) (+ x 8)) '(0 1 2 3 4 5)) ;; => (0 9 2 3 4 5) 903 (--update-at 2 (length it) '("foo" "bar" "baz" "quux")) ;; => ("foo" "bar" 3 "quux") 904 ``` 905 906 #### -remove-at `(n list)` 907 908 Return a list with element at `n`th position in `list` removed. 909 910 See also: [`-remove-at-indices`](#-remove-at-indices-indices-list), [`-remove`](#-remove-pred-list) 911 912 ```el 913 (-remove-at 0 '("0" "1" "2" "3" "4" "5")) ;; => ("1" "2" "3" "4" "5") 914 (-remove-at 1 '("0" "1" "2" "3" "4" "5")) ;; => ("0" "2" "3" "4" "5") 915 (-remove-at 2 '("0" "1" "2" "3" "4" "5")) ;; => ("0" "1" "3" "4" "5") 916 ``` 917 918 #### -remove-at-indices `(indices list)` 919 920 Return a list whose elements are elements from `list` without 921 elements selected as `(nth i list)` for all i 922 from `indices`. 923 924 See also: [`-remove-at`](#-remove-at-n-list), [`-remove`](#-remove-pred-list) 925 926 ```el 927 (-remove-at-indices '(0) '("0" "1" "2" "3" "4" "5")) ;; => ("1" "2" "3" "4" "5") 928 (-remove-at-indices '(0 2 4) '("0" "1" "2" "3" "4" "5")) ;; => ("1" "3" "5") 929 (-remove-at-indices '(0 5) '("0" "1" "2" "3" "4" "5")) ;; => ("1" "2" "3" "4") 930 ``` 931 932 ## Reductions 933 934 Functions reducing lists to a single value (which may also be a list). 935 936 #### -reduce-from `(fn init list)` 937 938 Reduce the function `fn` across `list`, starting with `init`. 939 Return the result of applying `fn` to `init` and the first element of 940 `list`, then applying `fn` to that result and the second element, 941 etc. If `list` is empty, return `init` without calling `fn`. 942 943 This function's anaphoric counterpart is `--reduce-from`. 944 945 For other folds, see also [`-reduce`](#-reduce-fn-list) and [`-reduce-r`](#-reduce-r-fn-list). 946 947 ```el 948 (-reduce-from #'- 10 '(1 2 3)) ;; => 4 949 (-reduce-from #'list 10 '(1 2 3)) ;; => (((10 1) 2) 3) 950 (--reduce-from (concat acc " " it) "START" '("a" "b" "c")) ;; => "START a b c" 951 ``` 952 953 #### -reduce-r-from `(fn init list)` 954 955 Reduce the function `fn` across `list` in reverse, starting with `init`. 956 Return the result of applying `fn` to the last element of `list` and 957 `init`, then applying `fn` to the second-to-last element and the 958 previous result of `fn`, etc. That is, the first argument of `fn` is 959 the current element, and its second argument the accumulated 960 value. If `list` is empty, return `init` without calling `fn`. 961 962 This function is like [`-reduce-from`](#-reduce-from-fn-init-list) but the operation associates 963 from the right rather than left. In other words, it starts from 964 the end of `list` and flips the arguments to `fn`. Conceptually, it 965 is like replacing the conses in `list` with applications of `fn`, and 966 its last link with `init`, and evaluating the resulting expression. 967 968 This function's anaphoric counterpart is `--reduce-r-from`. 969 970 For other folds, see also [`-reduce-r`](#-reduce-r-fn-list) and [`-reduce`](#-reduce-fn-list). 971 972 ```el 973 (-reduce-r-from #'- 10 '(1 2 3)) ;; => -8 974 (-reduce-r-from #'list 10 '(1 2 3)) ;; => (1 (2 (3 10))) 975 (--reduce-r-from (concat it " " acc) "END" '("a" "b" "c")) ;; => "a b c END" 976 ``` 977 978 #### -reduce `(fn list)` 979 980 Reduce the function `fn` across `list`. 981 Return the result of applying `fn` to the first two elements of 982 `list`, then applying `fn` to that result and the third element, etc. 983 If `list` contains a single element, return it without calling `fn`. 984 If `list` is empty, return the result of calling `fn` with no 985 arguments. 986 987 This function's anaphoric counterpart is `--reduce`. 988 989 For other folds, see also [`-reduce-from`](#-reduce-from-fn-init-list) and [`-reduce-r`](#-reduce-r-fn-list). 990 991 ```el 992 (-reduce #'- '(1 2 3 4)) ;; => -8 993 (-reduce #'list '(1 2 3 4)) ;; => (((1 2) 3) 4) 994 (--reduce (format "%s-%d" acc it) '(1 2 3)) ;; => "1-2-3" 995 ``` 996 997 #### -reduce-r `(fn list)` 998 999 Reduce the function `fn` across `list` in reverse. 1000 Return the result of applying `fn` to the last two elements of 1001 `list`, then applying `fn` to the third-to-last element and the 1002 previous result of `fn`, etc. That is, the first argument of `fn` is 1003 the current element, and its second argument the accumulated 1004 value. If `list` contains a single element, return it without 1005 calling `fn`. If `list` is empty, return the result of calling `fn` 1006 with no arguments. 1007 1008 This function is like [`-reduce`](#-reduce-fn-list) but the operation associates from 1009 the right rather than left. In other words, it starts from the 1010 end of `list` and flips the arguments to `fn`. Conceptually, it is 1011 like replacing the conses in `list` with applications of `fn`, 1012 ignoring its last link, and evaluating the resulting expression. 1013 1014 This function's anaphoric counterpart is `--reduce-r`. 1015 1016 For other folds, see also [`-reduce-r-from`](#-reduce-r-from-fn-init-list) and [`-reduce`](#-reduce-fn-list). 1017 1018 ```el 1019 (-reduce-r #'- '(1 2 3 4)) ;; => -2 1020 (-reduce-r #'list '(1 2 3 4)) ;; => (1 (2 (3 4))) 1021 (--reduce-r (format "%s-%d" acc it) '(1 2 3)) ;; => "3-2-1" 1022 ``` 1023 1024 #### -reductions-from `(fn init list)` 1025 1026 Return a list of `fn`'s intermediate reductions across `list`. 1027 That is, a list of the intermediate values of the accumulator 1028 when [`-reduce-from`](#-reduce-from-fn-init-list) (which see) is called with the same 1029 arguments. 1030 1031 This function's anaphoric counterpart is `--reductions-from`. 1032 1033 For other folds, see also [`-reductions`](#-reductions-fn-list) and [`-reductions-r`](#-reductions-r-fn-list). 1034 1035 ```el 1036 (-reductions-from #'max 0 '(2 1 4 3)) ;; => (0 2 2 4 4) 1037 (-reductions-from #'* 1 '(1 2 3 4)) ;; => (1 1 2 6 24) 1038 (--reductions-from (format "(FN %s %d)" acc it) "INIT" '(1 2 3)) ;; => ("INIT" "(FN INIT 1)" "(FN (FN INIT 1) 2)" "(FN (FN (FN INIT 1) 2) 3)") 1039 ``` 1040 1041 #### -reductions-r-from `(fn init list)` 1042 1043 Return a list of `fn`'s intermediate reductions across reversed `list`. 1044 That is, a list of the intermediate values of the accumulator 1045 when [`-reduce-r-from`](#-reduce-r-from-fn-init-list) (which see) is called with the same 1046 arguments. 1047 1048 This function's anaphoric counterpart is `--reductions-r-from`. 1049 1050 For other folds, see also [`-reductions`](#-reductions-fn-list) and [`-reductions-r`](#-reductions-r-fn-list). 1051 1052 ```el 1053 (-reductions-r-from #'max 0 '(2 1 4 3)) ;; => (4 4 4 3 0) 1054 (-reductions-r-from #'* 1 '(1 2 3 4)) ;; => (24 24 12 4 1) 1055 (--reductions-r-from (format "(FN %d %s)" it acc) "INIT" '(1 2 3)) ;; => ("(FN 1 (FN 2 (FN 3 INIT)))" "(FN 2 (FN 3 INIT))" "(FN 3 INIT)" "INIT") 1056 ``` 1057 1058 #### -reductions `(fn list)` 1059 1060 Return a list of `fn`'s intermediate reductions across `list`. 1061 That is, a list of the intermediate values of the accumulator 1062 when [`-reduce`](#-reduce-fn-list) (which see) is called with the same arguments. 1063 1064 This function's anaphoric counterpart is `--reductions`. 1065 1066 For other folds, see also [`-reductions`](#-reductions-fn-list) and [`-reductions-r`](#-reductions-r-fn-list). 1067 1068 ```el 1069 (-reductions #'+ '(1 2 3 4)) ;; => (1 3 6 10) 1070 (-reductions #'* '(1 2 3 4)) ;; => (1 2 6 24) 1071 (--reductions (format "(FN %s %d)" acc it) '(1 2 3)) ;; => (1 "(FN 1 2)" "(FN (FN 1 2) 3)") 1072 ``` 1073 1074 #### -reductions-r `(fn list)` 1075 1076 Return a list of `fn`'s intermediate reductions across reversed `list`. 1077 That is, a list of the intermediate values of the accumulator 1078 when [`-reduce-r`](#-reduce-r-fn-list) (which see) is called with the same arguments. 1079 1080 This function's anaphoric counterpart is `--reductions-r`. 1081 1082 For other folds, see also [`-reductions-r-from`](#-reductions-r-from-fn-init-list) and 1083 [`-reductions`](#-reductions-fn-list). 1084 1085 ```el 1086 (-reductions-r #'+ '(1 2 3 4)) ;; => (10 9 7 4) 1087 (-reductions-r #'* '(1 2 3 4)) ;; => (24 24 12 4) 1088 (--reductions-r (format "(FN %d %s)" it acc) '(1 2 3)) ;; => ("(FN 1 (FN 2 3))" "(FN 2 3)" 3) 1089 ``` 1090 1091 #### -count `(pred list)` 1092 1093 Counts the number of items in `list` where (`pred` item) is non-nil. 1094 1095 ```el 1096 (-count 'even? '(1 2 3 4 5)) ;; => 2 1097 (--count (< it 4) '(1 2 3 4)) ;; => 3 1098 ``` 1099 1100 #### -sum `(list)` 1101 1102 Return the sum of `list`. 1103 1104 ```el 1105 (-sum ()) ;; => 0 1106 (-sum '(1)) ;; => 1 1107 (-sum '(1 2 3 4)) ;; => 10 1108 ``` 1109 1110 #### -running-sum `(list)` 1111 1112 Return a list with running sums of items in `list`. 1113 `list` must be non-empty. 1114 1115 ```el 1116 (-running-sum '(1 2 3 4)) ;; => (1 3 6 10) 1117 (-running-sum '(1)) ;; => (1) 1118 (-running-sum ()) ;; Wrong type argument: consp, nil 1119 ``` 1120 1121 #### -product `(list)` 1122 1123 Return the product of `list`. 1124 1125 ```el 1126 (-product ()) ;; => 1 1127 (-product '(1)) ;; => 1 1128 (-product '(1 2 3 4)) ;; => 24 1129 ``` 1130 1131 #### -running-product `(list)` 1132 1133 Return a list with running products of items in `list`. 1134 `list` must be non-empty. 1135 1136 ```el 1137 (-running-product '(1 2 3 4)) ;; => (1 2 6 24) 1138 (-running-product '(1)) ;; => (1) 1139 (-running-product ()) ;; Wrong type argument: consp, nil 1140 ``` 1141 1142 #### -inits `(list)` 1143 1144 Return all prefixes of `list`. 1145 1146 ```el 1147 (-inits '(1 2 3 4)) ;; => (nil (1) (1 2) (1 2 3) (1 2 3 4)) 1148 (-inits nil) ;; => (nil) 1149 (-inits '(1)) ;; => (nil (1)) 1150 ``` 1151 1152 #### -tails `(list)` 1153 1154 Return all suffixes of `list` 1155 1156 ```el 1157 (-tails '(1 2 3 4)) ;; => ((1 2 3 4) (2 3 4) (3 4) (4) nil) 1158 (-tails nil) ;; => (nil) 1159 (-tails '(1)) ;; => ((1) nil) 1160 ``` 1161 1162 #### -common-prefix `(&rest lists)` 1163 1164 Return the longest common prefix of `lists`. 1165 1166 ```el 1167 (-common-prefix '(1)) ;; => (1) 1168 (-common-prefix '(1 2) '(3 4) '(1 2)) ;; => () 1169 (-common-prefix '(1 2) '(1 2 3) '(1 2 3 4)) ;; => (1 2) 1170 ``` 1171 1172 #### -common-suffix `(&rest lists)` 1173 1174 Return the longest common suffix of `lists`. 1175 1176 ```el 1177 (-common-suffix '(1)) ;; => (1) 1178 (-common-suffix '(1 2) '(3 4) '(1 2)) ;; => () 1179 (-common-suffix '(1 2 3 4) '(2 3 4) '(3 4)) ;; => (3 4) 1180 ``` 1181 1182 #### -min `(list)` 1183 1184 Return the smallest value from `list` of numbers or markers. 1185 1186 ```el 1187 (-min '(0)) ;; => 0 1188 (-min '(3 2 1)) ;; => 1 1189 (-min '(1 2 3)) ;; => 1 1190 ``` 1191 1192 #### -min-by `(comparator list)` 1193 1194 Take a comparison function `comparator` and a `list` and return 1195 the least element of the list by the comparison function. 1196 1197 See also combinator [`-on`](#-on-op-trans) which can transform the values before 1198 comparing them. 1199 1200 ```el 1201 (-min-by '> '(4 3 6 1)) ;; => 1 1202 (--min-by (> (car it) (car other)) '((1 2 3) (2) (3 2))) ;; => (1 2 3) 1203 (--min-by (> (length it) (length other)) '((1 2 3) (2) (3 2))) ;; => (2) 1204 ``` 1205 1206 #### -max `(list)` 1207 1208 Return the largest value from `list` of numbers or markers. 1209 1210 ```el 1211 (-max '(0)) ;; => 0 1212 (-max '(3 2 1)) ;; => 3 1213 (-max '(1 2 3)) ;; => 3 1214 ``` 1215 1216 #### -max-by `(comparator list)` 1217 1218 Take a comparison function `comparator` and a `list` and return 1219 the greatest element of the list by the comparison function. 1220 1221 See also combinator [`-on`](#-on-op-trans) which can transform the values before 1222 comparing them. 1223 1224 ```el 1225 (-max-by '> '(4 3 6 1)) ;; => 6 1226 (--max-by (> (car it) (car other)) '((1 2 3) (2) (3 2))) ;; => (3 2) 1227 (--max-by (> (length it) (length other)) '((1 2 3) (2) (3 2))) ;; => (1 2 3) 1228 ``` 1229 1230 ## Unfolding 1231 1232 Operations dual to reductions, building lists from a seed 1233 value rather than consuming a list to produce a single value. 1234 1235 #### -iterate `(fun init n)` 1236 1237 Return a list of iterated applications of `fun` to `init`. 1238 1239 This means a list of the form: 1240 1241 (`init` (`fun` `init`) (`fun` (`fun` `init`)) ...) 1242 1243 `n` is the length of the returned list. 1244 1245 ```el 1246 (-iterate #'1+ 1 10) ;; => (1 2 3 4 5 6 7 8 9 10) 1247 (-iterate (lambda (x) (+ x x)) 2 5) ;; => (2 4 8 16 32) 1248 (--iterate (* it it) 2 5) ;; => (2 4 16 256 65536) 1249 ``` 1250 1251 #### -unfold `(fun seed)` 1252 1253 Build a list from `seed` using `fun`. 1254 1255 This is "dual" operation to [`-reduce-r`](#-reduce-r-fn-list): while -reduce-r 1256 consumes a list to produce a single value, [`-unfold`](#-unfold-fun-seed) takes a 1257 seed value and builds a (potentially infinite!) list. 1258 1259 `fun` should return `nil` to stop the generating process, or a 1260 cons (`a` . `b`), where `a` will be prepended to the result and `b` is 1261 the new seed. 1262 1263 ```el 1264 (-unfold (lambda (x) (unless (= x 0) (cons x (1- x)))) 10) ;; => (10 9 8 7 6 5 4 3 2 1) 1265 (--unfold (when it (cons it (cdr it))) '(1 2 3 4)) ;; => ((1 2 3 4) (2 3 4) (3 4) (4)) 1266 (--unfold (when it (cons it (butlast it))) '(1 2 3 4)) ;; => ((1 2 3 4) (1 2 3) (1 2) (1)) 1267 ``` 1268 1269 ## Predicates 1270 1271 Reductions of one or more lists to a boolean value. 1272 1273 #### -some `(pred list)` 1274 1275 Return (`pred` x) for the first `list` item where (`pred` x) is non-nil, else nil. 1276 1277 Alias: `-any`. 1278 1279 This function's anaphoric counterpart is `--some`. 1280 1281 ```el 1282 (-some #'stringp '(1 "2" 3)) ;; => t 1283 (--some (string-match-p "x" it) '("foo" "axe" "xor")) ;; => 1 1284 (--some (= it-index 3) '(0 1 2)) ;; => nil 1285 ``` 1286 1287 #### -every `(pred list)` 1288 1289 Return non-nil if `pred` returns non-nil for all items in `list`. 1290 If so, return the last such result of `pred`. Otherwise, once an 1291 item is reached for which `pred` returns nil, return nil without 1292 calling `pred` on any further `list` elements. 1293 1294 This function is like `-every-p`, but on success returns the last 1295 non-nil result of `pred` instead of just t. 1296 1297 This function's anaphoric counterpart is `--every`. 1298 1299 ```el 1300 (-every #'numberp '(1 2 3)) ;; => t 1301 (--every (string-match-p "x" it) '("axe" "xor")) ;; => 0 1302 (--every (= it it-index) '(0 1 3)) ;; => nil 1303 ``` 1304 1305 #### -any? `(pred list)` 1306 1307 Return t if (`pred` x) is non-nil for any x in `list`, else nil. 1308 1309 Alias: `-any-p`, `-some?`, `-some-p` 1310 1311 ```el 1312 (-any? #'numberp '(nil 0 t)) ;; => t 1313 (-any? #'numberp '(nil t t)) ;; => nil 1314 (-any? #'null '(1 3 5)) ;; => nil 1315 ``` 1316 1317 #### -all? `(pred list)` 1318 1319 Return t if (`pred` `x`) is non-nil for all `x` in `list`, else nil. 1320 In the latter case, stop after the first `x` for which (`pred` `x`) is 1321 nil, without calling `pred` on any subsequent elements of `list`. 1322 1323 The similar function [`-every`](#-every-pred-list) is more widely useful, since it 1324 returns the last non-nil result of `pred` instead of just t on 1325 success. 1326 1327 Alias: `-all-p`, `-every-p`, `-every?`. 1328 1329 This function's anaphoric counterpart is `--all?`. 1330 1331 ```el 1332 (-all? #'numberp '(1 2 3)) ;; => t 1333 (-all? #'numberp '(2 t 6)) ;; => nil 1334 (--all? (= 0 (% it 2)) '(2 4 6)) ;; => t 1335 ``` 1336 1337 #### -none? `(pred list)` 1338 1339 Return t if (`pred` x) is nil for all x in `list`, else nil. 1340 1341 Alias: `-none-p` 1342 1343 ```el 1344 (-none? 'even? '(1 2 3)) ;; => nil 1345 (-none? 'even? '(1 3 5)) ;; => t 1346 (--none? (= 0 (% it 2)) '(1 2 3)) ;; => nil 1347 ``` 1348 1349 #### -only-some? `(pred list)` 1350 1351 Return `t` if at least one item of `list` matches `pred` and at least one item of `list` does not match `pred`. 1352 Return `nil` both if all items match the predicate or if none of the items match the predicate. 1353 1354 Alias: `-only-some-p` 1355 1356 ```el 1357 (-only-some? 'even? '(1 2 3)) ;; => t 1358 (-only-some? 'even? '(1 3 5)) ;; => nil 1359 (-only-some? 'even? '(2 4 6)) ;; => nil 1360 ``` 1361 1362 #### -contains? `(list element)` 1363 1364 Return non-nil if `list` contains `element`. 1365 1366 The test for equality is done with `equal`, or with `-compare-fn` 1367 if that's non-nil. 1368 1369 Alias: `-contains-p` 1370 1371 ```el 1372 (-contains? '(1 2 3) 1) ;; => t 1373 (-contains? '(1 2 3) 2) ;; => t 1374 (-contains? '(1 2 3) 4) ;; => nil 1375 ``` 1376 1377 #### -same-items? `(list list2)` 1378 1379 Return true if `list` and `list2` has the same items. 1380 1381 The order of the elements in the lists does not matter. 1382 1383 Alias: `-same-items-p` 1384 1385 ```el 1386 (-same-items? '(1 2 3) '(1 2 3)) ;; => t 1387 (-same-items? '(1 2 3) '(3 2 1)) ;; => t 1388 (-same-items? '(1 2 3) '(1 2 3 4)) ;; => nil 1389 ``` 1390 1391 #### -is-prefix? `(prefix list)` 1392 1393 Return non-nil if `prefix` is a prefix of `list`. 1394 1395 Alias: `-is-prefix-p`. 1396 1397 ```el 1398 (-is-prefix? '(1 2 3) '(1 2 3 4 5)) ;; => t 1399 (-is-prefix? '(1 2 3 4 5) '(1 2 3)) ;; => nil 1400 (-is-prefix? '(1 3) '(1 2 3 4 5)) ;; => nil 1401 ``` 1402 1403 #### -is-suffix? `(suffix list)` 1404 1405 Return non-nil if `suffix` is a suffix of `list`. 1406 1407 Alias: `-is-suffix-p`. 1408 1409 ```el 1410 (-is-suffix? '(3 4 5) '(1 2 3 4 5)) ;; => t 1411 (-is-suffix? '(1 2 3 4 5) '(3 4 5)) ;; => nil 1412 (-is-suffix? '(3 5) '(1 2 3 4 5)) ;; => nil 1413 ``` 1414 1415 #### -is-infix? `(infix list)` 1416 1417 Return non-nil if `infix` is infix of `list`. 1418 1419 This operation runs in O(n^2) time 1420 1421 Alias: `-is-infix-p` 1422 1423 ```el 1424 (-is-infix? '(1 2 3) '(1 2 3 4 5)) ;; => t 1425 (-is-infix? '(2 3 4) '(1 2 3 4 5)) ;; => t 1426 (-is-infix? '(3 4 5) '(1 2 3 4 5)) ;; => t 1427 ``` 1428 1429 #### -cons-pair? `(obj)` 1430 1431 Return non-nil if `obj` is a true cons pair. 1432 That is, a cons (`a` . `b`) where `b` is not a list. 1433 1434 Alias: `-cons-pair-p`. 1435 1436 ```el 1437 (-cons-pair? '(1 . 2)) ;; => t 1438 (-cons-pair? '(1 2)) ;; => nil 1439 (-cons-pair? '(1)) ;; => nil 1440 ``` 1441 1442 ## Partitioning 1443 1444 Functions partitioning the input list into a list of lists. 1445 1446 #### -split-at `(n list)` 1447 1448 Split `list` into two sublists after the `n`th element. 1449 The result is a list of two elements (`take` `drop`) where `take` is a 1450 new list of the first `n` elements of `list`, and `drop` is the 1451 remaining elements of `list` (not a copy). `take` and `drop` are like 1452 the results of [`-take`](#-take-n-list) and [`-drop`](#-drop-n-list), respectively, but the split 1453 is done in a single list traversal. 1454 1455 ```el 1456 (-split-at 3 '(1 2 3 4 5)) ;; => ((1 2 3) (4 5)) 1457 (-split-at 17 '(1 2 3 4 5)) ;; => ((1 2 3 4 5) nil) 1458 (-split-at 0 '(1 2 3 4 5)) ;; => (nil (1 2 3 4 5)) 1459 ``` 1460 1461 #### -split-with `(pred list)` 1462 1463 Return a list of ((-take-while `pred` `list`) (-drop-while `pred` `list`)), in no more than one pass through the list. 1464 1465 ```el 1466 (-split-with 'even? '(1 2 3 4)) ;; => (nil (1 2 3 4)) 1467 (-split-with 'even? '(2 4 5 6)) ;; => ((2 4) (5 6)) 1468 (--split-with (< it 4) '(1 2 3 4 3 2 1)) ;; => ((1 2 3) (4 3 2 1)) 1469 ``` 1470 1471 #### -split-on `(item list)` 1472 1473 Split the `list` each time `item` is found. 1474 1475 Unlike [`-partition-by`](#-partition-by-fn-list), the `item` is discarded from the results. 1476 Empty lists are also removed from the result. 1477 1478 Comparison is done by `equal`. 1479 1480 See also [`-split-when`](#-split-when-fn-list) 1481 1482 ```el 1483 (-split-on '| '(Nil | Leaf a | Node [Tree a])) ;; => ((Nil) (Leaf a) (Node [Tree a])) 1484 (-split-on :endgroup '("a" "b" :endgroup "c" :endgroup "d" "e")) ;; => (("a" "b") ("c") ("d" "e")) 1485 (-split-on :endgroup '("a" "b" :endgroup :endgroup "d" "e")) ;; => (("a" "b") ("d" "e")) 1486 ``` 1487 1488 #### -split-when `(fn list)` 1489 1490 Split the `list` on each element where `fn` returns non-nil. 1491 1492 Unlike [`-partition-by`](#-partition-by-fn-list), the "matched" element is discarded from 1493 the results. Empty lists are also removed from the result. 1494 1495 This function can be thought of as a generalization of 1496 `split-string`. 1497 1498 ```el 1499 (-split-when 'even? '(1 2 3 4 5 6)) ;; => ((1) (3) (5)) 1500 (-split-when 'even? '(1 2 3 4 6 8 9)) ;; => ((1) (3) (9)) 1501 (--split-when (memq it '(&optional &rest)) '(a b &optional c d &rest args)) ;; => ((a b) (c d) (args)) 1502 ``` 1503 1504 #### -separate `(pred list)` 1505 1506 Return a list of ((-filter `pred` `list`) (-remove `pred` `list`)), in one pass through the list. 1507 1508 ```el 1509 (-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7)) ;; => ((2 4 6) (1 3 5 7)) 1510 (--separate (< it 5) '(3 7 5 9 3 2 1 4 6)) ;; => ((3 3 2 1 4) (7 5 9 6)) 1511 (-separate 'cdr '((1 2) (1) (1 2 3) (4))) ;; => (((1 2) (1 2 3)) ((1) (4))) 1512 ``` 1513 1514 #### -partition `(n list)` 1515 1516 Return a new list with the items in `list` grouped into `n`-sized sublists. 1517 If there are not enough items to make the last group `n`-sized, 1518 those items are discarded. 1519 1520 ```el 1521 (-partition 2 '(1 2 3 4 5 6)) ;; => ((1 2) (3 4) (5 6)) 1522 (-partition 2 '(1 2 3 4 5 6 7)) ;; => ((1 2) (3 4) (5 6)) 1523 (-partition 3 '(1 2 3 4 5 6 7)) ;; => ((1 2 3) (4 5 6)) 1524 ``` 1525 1526 #### -partition-all `(n list)` 1527 1528 Return a new list with the items in `list` grouped into `n`-sized sublists. 1529 The last group may contain less than `n` items. 1530 1531 ```el 1532 (-partition-all 2 '(1 2 3 4 5 6)) ;; => ((1 2) (3 4) (5 6)) 1533 (-partition-all 2 '(1 2 3 4 5 6 7)) ;; => ((1 2) (3 4) (5 6) (7)) 1534 (-partition-all 3 '(1 2 3 4 5 6 7)) ;; => ((1 2 3) (4 5 6) (7)) 1535 ``` 1536 1537 #### -partition-in-steps `(n step list)` 1538 1539 Return a new list with the items in `list` grouped into `n`-sized sublists at offsets `step` apart. 1540 If there are not enough items to make the last group `n`-sized, 1541 those items are discarded. 1542 1543 ```el 1544 (-partition-in-steps 2 1 '(1 2 3 4)) ;; => ((1 2) (2 3) (3 4)) 1545 (-partition-in-steps 3 2 '(1 2 3 4)) ;; => ((1 2 3)) 1546 (-partition-in-steps 3 2 '(1 2 3 4 5)) ;; => ((1 2 3) (3 4 5)) 1547 ``` 1548 1549 #### -partition-all-in-steps `(n step list)` 1550 1551 Return a new list with the items in `list` grouped into `n`-sized sublists at offsets `step` apart. 1552 The last groups may contain less than `n` items. 1553 1554 ```el 1555 (-partition-all-in-steps 2 1 '(1 2 3 4)) ;; => ((1 2) (2 3) (3 4) (4)) 1556 (-partition-all-in-steps 3 2 '(1 2 3 4)) ;; => ((1 2 3) (3 4)) 1557 (-partition-all-in-steps 3 2 '(1 2 3 4 5)) ;; => ((1 2 3) (3 4 5) (5)) 1558 ``` 1559 1560 #### -partition-by `(fn list)` 1561 1562 Apply `fn` to each item in `list`, splitting it each time `fn` returns a new value. 1563 1564 ```el 1565 (-partition-by 'even? ()) ;; => () 1566 (-partition-by 'even? '(1 1 2 2 2 3 4 6 8)) ;; => ((1 1) (2 2 2) (3) (4 6 8)) 1567 (--partition-by (< it 3) '(1 2 3 4 3 2 1)) ;; => ((1 2) (3 4 3) (2 1)) 1568 ``` 1569 1570 #### -partition-by-header `(fn list)` 1571 1572 Apply `fn` to the first item in `list`. That is the header 1573 value. Apply `fn` to each item in `list`, splitting it each time `fn` 1574 returns the header value, but only after seeing at least one 1575 other value (the body). 1576 1577 ```el 1578 (--partition-by-header (= it 1) '(1 2 3 1 2 1 2 3 4)) ;; => ((1 2 3) (1 2) (1 2 3 4)) 1579 (--partition-by-header (> it 0) '(1 2 0 1 0 1 2 3 0)) ;; => ((1 2 0) (1 0) (1 2 3 0)) 1580 (-partition-by-header 'even? '(2 1 1 1 4 1 3 5 6 6 1)) ;; => ((2 1 1 1) (4 1 3 5) (6 6 1)) 1581 ``` 1582 1583 #### -partition-after-pred `(pred list)` 1584 1585 Partition `list` after each element for which `pred` returns non-nil. 1586 1587 This function's anaphoric counterpart is `--partition-after-pred`. 1588 1589 ```el 1590 (-partition-after-pred #'booleanp ()) ;; => () 1591 (-partition-after-pred #'booleanp '(t t)) ;; => ((t) (t)) 1592 (-partition-after-pred #'booleanp '(0 0 t t 0 t)) ;; => ((0 0 t) (t) (0 t)) 1593 ``` 1594 1595 #### -partition-before-pred `(pred list)` 1596 1597 Partition directly before each time `pred` is true on an element of `list`. 1598 1599 ```el 1600 (-partition-before-pred #'booleanp ()) ;; => () 1601 (-partition-before-pred #'booleanp '(0 t)) ;; => ((0) (t)) 1602 (-partition-before-pred #'booleanp '(0 0 t 0 t t)) ;; => ((0 0) (t 0) (t) (t)) 1603 ``` 1604 1605 #### -partition-before-item `(item list)` 1606 1607 Partition directly before each time `item` appears in `list`. 1608 1609 ```el 1610 (-partition-before-item 3 ()) ;; => () 1611 (-partition-before-item 3 '(1)) ;; => ((1)) 1612 (-partition-before-item 3 '(3)) ;; => ((3)) 1613 ``` 1614 1615 #### -partition-after-item `(item list)` 1616 1617 Partition directly after each time `item` appears in `list`. 1618 1619 ```el 1620 (-partition-after-item 3 ()) ;; => () 1621 (-partition-after-item 3 '(1)) ;; => ((1)) 1622 (-partition-after-item 3 '(3)) ;; => ((3)) 1623 ``` 1624 1625 #### -group-by `(fn list)` 1626 1627 Separate `list` into an alist whose keys are `fn` applied to the 1628 elements of `list`. Keys are compared by `equal`. 1629 1630 ```el 1631 (-group-by 'even? ()) ;; => () 1632 (-group-by 'even? '(1 1 2 2 2 3 4 6 8)) ;; => ((nil 1 1 3) (t 2 2 2 4 6 8)) 1633 (--group-by (car (split-string it "/")) '("a/b" "c/d" "a/e")) ;; => (("a" "a/b" "a/e") ("c" "c/d")) 1634 ``` 1635 1636 ## Indexing 1637 1638 Functions retrieving or sorting based on list indices and 1639 related predicates. 1640 1641 #### -elem-index `(elem list)` 1642 1643 Return the index of the first element in the given `list` which 1644 is equal to the query element `elem`, or nil if there is no 1645 such element. 1646 1647 ```el 1648 (-elem-index 2 '(6 7 8 2 3 4)) ;; => 3 1649 (-elem-index "bar" '("foo" "bar" "baz")) ;; => 1 1650 (-elem-index '(1 2) '((3) (5 6) (1 2) nil)) ;; => 2 1651 ``` 1652 1653 #### -elem-indices `(elem list)` 1654 1655 Return the indices of all elements in `list` equal to the query 1656 element `elem`, in ascending order. 1657 1658 ```el 1659 (-elem-indices 2 '(6 7 8 2 3 4 2 1)) ;; => (3 6) 1660 (-elem-indices "bar" '("foo" "bar" "baz")) ;; => (1) 1661 (-elem-indices '(1 2) '((3) (1 2) (5 6) (1 2) nil)) ;; => (1 3) 1662 ``` 1663 1664 #### -find-index `(pred list)` 1665 1666 Take a predicate `pred` and a `list` and return the index of the 1667 first element in the list satisfying the predicate, or nil if 1668 there is no such element. 1669 1670 See also [`-first`](#-first-pred-list). 1671 1672 ```el 1673 (-find-index 'even? '(2 4 1 6 3 3 5 8)) ;; => 0 1674 (--find-index (< 5 it) '(2 4 1 6 3 3 5 8)) ;; => 3 1675 (-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz")) ;; => 1 1676 ``` 1677 1678 #### -find-last-index `(pred list)` 1679 1680 Take a predicate `pred` and a `list` and return the index of the 1681 last element in the list satisfying the predicate, or nil if 1682 there is no such element. 1683 1684 See also [`-last`](#-last-pred-list). 1685 1686 ```el 1687 (-find-last-index 'even? '(2 4 1 6 3 3 5 8)) ;; => 7 1688 (--find-last-index (< 5 it) '(2 7 1 6 3 8 5 2)) ;; => 5 1689 (-find-last-index (-partial 'string-lessp "baz") '("q" "foo" "baz")) ;; => 1 1690 ``` 1691 1692 #### -find-indices `(pred list)` 1693 1694 Return the indices of all elements in `list` satisfying the 1695 predicate `pred`, in ascending order. 1696 1697 ```el 1698 (-find-indices 'even? '(2 4 1 6 3 3 5 8)) ;; => (0 1 3 7) 1699 (--find-indices (< 5 it) '(2 4 1 6 3 3 5 8)) ;; => (3 7) 1700 (-find-indices (-partial 'string-lessp "baz") '("bar" "foo" "baz")) ;; => (1) 1701 ``` 1702 1703 #### -grade-up `(comparator list)` 1704 1705 Grade elements of `list` using `comparator` relation. 1706 This yields a permutation vector such that applying this 1707 permutation to `list` sorts it in ascending order. 1708 1709 ```el 1710 (-grade-up #'< '(3 1 4 2 1 3 3)) ;; => (1 4 3 0 5 6 2) 1711 (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up #'< l) l)) ;; => (1 1 2 3 3 3 4) 1712 ``` 1713 1714 #### -grade-down `(comparator list)` 1715 1716 Grade elements of `list` using `comparator` relation. 1717 This yields a permutation vector such that applying this 1718 permutation to `list` sorts it in descending order. 1719 1720 ```el 1721 (-grade-down #'< '(3 1 4 2 1 3 3)) ;; => (2 0 5 6 3 1 4) 1722 (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down #'< l) l)) ;; => (4 3 3 3 2 1 1) 1723 ``` 1724 1725 ## Set operations 1726 1727 Operations pretending lists are sets. 1728 1729 #### -union `(list list2)` 1730 1731 Return a new list containing the elements of `list` and elements of `list2` that are not in `list`. 1732 The test for equality is done with `equal`, 1733 or with `-compare-fn` if that's non-nil. 1734 1735 ```el 1736 (-union '(1 2 3) '(3 4 5)) ;; => (1 2 3 4 5) 1737 (-union '(1 2 3 4) ()) ;; => (1 2 3 4) 1738 (-union '(1 1 2 2) '(3 2 1)) ;; => (1 1 2 2 3) 1739 ``` 1740 1741 #### -difference `(list list2)` 1742 1743 Return a new list with only the members of `list` that are not in `list2`. 1744 The test for equality is done with `equal`, 1745 or with `-compare-fn` if that's non-nil. 1746 1747 ```el 1748 (-difference () ()) ;; => () 1749 (-difference '(1 2 3) '(4 5 6)) ;; => (1 2 3) 1750 (-difference '(1 2 3 4) '(3 4 5 6)) ;; => (1 2) 1751 ``` 1752 1753 #### -intersection `(list list2)` 1754 1755 Return a new list containing only the elements that are members of both `list` and `list2`. 1756 The test for equality is done with `equal`, 1757 or with `-compare-fn` if that's non-nil. 1758 1759 ```el 1760 (-intersection () ()) ;; => () 1761 (-intersection '(1 2 3) '(4 5 6)) ;; => () 1762 (-intersection '(1 2 3 4) '(3 4 5 6)) ;; => (3 4) 1763 ``` 1764 1765 #### -powerset `(list)` 1766 1767 Return the power set of `list`. 1768 1769 ```el 1770 (-powerset ()) ;; => (nil) 1771 (-powerset '(x y z)) ;; => ((x y z) (x y) (x z) (x) (y z) (y) (z) nil) 1772 ``` 1773 1774 #### -permutations `(list)` 1775 1776 Return the permutations of `list`. 1777 1778 ```el 1779 (-permutations ()) ;; => (nil) 1780 (-permutations '(1 2)) ;; => ((1 2) (2 1)) 1781 (-permutations '(a b c)) ;; => ((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)) 1782 ``` 1783 1784 #### -distinct `(list)` 1785 1786 Return a new list with all duplicates removed. 1787 The test for equality is done with `equal`, 1788 or with `-compare-fn` if that's non-nil. 1789 1790 Alias: `-uniq` 1791 1792 ```el 1793 (-distinct ()) ;; => () 1794 (-distinct '(1 2 2 4)) ;; => (1 2 4) 1795 (-distinct '(t t t)) ;; => (t) 1796 ``` 1797 1798 ## Other list operations 1799 1800 Other list functions not fit to be classified elsewhere. 1801 1802 #### -rotate `(n list)` 1803 1804 Rotate `list` `n` places to the right (left if `n` is negative). 1805 The time complexity is O(n). 1806 1807 ```el 1808 (-rotate 3 '(1 2 3 4 5 6 7)) ;; => (5 6 7 1 2 3 4) 1809 (-rotate -3 '(1 2 3 4 5 6 7)) ;; => (4 5 6 7 1 2 3) 1810 (-rotate 16 '(1 2 3 4 5 6 7)) ;; => (6 7 1 2 3 4 5) 1811 ``` 1812 1813 #### -repeat `(n x)` 1814 1815 Return a new list of length `n` with each element being `x`. 1816 Return nil if `n` is less than 1. 1817 1818 ```el 1819 (-repeat 3 :a) ;; => (:a :a :a) 1820 (-repeat 1 :a) ;; => (:a) 1821 (-repeat 0 :a) ;; => nil 1822 ``` 1823 1824 #### -cons* `(&rest args)` 1825 1826 Make a new list from the elements of `args`. 1827 The last 2 elements of `args` are used as the final cons of the 1828 result, so if the final element of `args` is not a list, the result 1829 is a dotted list. With no `args`, return nil. 1830 1831 ```el 1832 (-cons* 1 2) ;; => (1 . 2) 1833 (-cons* 1 2 3) ;; => (1 2 . 3) 1834 (-cons* 1) ;; => 1 1835 ``` 1836 1837 #### -snoc `(list elem &rest elements)` 1838 1839 Append `elem` to the end of the list. 1840 1841 This is like `cons`, but operates on the end of list. 1842 1843 If `elements` is non nil, append these to the list as well. 1844 1845 ```el 1846 (-snoc '(1 2 3) 4) ;; => (1 2 3 4) 1847 (-snoc '(1 2 3) 4 5 6) ;; => (1 2 3 4 5 6) 1848 (-snoc '(1 2 3) '(4 5 6)) ;; => (1 2 3 (4 5 6)) 1849 ``` 1850 1851 #### -interpose `(sep list)` 1852 1853 Return a new list of all elements in `list` separated by `sep`. 1854 1855 ```el 1856 (-interpose "-" ()) ;; => () 1857 (-interpose "-" '("a")) ;; => ("a") 1858 (-interpose "-" '("a" "b" "c")) ;; => ("a" "-" "b" "-" "c") 1859 ``` 1860 1861 #### -interleave `(&rest lists)` 1862 1863 Return a new list of the first item in each list, then the second etc. 1864 1865 ```el 1866 (-interleave '(1 2) '("a" "b")) ;; => (1 "a" 2 "b") 1867 (-interleave '(1 2) '("a" "b") '("A" "B")) ;; => (1 "a" "A" 2 "b" "B") 1868 (-interleave '(1 2 3) '("a" "b")) ;; => (1 "a" 2 "b") 1869 ``` 1870 1871 #### -iota `(count &optional start step)` 1872 1873 Return a list containing `count` numbers. 1874 Starts from `start` and adds `step` each time. The default `start` is 1875 zero, the default `step` is 1. 1876 This function takes its name from the corresponding primitive in 1877 the `apl` language. 1878 1879 ```el 1880 (-iota 6) ;; => (0 1 2 3 4 5) 1881 (-iota 4 2.5 -2) ;; => (2.5 0.5 -1.5 -3.5) 1882 (-iota -1) ;; Wrong type argument: natnump, -1 1883 ``` 1884 1885 #### -zip-with `(fn list1 list2)` 1886 1887 Zip the two lists `list1` and `list2` using a function `fn`. This 1888 function is applied pairwise taking as first argument element of 1889 `list1` and as second argument element of `list2` at corresponding 1890 position. 1891 1892 The anaphoric form `--zip-with` binds the elements from `list1` as symbol `it`, 1893 and the elements from `list2` as symbol `other`. 1894 1895 ```el 1896 (-zip-with '+ '(1 2 3) '(4 5 6)) ;; => (5 7 9) 1897 (-zip-with 'cons '(1 2 3) '(4 5 6)) ;; => ((1 . 4) (2 . 5) (3 . 6)) 1898 (--zip-with (concat it " and " other) '("Batman" "Jekyll") '("Robin" "Hyde")) ;; => ("Batman and Robin" "Jekyll and Hyde") 1899 ``` 1900 1901 #### -zip `(&rest lists)` 1902 1903 Zip `lists` together. Group the head of each list, followed by the 1904 second elements of each list, and so on. The lengths of the returned 1905 groupings are equal to the length of the shortest input list. 1906 1907 If two lists are provided as arguments, return the groupings as a list 1908 of cons cells. Otherwise, return the groupings as a list of lists. 1909 1910 Use [`-zip-lists`](#-zip-lists-rest-lists) if you need the return value to always be a list 1911 of lists. 1912 1913 Alias: `-zip-pair` 1914 1915 See also: [`-zip-lists`](#-zip-lists-rest-lists) 1916 1917 ```el 1918 (-zip '(1 2 3) '(4 5 6)) ;; => ((1 . 4) (2 . 5) (3 . 6)) 1919 (-zip '(1 2 3) '(4 5 6 7)) ;; => ((1 . 4) (2 . 5) (3 . 6)) 1920 (-zip '(1 2) '(3 4 5) '(6)) ;; => ((1 3 6)) 1921 ``` 1922 1923 #### -zip-lists `(&rest lists)` 1924 1925 Zip `lists` together. Group the head of each list, followed by the 1926 second elements of each list, and so on. The lengths of the returned 1927 groupings are equal to the length of the shortest input list. 1928 1929 The return value is always list of lists, which is a difference 1930 from `-zip-pair` which returns a cons-cell in case two input 1931 lists are provided. 1932 1933 See also: [`-zip`](#-zip-rest-lists) 1934 1935 ```el 1936 (-zip-lists '(1 2 3) '(4 5 6)) ;; => ((1 4) (2 5) (3 6)) 1937 (-zip-lists '(1 2 3) '(4 5 6 7)) ;; => ((1 4) (2 5) (3 6)) 1938 (-zip-lists '(1 2) '(3 4 5) '(6)) ;; => ((1 3 6)) 1939 ``` 1940 1941 #### -zip-fill `(fill-value &rest lists)` 1942 1943 Zip `lists`, with `fill-value` padded onto the shorter lists. The 1944 lengths of the returned groupings are equal to the length of the 1945 longest input list. 1946 1947 ```el 1948 (-zip-fill 0 '(1 2 3 4 5) '(6 7 8 9)) ;; => ((1 . 6) (2 . 7) (3 . 8) (4 . 9) (5 . 0)) 1949 ``` 1950 1951 #### -unzip `(lists)` 1952 1953 Unzip `lists`. 1954 1955 This works just like [`-zip`](#-zip-rest-lists) but takes a list of lists instead of 1956 a variable number of arguments, such that 1957 1958 (-unzip (-zip `l1` `l2` `l3` ...)) 1959 1960 is identity (given that the lists are the same length). 1961 1962 Note in particular that calling this on a list of two lists will 1963 return a list of cons-cells such that the above identity works. 1964 1965 See also: [`-zip`](#-zip-rest-lists) 1966 1967 ```el 1968 (-unzip (-zip '(1 2 3) '(a b c) '("e" "f" "g"))) ;; => ((1 2 3) (a b c) ("e" "f" "g")) 1969 (-unzip '((1 2) (3 4) (5 6) (7 8) (9 10))) ;; => ((1 3 5 7 9) (2 4 6 8 10)) 1970 (-unzip '((1 2) (3 4))) ;; => ((1 . 3) (2 . 4)) 1971 ``` 1972 1973 #### -cycle `(list)` 1974 1975 Return an infinite circular copy of `list`. 1976 The returned list cycles through the elements of `list` and repeats 1977 from the beginning. 1978 1979 ```el 1980 (-take 5 (-cycle '(1 2 3))) ;; => (1 2 3 1 2) 1981 (-take 7 (-cycle '(1 "and" 3))) ;; => (1 "and" 3 1 "and" 3 1) 1982 (-zip (-cycle '(1 2 3)) '(1 2)) ;; => ((1 . 1) (2 . 2)) 1983 ``` 1984 1985 #### -pad `(fill-value &rest lists)` 1986 1987 Appends `fill-value` to the end of each list in `lists` such that they 1988 will all have the same length. 1989 1990 ```el 1991 (-pad 0 ()) ;; => (nil) 1992 (-pad 0 '(1)) ;; => ((1)) 1993 (-pad 0 '(1 2 3) '(4 5)) ;; => ((1 2 3) (4 5 0)) 1994 ``` 1995 1996 #### -table `(fn &rest lists)` 1997 1998 Compute outer product of `lists` using function `fn`. 1999 2000 The function `fn` should have the same arity as the number of 2001 supplied lists. 2002 2003 The outer product is computed by applying fn to all possible 2004 combinations created by taking one element from each list in 2005 order. The dimension of the result is (length lists). 2006 2007 See also: [`-table-flat`](#-table-flat-fn-rest-lists) 2008 2009 ```el 2010 (-table '* '(1 2 3) '(1 2 3)) ;; => ((1 2 3) (2 4 6) (3 6 9)) 2011 (-table (lambda (a b) (-sum (-zip-with '* a b))) '((1 2) (3 4)) '((1 3) (2 4))) ;; => ((7 15) (10 22)) 2012 (apply '-table 'list (-repeat 3 '(1 2))) ;; => ((((1 1 1) (2 1 1)) ((1 2 1) (2 2 1))) (((1 1 2) (2 1 2)) ((1 2 2) (2 2 2)))) 2013 ``` 2014 2015 #### -table-flat `(fn &rest lists)` 2016 2017 Compute flat outer product of `lists` using function `fn`. 2018 2019 The function `fn` should have the same arity as the number of 2020 supplied lists. 2021 2022 The outer product is computed by applying fn to all possible 2023 combinations created by taking one element from each list in 2024 order. The results are flattened, ignoring the tensor structure 2025 of the result. This is equivalent to calling: 2026 2027 (-flatten-n (1- (length lists)) (apply '-table fn lists)) 2028 2029 but the implementation here is much more efficient. 2030 2031 See also: [`-flatten-n`](#-flatten-n-num-list), [`-table`](#-table-fn-rest-lists) 2032 2033 ```el 2034 (-table-flat 'list '(1 2 3) '(a b c)) ;; => ((1 a) (2 a) (3 a) (1 b) (2 b) (3 b) (1 c) (2 c) (3 c)) 2035 (-table-flat '* '(1 2 3) '(1 2 3)) ;; => (1 2 3 2 4 6 3 6 9) 2036 (apply '-table-flat 'list (-repeat 3 '(1 2))) ;; => ((1 1 1) (2 1 1) (1 2 1) (2 2 1) (1 1 2) (2 1 2) (1 2 2) (2 2 2)) 2037 ``` 2038 2039 #### -first `(pred list)` 2040 2041 Return the first item in `list` for which `pred` returns non-nil. 2042 Return nil if no such element is found. 2043 To get the first item in the list no questions asked, use `car`. 2044 2045 Alias: `-find`. 2046 2047 This function's anaphoric counterpart is `--first`. 2048 2049 ```el 2050 (-first #'natnump '(-1 0 1)) ;; => 0 2051 (-first #'null '(1 2 3)) ;; => nil 2052 (--first (> it 2) '(1 2 3)) ;; => 3 2053 ``` 2054 2055 #### -last `(pred list)` 2056 2057 Return the last x in `list` where (`pred` x) is non-nil, else nil. 2058 2059 ```el 2060 (-last 'even? '(1 2 3 4 5 6 3 3 3)) ;; => 6 2061 (-last 'even? '(1 3 7 5 9)) ;; => nil 2062 (--last (> (length it) 3) '("a" "looong" "word" "and" "short" "one")) ;; => "short" 2063 ``` 2064 2065 #### -first-item `(list)` 2066 2067 Return the first item of `list`, or nil on an empty list. 2068 2069 See also: [`-second-item`](#-second-item-list), [`-last-item`](#-last-item-list). 2070 2071 ```el 2072 (-first-item '(1 2 3)) ;; => 1 2073 (-first-item nil) ;; => nil 2074 (let ((list (list 1 2 3))) (setf (-first-item list) 5) list) ;; => (5 2 3) 2075 ``` 2076 2077 #### -second-item `(list)` 2078 2079 Return the second item of `list`, or nil if `list` is too short. 2080 2081 See also: [`-third-item`](#-third-item-list). 2082 2083 ```el 2084 (-second-item '(1 2 3)) ;; => 2 2085 (-second-item nil) ;; => nil 2086 ``` 2087 2088 #### -third-item `(list)` 2089 2090 Return the third item of `list`, or nil if `list` is too short. 2091 2092 See also: [`-fourth-item`](#-fourth-item-list). 2093 2094 ```el 2095 (-third-item '(1 2 3)) ;; => 3 2096 (-third-item nil) ;; => nil 2097 ``` 2098 2099 #### -fourth-item `(list)` 2100 2101 Return the fourth item of `list`, or nil if `list` is too short. 2102 2103 See also: [`-fifth-item`](#-fifth-item-list). 2104 2105 ```el 2106 (-fourth-item '(1 2 3 4)) ;; => 4 2107 (-fourth-item nil) ;; => nil 2108 ``` 2109 2110 #### -fifth-item `(list)` 2111 2112 Return the fifth item of `list`, or nil if `list` is too short. 2113 2114 See also: [`-last-item`](#-last-item-list). 2115 2116 ```el 2117 (-fifth-item '(1 2 3 4 5)) ;; => 5 2118 (-fifth-item nil) ;; => nil 2119 ``` 2120 2121 #### -last-item `(list)` 2122 2123 Return the last item of `list`, or nil on an empty list. 2124 2125 ```el 2126 (-last-item '(1 2 3)) ;; => 3 2127 (-last-item nil) ;; => nil 2128 (let ((list (list 1 2 3))) (setf (-last-item list) 5) list) ;; => (1 2 5) 2129 ``` 2130 2131 #### -butlast `(list)` 2132 2133 Return a list of all items in list except for the last. 2134 2135 ```el 2136 (-butlast '(1 2 3)) ;; => (1 2) 2137 (-butlast '(1 2)) ;; => (1) 2138 (-butlast '(1)) ;; => nil 2139 ``` 2140 2141 #### -sort `(comparator list)` 2142 2143 Sort `list`, stably, comparing elements using `comparator`. 2144 Return the sorted list. `list` is `not` modified by side effects. 2145 `comparator` is called with two elements of `list`, and should return non-nil 2146 if the first element should sort before the second. 2147 2148 ```el 2149 (-sort '< '(3 1 2)) ;; => (1 2 3) 2150 (-sort '> '(3 1 2)) ;; => (3 2 1) 2151 (--sort (< it other) '(3 1 2)) ;; => (1 2 3) 2152 ``` 2153 2154 #### -list `(arg)` 2155 2156 Ensure `arg` is a list. 2157 If `arg` is already a list, return it as is (not a copy). 2158 Otherwise, return a new list with `arg` as its only element. 2159 2160 Another supported calling convention is (-list &rest `args`). 2161 In this case, if `arg` is not a list, a new list with all of 2162 `args` as elements is returned. This use is supported for 2163 backward compatibility and is otherwise deprecated. 2164 2165 ```el 2166 (-list 1) ;; => (1) 2167 (-list ()) ;; => () 2168 (-list '(1 2 3)) ;; => (1 2 3) 2169 ``` 2170 2171 #### -fix `(fn list)` 2172 2173 Compute the (least) fixpoint of `fn` with initial input `list`. 2174 2175 `fn` is called at least once, results are compared with `equal`. 2176 2177 ```el 2178 (-fix (lambda (l) (-non-nil (--mapcat (-split-at (/ (length it) 2) it) l))) '((1 2 3))) ;; => ((1) (2) (3)) 2179 (let ((l '((starwars scifi) (jedi starwars warrior)))) (--fix (-uniq (--mapcat (cons it (cdr (assq it l))) it)) '(jedi book))) ;; => (jedi starwars warrior scifi book) 2180 ``` 2181 2182 ## Tree operations 2183 2184 Functions pretending lists are trees. 2185 2186 #### -tree-seq `(branch children tree)` 2187 2188 Return a sequence of the nodes in `tree`, in depth-first search order. 2189 2190 `branch` is a predicate of one argument that returns non-nil if the 2191 passed argument is a branch, that is, a node that can have children. 2192 2193 `children` is a function of one argument that returns the children 2194 of the passed branch node. 2195 2196 Non-branch nodes are simply copied. 2197 2198 ```el 2199 (-tree-seq 'listp 'identity '(1 (2 3) 4 (5 (6 7)))) ;; => ((1 (2 3) 4 (5 (6 7))) 1 (2 3) 2 3 4 (5 (6 7)) 5 (6 7) 6 7) 2200 (-tree-seq 'listp 'reverse '(1 (2 3) 4 (5 (6 7)))) ;; => ((1 (2 3) 4 (5 (6 7))) (5 (6 7)) (6 7) 7 6 5 4 (2 3) 3 2 1) 2201 (--tree-seq (vectorp it) (append it nil) [1 [2 3] 4 [5 [6 7]]]) ;; => ([1 [2 3] 4 [5 [6 7]]] 1 [2 3] 2 3 4 [5 [6 7]] 5 [6 7] 6 7) 2202 ``` 2203 2204 #### -tree-map `(fn tree)` 2205 2206 Apply `fn` to each element of `tree` while preserving the tree structure. 2207 2208 ```el 2209 (-tree-map '1+ '(1 (2 3) (4 (5 6) 7))) ;; => (2 (3 4) (5 (6 7) 8)) 2210 (-tree-map '(lambda (x) (cons x (expt 2 x))) '(1 (2 3) 4)) ;; => ((1 . 2) ((2 . 4) (3 . 8)) (4 . 16)) 2211 (--tree-map (length it) '("<body>" ("<p>" "text" "</p>") "</body>")) ;; => (6 (3 4 4) 7) 2212 ``` 2213 2214 #### -tree-map-nodes `(pred fun tree)` 2215 2216 Call `fun` on each node of `tree` that satisfies `pred`. 2217 2218 If `pred` returns nil, continue descending down this node. If `pred` 2219 returns non-nil, apply `fun` to this node and do not descend 2220 further. 2221 2222 ```el 2223 (-tree-map-nodes 'vectorp (lambda (x) (-sum (append x nil))) '(1 [2 3] 4 (5 [6 7] 8))) ;; => (1 5 4 (5 13 8)) 2224 (-tree-map-nodes 'keywordp (lambda (x) (symbol-name x)) '(1 :foo 4 ((5 6 :bar) :baz 8))) ;; => (1 ":foo" 4 ((5 6 ":bar") ":baz" 8)) 2225 (--tree-map-nodes (eq (car-safe it) 'add-mode) (-concat it (list :mode 'emacs-lisp-mode)) '(with-mode emacs-lisp-mode (foo bar) (add-mode a b) (baz (add-mode c d)))) ;; => (with-mode emacs-lisp-mode (foo bar) (add-mode a b :mode emacs-lisp-mode) (baz (add-mode c d :mode emacs-lisp-mode))) 2226 ``` 2227 2228 #### -tree-reduce `(fn tree)` 2229 2230 Use `fn` to reduce elements of list `tree`. 2231 If elements of `tree` are lists themselves, apply the reduction recursively. 2232 2233 `fn` is first applied to first element of the list and second 2234 element, then on this result and third element from the list etc. 2235 2236 See [`-reduce-r`](#-reduce-r-fn-list) for how exactly are lists of zero or one element handled. 2237 2238 ```el 2239 (-tree-reduce '+ '(1 (2 3) (4 5))) ;; => 15 2240 (-tree-reduce 'concat '("strings" (" on" " various") ((" levels")))) ;; => "strings on various levels" 2241 (--tree-reduce (cond ((stringp it) (concat it " " acc)) (t (let ((sn (symbol-name it))) (concat "<" sn ">" acc "</" sn ">")))) '(body (p "some words") (div "more" (b "bold") "words"))) ;; => "<body><p>some words</p> <div>more <b>bold</b> words</div></body>" 2242 ``` 2243 2244 #### -tree-reduce-from `(fn init-value tree)` 2245 2246 Use `fn` to reduce elements of list `tree`. 2247 If elements of `tree` are lists themselves, apply the reduction recursively. 2248 2249 `fn` is first applied to `init-value` and first element of the list, 2250 then on this result and second element from the list etc. 2251 2252 The initial value is ignored on cons pairs as they always contain 2253 two elements. 2254 2255 ```el 2256 (-tree-reduce-from '+ 1 '(1 (1 1) ((1)))) ;; => 8 2257 (--tree-reduce-from (-concat acc (list it)) nil '(1 (2 3 (4 5)) (6 7))) ;; => ((7 6) ((5 4) 3 2) 1) 2258 ``` 2259 2260 #### -tree-mapreduce `(fn folder tree)` 2261 2262 Apply `fn` to each element of `tree`, and make a list of the results. 2263 If elements of `tree` are lists themselves, apply `fn` recursively to 2264 elements of these nested lists. 2265 2266 Then reduce the resulting lists using `folder` and initial value 2267 `init-value`. See [`-reduce-r-from`](#-reduce-r-from-fn-init-list). 2268 2269 This is the same as calling [`-tree-reduce`](#-tree-reduce-fn-tree) after [`-tree-map`](#-tree-map-fn-tree) 2270 but is twice as fast as it only traverse the structure once. 2271 2272 ```el 2273 (-tree-mapreduce 'list 'append '(1 (2 (3 4) (5 6)) (7 (8 9)))) ;; => (1 2 3 4 5 6 7 8 9) 2274 (--tree-mapreduce 1 (+ it acc) '(1 (2 (4 9) (2 1)) (7 (4 3)))) ;; => 9 2275 (--tree-mapreduce 0 (max acc (1+ it)) '(1 (2 (4 9) (2 1)) (7 (4 3)))) ;; => 3 2276 ``` 2277 2278 #### -tree-mapreduce-from `(fn folder init-value tree)` 2279 2280 Apply `fn` to each element of `tree`, and make a list of the results. 2281 If elements of `tree` are lists themselves, apply `fn` recursively to 2282 elements of these nested lists. 2283 2284 Then reduce the resulting lists using `folder` and initial value 2285 `init-value`. See [`-reduce-r-from`](#-reduce-r-from-fn-init-list). 2286 2287 This is the same as calling [`-tree-reduce-from`](#-tree-reduce-from-fn-init-value-tree) after [`-tree-map`](#-tree-map-fn-tree) 2288 but is twice as fast as it only traverse the structure once. 2289 2290 ```el 2291 (-tree-mapreduce-from 'identity '* 1 '(1 (2 (3 4) (5 6)) (7 (8 9)))) ;; => 362880 2292 (--tree-mapreduce-from (+ it it) (cons it acc) nil '(1 (2 (4 9) (2 1)) (7 (4 3)))) ;; => (2 (4 (8 18) (4 2)) (14 (8 6))) 2293 (concat "{" (--tree-mapreduce-from (cond ((-cons-pair? it) (concat (symbol-name (car it)) " -> " (symbol-name (cdr it)))) (t (concat (symbol-name it) " : {"))) (concat it (unless (or (equal acc "}") (equal (substring it (1- (length it))) "{")) ", ") acc) "}" '((elisp-mode (foo (bar . booze)) (baz . qux)) (c-mode (foo . bla) (bum . bam))))) ;; => "{elisp-mode : {foo : {bar -> booze}, baz -> qux}, c-mode : {foo -> bla, bum -> bam}}" 2294 ``` 2295 2296 #### -clone `(list)` 2297 2298 Create a deep copy of `list`. 2299 The new list has the same elements and structure but all cons are 2300 replaced with new ones. This is useful when you need to clone a 2301 structure such as plist or alist. 2302 2303 ```el 2304 (let* ((a '(1 2 3)) (b (-clone a))) (nreverse a) b) ;; => (1 2 3) 2305 ``` 2306 2307 ## Threading macros 2308 2309 Macros that conditionally combine sequential forms for brevity 2310 or readability. 2311 2312 #### -> `(x &optional form &rest more)` 2313 2314 Thread the expr through the forms. Insert `x` as the second item 2315 in the first form, making a list of it if it is not a list 2316 already. If there are more forms, insert the first form as the 2317 second item in second form, etc. 2318 2319 ```el 2320 (-> '(2 3 5)) ;; => (2 3 5) 2321 (-> '(2 3 5) (append '(8 13))) ;; => (2 3 5 8 13) 2322 (-> '(2 3 5) (append '(8 13)) (-slice 1 -1)) ;; => (3 5 8) 2323 ``` 2324 2325 #### ->> `(x &optional form &rest more)` 2326 2327 Thread the expr through the forms. Insert `x` as the last item 2328 in the first form, making a list of it if it is not a list 2329 already. If there are more forms, insert the first form as the 2330 last item in second form, etc. 2331 2332 ```el 2333 (->> '(1 2 3) (-map 'square)) ;; => (1 4 9) 2334 (->> '(1 2 3) (-map 'square) (-remove 'even?)) ;; => (1 9) 2335 (->> '(1 2 3) (-map 'square) (-reduce '+)) ;; => 14 2336 ``` 2337 2338 #### --> `(x &rest forms)` 2339 2340 Starting with the value of `x`, thread each expression through `forms`. 2341 2342 Insert `x` at the position signified by the symbol `it` in the first 2343 form. If there are more forms, insert the first form at the position 2344 signified by `it` in in second form, etc. 2345 2346 ```el 2347 (--> "def" (concat "abc" it "ghi")) ;; => "abcdefghi" 2348 (--> "def" (concat "abc" it "ghi") (upcase it)) ;; => "ABCDEFGHI" 2349 (--> "def" (concat "abc" it "ghi") upcase) ;; => "ABCDEFGHI" 2350 ``` 2351 2352 #### -as-> `(value variable &rest forms)` 2353 2354 Starting with `value`, thread `variable` through `forms`. 2355 2356 In the first form, bind `variable` to `value`. In the second form, bind 2357 `variable` to the result of the first form, and so forth. 2358 2359 ```el 2360 (-as-> 3 my-var (1+ my-var) (list my-var) (mapcar (lambda (ele) (* 2 ele)) my-var)) ;; => (8) 2361 (-as-> 3 my-var 1+) ;; => 4 2362 (-as-> 3 my-var) ;; => 3 2363 ``` 2364 2365 #### -some-> `(x &optional form &rest more)` 2366 2367 When expr is non-nil, thread it through the first form (via [`->`](#--x-optional-form-rest-more)), 2368 and when that result is non-nil, through the next form, etc. 2369 2370 ```el 2371 (-some-> '(2 3 5)) ;; => (2 3 5) 2372 (-some-> 5 square) ;; => 25 2373 (-some-> 5 even? square) ;; => nil 2374 ``` 2375 2376 #### -some->> `(x &optional form &rest more)` 2377 2378 When expr is non-nil, thread it through the first form (via [`->>`](#--x-optional-form-rest-more)), 2379 and when that result is non-nil, through the next form, etc. 2380 2381 ```el 2382 (-some->> '(1 2 3) (-map 'square)) ;; => (1 4 9) 2383 (-some->> '(1 3 5) (-last 'even?) (+ 100)) ;; => nil 2384 (-some->> '(2 4 6) (-last 'even?) (+ 100)) ;; => 106 2385 ``` 2386 2387 #### -some--> `(expr &rest forms)` 2388 2389 Thread `expr` through `forms` via [`-->`](#---x-rest-forms), while the result is non-nil. 2390 When `expr` evaluates to non-nil, thread the result through the 2391 first of `forms`, and when that result is non-nil, thread it 2392 through the next form, etc. 2393 2394 ```el 2395 (-some--> "def" (concat "abc" it "ghi")) ;; => "abcdefghi" 2396 (-some--> nil (concat "abc" it "ghi")) ;; => nil 2397 (-some--> '(0 1) (-remove #'natnump it) (append it it) (-map #'1+ it)) ;; => () 2398 ``` 2399 2400 #### -doto `(init &rest forms)` 2401 2402 Evaluate `init` and pass it as argument to `forms` with [`->`](#--x-optional-form-rest-more). 2403 The `result` of evaluating `init` is threaded through each of `forms` 2404 individually using [`->`](#--x-optional-form-rest-more), which see. The return value is `result`, 2405 which `forms` may have modified by side effect. 2406 2407 ```el 2408 (-doto (list 1 2 3) pop pop) ;; => (3) 2409 (-doto (cons 1 2) (setcar 3) (setcdr 4)) ;; => (3 . 4) 2410 (gethash 'k (--doto (make-hash-table) (puthash 'k 'v it))) ;; => v 2411 ``` 2412 2413 ## Binding 2414 2415 Macros that combine `let` and `let*` with destructuring and flow control. 2416 2417 #### -when-let `((var val) &rest body)` 2418 2419 If `val` evaluates to non-nil, bind it to `var` and execute body. 2420 2421 Note: binding is done according to [`-let`](#-let-varlist-rest-body). 2422 2423 ```el 2424 (-when-let (match-index (string-match "d" "abcd")) (+ match-index 2)) ;; => 5 2425 (-when-let ((&plist :foo foo) (list :foo "foo")) foo) ;; => "foo" 2426 (-when-let ((&plist :foo foo) (list :bar "bar")) foo) ;; => nil 2427 ``` 2428 2429 #### -when-let* `(vars-vals &rest body)` 2430 2431 If all `vals` evaluate to true, bind them to their corresponding 2432 `vars` and execute body. `vars-vals` should be a list of (`var` `val`) 2433 pairs. 2434 2435 Note: binding is done according to [`-let*`](#-let-varlist-rest-body). `vals` are evaluated 2436 sequentially, and evaluation stops after the first nil `val` is 2437 encountered. 2438 2439 ```el 2440 (-when-let* ((x 5) (y 3) (z (+ y 4))) (+ x y z)) ;; => 15 2441 (-when-let* ((x 5) (y nil) (z 7)) (+ x y z)) ;; => nil 2442 ``` 2443 2444 #### -if-let `((var val) then &rest else)` 2445 2446 If `val` evaluates to non-nil, bind it to `var` and do `then`, 2447 otherwise do `else`. 2448 2449 Note: binding is done according to [`-let`](#-let-varlist-rest-body). 2450 2451 ```el 2452 (-if-let (match-index (string-match "d" "abc")) (+ match-index 3) 7) ;; => 7 2453 (--if-let (even? 4) it nil) ;; => t 2454 ``` 2455 2456 #### -if-let* `(vars-vals then &rest else)` 2457 2458 If all `vals` evaluate to true, bind them to their corresponding 2459 `vars` and do `then`, otherwise do `else`. `vars-vals` should be a list 2460 of (`var` `val`) pairs. 2461 2462 Note: binding is done according to [`-let*`](#-let-varlist-rest-body). `vals` are evaluated 2463 sequentially, and evaluation stops after the first nil `val` is 2464 encountered. 2465 2466 ```el 2467 (-if-let* ((x 5) (y 3) (z 7)) (+ x y z) "foo") ;; => 15 2468 (-if-let* ((x 5) (y nil) (z 7)) (+ x y z) "foo") ;; => "foo" 2469 (-if-let* (((_ _ x) '(nil nil 7))) x) ;; => 7 2470 ``` 2471 2472 #### -let `(varlist &rest body)` 2473 2474 Bind variables according to `varlist` then eval `body`. 2475 2476 `varlist` is a list of lists of the form (`pattern` `source`). Each 2477 `pattern` is matched against the `source` "structurally". `source` 2478 is only evaluated once for each `pattern`. Each `pattern` is matched 2479 recursively, and can therefore contain sub-patterns which are 2480 matched against corresponding sub-expressions of `source`. 2481 2482 All the SOURCEs are evalled before any symbols are 2483 bound (i.e. "in parallel"). 2484 2485 If `varlist` only contains one (`pattern` `source`) element, you can 2486 optionally specify it using a vector and discarding the 2487 outer-most parens. Thus 2488 2489 (-let ((`pattern` `source`)) ...) 2490 2491 becomes 2492 2493 (-let [`pattern` `source`] ...). 2494 2495 [`-let`](#-let-varlist-rest-body) uses a convention of not binding places (symbols) starting 2496 with _ whenever it's possible. You can use this to skip over 2497 entries you don't care about. However, this is not *always* 2498 possible (as a result of implementation) and these symbols might 2499 get bound to undefined values. 2500 2501 Following is the overview of supported patterns. Remember that 2502 patterns can be matched recursively, so every a, b, aK in the 2503 following can be a matching construct and not necessarily a 2504 symbol/variable. 2505 2506 Symbol: 2507 2508 a - bind the `source` to `a`. This is just like regular `let`. 2509 2510 Conses and lists: 2511 2512 (a) - bind `car` of cons/list to `a` 2513 2514 (a . b) - bind car of cons to `a` and `cdr` to `b` 2515 2516 (a b) - bind car of list to `a` and `cadr` to `b` 2517 2518 (a1 a2 a3 ...) - bind 0th car of list to `a1`, 1st to `a2`, 2nd to `a3`... 2519 2520 (a1 a2 a3 ... aN . rest) - as above, but bind the `n`th cdr to `rest`. 2521 2522 Vectors: 2523 2524 [a] - bind 0th element of a non-list sequence to `a` (works with 2525 vectors, strings, bit arrays...) 2526 2527 [a1 a2 a3 ...] - bind 0th element of non-list sequence to `a0`, 1st to 2528 `a1`, 2nd to `a2`, ... 2529 If the `pattern` is shorter than `source`, the values at 2530 places not in `pattern` are ignored. 2531 If the `pattern` is longer than `source`, an `error` is 2532 thrown. 2533 2534 [a1 a2 a3 ... &rest rest] - as above, but bind the rest of 2535 the sequence to `rest`. This is 2536 conceptually the same as improper list 2537 matching (a1 a2 ... aN . rest) 2538 2539 Key/value stores: 2540 2541 (&plist key0 a0 ... keyN aN) - bind value mapped by keyK in the 2542 `source` plist to aK. If the 2543 value is not found, aK is nil. 2544 Uses `plist-get` to fetch values. 2545 2546 (&alist key0 a0 ... keyN aN) - bind value mapped by keyK in the 2547 `source` alist to aK. If the 2548 value is not found, aK is nil. 2549 Uses `assoc` to fetch values. 2550 2551 (&hash key0 a0 ... keyN aN) - bind value mapped by keyK in the 2552 `source` hash table to aK. If the 2553 value is not found, aK is nil. 2554 Uses `gethash` to fetch values. 2555 2556 Further, special keyword &keys supports "inline" matching of 2557 plist-like key-value pairs, similarly to &keys keyword of 2558 `cl-defun`. 2559 2560 (a1 a2 ... aN &keys key1 b1 ... keyN bK) 2561 2562 This binds `n` values from the list to a1 ... aN, then interprets 2563 the cdr as a plist (see key/value matching above). 2564 2565 `a` shorthand notation for kv-destructuring exists which allows the 2566 patterns be optionally left out and derived from the key name in 2567 the following fashion: 2568 2569 - a key :foo is converted into `foo` pattern, 2570 - a key 'bar is converted into `bar` pattern, 2571 - a key "baz" is converted into `baz` pattern. 2572 2573 That is, the entire value under the key is bound to the derived 2574 variable without any further destructuring. 2575 2576 This is possible only when the form following the key is not a 2577 valid pattern (i.e. not a symbol, a cons cell or a vector). 2578 Otherwise the matching proceeds as usual and in case of an 2579 invalid spec fails with an error. 2580 2581 Thus the patterns are normalized as follows: 2582 2583 ;; derive all the missing patterns 2584 (&plist :foo 'bar "baz") => (&plist :foo foo 'bar bar "baz" baz) 2585 2586 ;; we can specify some but not others 2587 (&plist :foo 'bar explicit-bar) => (&plist :foo foo 'bar explicit-bar) 2588 2589 ;; nothing happens, we store :foo in x 2590 (&plist :foo x) => (&plist :foo x) 2591 2592 ;; nothing happens, we match recursively 2593 (&plist :foo (a b c)) => (&plist :foo (a b c)) 2594 2595 You can name the source using the syntax `symbol` &as `pattern`. 2596 This syntax works with lists (proper or improper), vectors and 2597 all types of maps. 2598 2599 (list &as a b c) (list 1 2 3) 2600 2601 binds `a` to 1, `b` to 2, `c` to 3 and `list` to (1 2 3). 2602 2603 Similarly: 2604 2605 (bounds &as beg . end) (cons 1 2) 2606 2607 binds `beg` to 1, `end` to 2 and `bounds` to (1 . 2). 2608 2609 (items &as first . rest) (list 1 2 3) 2610 2611 binds `first` to 1, `rest` to (2 3) and `items` to (1 2 3) 2612 2613 [vect &as _ b c] [1 2 3] 2614 2615 binds `b` to 2, `c` to 3 and `vect` to [1 2 3] (_ avoids binding as usual). 2616 2617 (plist &as &plist :b b) (list :a 1 :b 2 :c 3) 2618 2619 binds `b` to 2 and `plist` to (:a 1 :b 2 :c 3). Same for &alist and &hash. 2620 2621 This is especially useful when we want to capture the result of a 2622 computation and destructure at the same time. Consider the 2623 form (function-returning-complex-structure) returning a list of 2624 two vectors with two items each. We want to capture this entire 2625 result and pass it to another computation, but at the same time 2626 we want to get the second item from each vector. We can achieve 2627 it with pattern 2628 2629 (result &as [_ a] [_ b]) (function-returning-complex-structure) 2630 2631 Note: Clojure programmers may know this feature as the ":as 2632 binding". The difference is that we put the &as at the front 2633 because we need to support improper list binding. 2634 2635 ```el 2636 (-let (([a (b c) d] [1 (2 3) 4])) (list a b c d)) ;; => (1 2 3 4) 2637 (-let [(a b c . d) (list 1 2 3 4 5 6)] (list a b c d)) ;; => (1 2 3 (4 5 6)) 2638 (-let [(&plist :foo foo :bar bar) (list :baz 3 :foo 1 :qux 4 :bar 2)] (list foo bar)) ;; => (1 2) 2639 ``` 2640 2641 #### -let* `(varlist &rest body)` 2642 2643 Bind variables according to `varlist` then eval `body`. 2644 2645 `varlist` is a list of lists of the form (`pattern` `source`). Each 2646 `pattern` is matched against the `source` structurally. `source` is 2647 only evaluated once for each `pattern`. 2648 2649 Each `source` can refer to the symbols already bound by this 2650 `varlist`. This is useful if you want to destructure `source` 2651 recursively but also want to name the intermediate structures. 2652 2653 See [`-let`](#-let-varlist-rest-body) for the list of all possible patterns. 2654 2655 ```el 2656 (-let* (((a . b) (cons 1 2)) ((c . d) (cons 3 4))) (list a b c d)) ;; => (1 2 3 4) 2657 (-let* (((a . b) (cons 1 (cons 2 3))) ((c . d) b)) (list a b c d)) ;; => (1 (2 . 3) 2 3) 2658 (-let* (((&alist "foo" foo "bar" bar) (list (cons "foo" 1) (cons "bar" (list 'a 'b 'c)))) ((a b c) bar)) (list foo a b c bar)) ;; => (1 a b c (a b c)) 2659 ``` 2660 2661 #### -lambda `(match-form &rest body)` 2662 2663 Return a lambda which destructures its input as `match-form` and executes `body`. 2664 2665 Note that you have to enclose the `match-form` in a pair of parens, 2666 such that: 2667 2668 (-lambda (x) body) 2669 (-lambda (x y ...) body) 2670 2671 has the usual semantics of `lambda`. Furthermore, these get 2672 translated into normal `lambda`, so there is no performance 2673 penalty. 2674 2675 See [`-let`](#-let-varlist-rest-body) for a description of the destructuring mechanism. 2676 2677 ```el 2678 (-map (-lambda ((x y)) (+ x y)) '((1 2) (3 4) (5 6))) ;; => (3 7 11) 2679 (-map (-lambda ([x y]) (+ x y)) '([1 2] [3 4] [5 6])) ;; => (3 7 11) 2680 (funcall (-lambda ((_ . a) (_ . b)) (-concat a b)) '(1 2 3) '(4 5 6)) ;; => (2 3 5 6) 2681 ``` 2682 2683 #### -setq `([match-form val] ...)` 2684 2685 Bind each `match-form` to the value of its `val`. 2686 2687 `match-form` destructuring is done according to the rules of [`-let`](#-let-varlist-rest-body). 2688 2689 This macro allows you to bind multiple variables by destructuring 2690 the value, so for example: 2691 2692 (-setq (a b) x 2693 (&plist :c c) plist) 2694 2695 expands roughly speaking to the following code 2696 2697 (setq a (car x) 2698 b (cadr x) 2699 c (plist-get plist :c)) 2700 2701 Care is taken to only evaluate each `val` once so that in case of 2702 multiple assignments it does not cause unexpected side effects. 2703 2704 ```el 2705 (let (a) (-setq a 1) a) ;; => 1 2706 (let (a b) (-setq (a b) (list 1 2)) (list a b)) ;; => (1 2) 2707 (let (c) (-setq (&plist :c c) (list :c "c")) c) ;; => "c" 2708 ``` 2709 2710 ## Side effects 2711 2712 Functions iterating over lists for side effect only. 2713 2714 #### -each `(list fn)` 2715 2716 Call `fn` on each element of `list`. 2717 Return nil; this function is intended for side effects. 2718 2719 Its anaphoric counterpart is `--each`. 2720 2721 For access to the current element's index in `list`, see 2722 [`-each-indexed`](#-each-indexed-list-fn). 2723 2724 ```el 2725 (let (l) (-each '(1 2 3) (lambda (x) (push x l))) l) ;; => (3 2 1) 2726 (let (l) (--each '(1 2 3) (push it l)) l) ;; => (3 2 1) 2727 (-each '(1 2 3) #'identity) ;; => nil 2728 ``` 2729 2730 #### -each-while `(list pred fn)` 2731 2732 Call `fn` on each `item` in `list`, while (`pred` `item`) is non-nil. 2733 Once an `item` is reached for which `pred` returns nil, `fn` is no 2734 longer called. Return nil; this function is intended for side 2735 effects. 2736 2737 Its anaphoric counterpart is `--each-while`. 2738 2739 ```el 2740 (let (l) (-each-while '(2 4 5 6) #'even? (lambda (x) (push x l))) l) ;; => (4 2) 2741 (let (l) (--each-while '(1 2 3 4) (< it 3) (push it l)) l) ;; => (2 1) 2742 (let ((s 0)) (--each-while '(1 3 4 5) (< it 5) (setq s (+ s it))) s) ;; => 8 2743 ``` 2744 2745 #### -each-indexed `(list fn)` 2746 2747 Call `fn` on each index and element of `list`. 2748 For each `item` at `index` in `list`, call (funcall `fn` `index` `item`). 2749 Return nil; this function is intended for side effects. 2750 2751 See also: [`-map-indexed`](#-map-indexed-fn-list). 2752 2753 ```el 2754 (let (l) (-each-indexed '(a b c) (lambda (i x) (push (list x i) l))) l) ;; => ((c 2) (b 1) (a 0)) 2755 (let (l) (--each-indexed '(a b c) (push (list it it-index) l)) l) ;; => ((c 2) (b 1) (a 0)) 2756 (let (l) (--each-indexed () (push it l)) l) ;; => () 2757 ``` 2758 2759 #### -each-r `(list fn)` 2760 2761 Call `fn` on each element of `list` in reversed order. 2762 Return nil; this function is intended for side effects. 2763 2764 Its anaphoric counterpart is `--each-r`. 2765 2766 ```el 2767 (let (l) (-each-r '(1 2 3) (lambda (x) (push x l))) l) ;; => (1 2 3) 2768 (let (l) (--each-r '(1 2 3) (push it l)) l) ;; => (1 2 3) 2769 (-each-r '(1 2 3) #'identity) ;; => nil 2770 ``` 2771 2772 #### -each-r-while `(list pred fn)` 2773 2774 Call `fn` on each `item` in reversed `list`, while (`pred` `item`) is non-nil. 2775 Once an `item` is reached for which `pred` returns nil, `fn` is no 2776 longer called. Return nil; this function is intended for side 2777 effects. 2778 2779 Its anaphoric counterpart is `--each-r-while`. 2780 2781 ```el 2782 (let (l) (-each-r-while '(2 4 5 6) #'even? (lambda (x) (push x l))) l) ;; => (6) 2783 (let (l) (--each-r-while '(1 2 3 4) (>= it 3) (push it l)) l) ;; => (3 4) 2784 (let ((s 0)) (--each-r-while '(1 2 3 5) (> it 1) (setq s (+ s it))) s) ;; => 10 2785 ``` 2786 2787 #### -dotimes `(num fn)` 2788 2789 Call `fn` `num` times, presumably for side effects. 2790 `fn` is called with a single argument on successive integers 2791 running from 0, inclusive, to `num`, exclusive. `fn` is not called 2792 if `num` is less than 1. 2793 2794 This function's anaphoric counterpart is `--dotimes`. 2795 2796 ```el 2797 (let (s) (-dotimes 3 (lambda (n) (push n s))) s) ;; => (2 1 0) 2798 (let (s) (-dotimes 0 (lambda (n) (push n s))) s) ;; => () 2799 (let (s) (--dotimes 5 (push it s)) s) ;; => (4 3 2 1 0) 2800 ``` 2801 2802 ## Destructive operations 2803 2804 Macros that modify variables holding lists. 2805 2806 #### !cons `(car cdr)` 2807 2808 Destructive: Set `cdr` to the cons of `car` and `cdr`. 2809 2810 ```el 2811 (let (l) (!cons 5 l) l) ;; => (5) 2812 (let ((l '(3))) (!cons 5 l) l) ;; => (5 3) 2813 ``` 2814 2815 #### !cdr `(list)` 2816 2817 Destructive: Set `list` to the cdr of `list`. 2818 2819 ```el 2820 (let ((l '(3))) (!cdr l) l) ;; => () 2821 (let ((l '(3 5))) (!cdr l) l) ;; => (5) 2822 ``` 2823 2824 ## Function combinators 2825 2826 Functions that manipulate and compose other functions. 2827 2828 #### -partial `(fun &rest args)` 2829 2830 Return a function that is a partial application of `fun` to `args`. 2831 `args` is a list of the first `n` arguments to pass to `fun`. 2832 The result is a new function which does the same as `fun`, except that 2833 the first `n` arguments are fixed at the values with which this function 2834 was called. 2835 2836 ```el 2837 (funcall (-partial #'+ 5)) ;; => 5 2838 (funcall (-partial #'- 5) 3) ;; => 2 2839 (funcall (-partial #'+ 5 2) 3) ;; => 10 2840 ``` 2841 2842 #### -rpartial `(fn &rest args)` 2843 2844 Return a function that is a partial application of `fn` to `args`. 2845 `args` is a list of the last `n` arguments to pass to `fn`. The result 2846 is a new function which does the same as `fn`, except that the last 2847 `n` arguments are fixed at the values with which this function was 2848 called. This is like [`-partial`](#-partial-fun-rest-args), except the arguments are fixed 2849 starting from the right rather than the left. 2850 2851 ```el 2852 (funcall (-rpartial #'- 5)) ;; => -5 2853 (funcall (-rpartial #'- 5) 8) ;; => 3 2854 (funcall (-rpartial #'- 5 2) 10) ;; => 3 2855 ``` 2856 2857 #### -juxt `(&rest fns)` 2858 2859 Return a function that is the juxtaposition of `fns`. 2860 The returned function takes a variable number of `args`, applies 2861 each of `fns` in turn to `args`, and returns the list of results. 2862 2863 ```el 2864 (funcall (-juxt) 1 2) ;; => () 2865 (funcall (-juxt #'+ #'- #'* #'/) 7 5) ;; => (12 2 35 1) 2866 (mapcar (-juxt #'number-to-string #'1+) '(1 2)) ;; => (("1" 2) ("2" 3)) 2867 ``` 2868 2869 #### -compose `(&rest fns)` 2870 2871 Compose `fns` into a single composite function. 2872 Return a function that takes a variable number of `args`, applies 2873 the last function in `fns` to `args`, and returns the result of 2874 calling each remaining function on the result of the previous 2875 function, right-to-left. If no `fns` are given, return a variadic 2876 `identity` function. 2877 2878 ```el 2879 (funcall (-compose #'- #'1+ #'+) 1 2 3) ;; => -7 2880 (funcall (-compose #'identity #'1+) 3) ;; => 4 2881 (mapcar (-compose #'not #'stringp) '(nil "")) ;; => (t nil) 2882 ``` 2883 2884 #### -applify `(fn)` 2885 2886 Return a function that applies `fn` to a single list of args. 2887 This changes the arity of `fn` from taking `n` distinct arguments to 2888 taking 1 argument which is a list of `n` arguments. 2889 2890 ```el 2891 (funcall (-applify #'+) nil) ;; => 0 2892 (mapcar (-applify #'+) '((1 1 1) (1 2 3) (5 5 5))) ;; => (3 6 15) 2893 (funcall (-applify #'<) '(3 6)) ;; => t 2894 ``` 2895 2896 #### -on `(op trans)` 2897 2898 Return a function that calls `trans` on each arg and `op` on the results. 2899 The returned function takes a variable number of arguments, calls 2900 the function `trans` on each one in turn, and then passes those 2901 results as the list of arguments to `op`, in the same order. 2902 2903 For example, the following pairs of expressions are morally 2904 equivalent: 2905 2906 (funcall (-on #'+ #'1+) 1 2 3) = (+ (1+ 1) (1+ 2) (1+ 3)) 2907 (funcall (-on #'+ #'1+)) = (+) 2908 2909 ```el 2910 (-sort (-on #'< #'length) '((1 2 3) (1) (1 2))) ;; => ((1) (1 2) (1 2 3)) 2911 (funcall (-on #'min #'string-to-number) "22" "2" "1" "12") ;; => 1 2912 (-min-by (-on #'> #'length) '((1 2 3) (4) (1 2))) ;; => (4) 2913 ``` 2914 2915 #### -flip `(fn)` 2916 2917 Return a function that calls `fn` with its arguments reversed. 2918 The returned function takes the same number of arguments as `fn`. 2919 2920 For example, the following two expressions are morally 2921 equivalent: 2922 2923 (funcall (-flip #'-) 1 2) = (- 2 1) 2924 2925 See also: [`-rotate-args`](#-rotate-args-n-fn). 2926 2927 ```el 2928 (-sort (-flip #'<) '(4 3 6 1)) ;; => (6 4 3 1) 2929 (funcall (-flip #'-) 3 2 1 10) ;; => 4 2930 (funcall (-flip #'1+) 1) ;; => 2 2931 ``` 2932 2933 #### -rotate-args `(n fn)` 2934 2935 Return a function that calls `fn` with args rotated `n` places to the right. 2936 The returned function takes the same number of arguments as `fn`, 2937 rotates the list of arguments `n` places to the right (left if `n` is 2938 negative) just like [`-rotate`](#-rotate-n-list), and applies `fn` to the result. 2939 2940 See also: [`-flip`](#-flip-fn). 2941 2942 ```el 2943 (funcall (-rotate-args -1 #'list) 1 2 3 4) ;; => (2 3 4 1) 2944 (funcall (-rotate-args 1 #'-) 1 10 100) ;; => 89 2945 (funcall (-rotate-args 2 #'list) 3 4 5 1 2) ;; => (1 2 3 4 5) 2946 ``` 2947 2948 #### -const `(c)` 2949 2950 Return a function that returns `c` ignoring any additional arguments. 2951 2952 In types: a -> b -> a 2953 2954 ```el 2955 (funcall (-const 2) 1 3 "foo") ;; => 2 2956 (mapcar (-const 1) '("a" "b" "c" "d")) ;; => (1 1 1 1) 2957 (-sum (mapcar (-const 1) '("a" "b" "c" "d"))) ;; => 4 2958 ``` 2959 2960 #### -cut `(&rest params)` 2961 2962 Take n-ary function and n arguments and specialize some of them. 2963 Arguments denoted by <> will be left unspecialized. 2964 2965 See `srfi-26` for detailed description. 2966 2967 ```el 2968 (funcall (-cut list 1 <> 3 <> 5) 2 4) ;; => (1 2 3 4 5) 2969 (-map (-cut funcall <> 5) `(1+ 1- ,(lambda (x) (/ 1.0 x)))) ;; => (6 4 0.2) 2970 (-map (-cut <> 1 2 3) '(list vector string)) ;; => ((1 2 3) [1 2 3] "\1\2\3") 2971 ``` 2972 2973 #### -not `(pred)` 2974 2975 Return a predicate that negates the result of `pred`. 2976 The returned predicate passes its arguments to `pred`. If `pred` 2977 returns nil, the result is non-nil; otherwise the result is nil. 2978 2979 See also: [`-andfn`](#-andfn-rest-preds) and [`-orfn`](#-orfn-rest-preds). 2980 2981 ```el 2982 (funcall (-not #'numberp) "5") ;; => t 2983 (-sort (-not #'<) '(5 2 1 0 6)) ;; => (6 5 2 1 0) 2984 (-filter (-not (-partial #'< 4)) '(1 2 3 4 5 6 7 8)) ;; => (1 2 3 4) 2985 ``` 2986 2987 #### -orfn `(&rest preds)` 2988 2989 Return a predicate that returns the first non-nil result of `preds`. 2990 The returned predicate takes a variable number of arguments, 2991 passes them to each predicate in `preds` in turn until one of them 2992 returns non-nil, and returns that non-nil result without calling 2993 the remaining `preds`. If all `preds` return nil, or if no `preds` are 2994 given, the returned predicate returns nil. 2995 2996 See also: [`-andfn`](#-andfn-rest-preds) and [`-not`](#-not-pred). 2997 2998 ```el 2999 (-filter (-orfn #'natnump #'booleanp) '(1 nil "a" -4 b c t)) ;; => (1 nil t) 3000 (funcall (-orfn #'symbolp (-cut string-match-p "x" <>)) "axe") ;; => 1 3001 (funcall (-orfn #'= #'+) 1 1) ;; => t 3002 ``` 3003 3004 #### -andfn `(&rest preds)` 3005 3006 Return a predicate that returns non-nil if all `preds` do so. 3007 The returned predicate `p` takes a variable number of arguments and 3008 passes them to each predicate in `preds` in turn. If any one of 3009 `preds` returns nil, `p` also returns nil without calling the 3010 remaining `preds`. If all `preds` return non-nil, `p` returns the last 3011 such value. If no `preds` are given, `p` always returns non-nil. 3012 3013 See also: [`-orfn`](#-orfn-rest-preds) and [`-not`](#-not-pred). 3014 3015 ```el 3016 (-filter (-andfn #'numberp (-cut < <> 5)) '(a 1 b 6 c 2)) ;; => (1 2) 3017 (mapcar (-andfn #'numberp #'1+) '(a 1 b 6)) ;; => (nil 2 nil 7) 3018 (funcall (-andfn #'= #'+) 1 1) ;; => 2 3019 ``` 3020 3021 #### -iteratefn `(fn n)` 3022 3023 Return a function `fn` composed `n` times with itself. 3024 3025 `fn` is a unary function. If you need to use a function of higher 3026 arity, use [`-applify`](#-applify-fn) first to turn it into a unary function. 3027 3028 With n = 0, this acts as identity function. 3029 3030 In types: (a -> a) -> Int -> a -> a. 3031 3032 This function satisfies the following law: 3033 3034 (funcall (-iteratefn fn n) init) = (-last-item (-iterate fn init (1+ n))). 3035 3036 ```el 3037 (funcall (-iteratefn (lambda (x) (* x x)) 3) 2) ;; => 256 3038 (funcall (-iteratefn '1+ 3) 1) ;; => 4 3039 (funcall (-iteratefn 'cdr 3) '(1 2 3 4 5)) ;; => (4 5) 3040 ``` 3041 3042 #### -fixfn `(fn &optional equal-test halt-test)` 3043 3044 Return a function that computes the (least) fixpoint of `fn`. 3045 3046 `fn` must be a unary function. The returned lambda takes a single 3047 argument, `x`, the initial value for the fixpoint iteration. The 3048 iteration halts when either of the following conditions is satisfied: 3049 3050 1. Iteration converges to the fixpoint, with equality being 3051 tested using `equal-test`. If `equal-test` is not specified, 3052 `equal` is used. For functions over the floating point 3053 numbers, it may be necessary to provide an appropriate 3054 approximate comparison test. 3055 3056 2. `halt-test` returns a non-nil value. `halt-test` defaults to a 3057 simple counter that returns t after `-fixfn-max-iterations`, 3058 to guard against infinite iteration. Otherwise, `halt-test` 3059 must be a function that accepts a single argument, the 3060 current value of `x`, and returns non-nil as long as iteration 3061 should continue. In this way, a more sophisticated 3062 convergence test may be supplied by the caller. 3063 3064 The return value of the lambda is either the fixpoint or, if 3065 iteration halted before converging, a cons with car `halted` and 3066 cdr the final output from `halt-test`. 3067 3068 In types: (a -> a) -> a -> a. 3069 3070 ```el 3071 (funcall (-fixfn #'cos #'approx=) 0.7) ;; ~> 0.7390851332151607 3072 (funcall (-fixfn (lambda (x) (expt (+ x 10) 0.25))) 2.0) ;; => 1.8555845286409378 3073 (funcall (-fixfn #'sin #'approx=) 0.1) ;; => (halted . t) 3074 ``` 3075 3076 #### -prodfn `(&rest fns)` 3077 3078 Take a list of n functions and return a function that takes a 3079 list of length n, applying i-th function to i-th element of the 3080 input list. Returns a list of length n. 3081 3082 In types (for n=2): ((a -> b), (c -> d)) -> (a, c) -> (b, d) 3083 3084 This function satisfies the following laws: 3085 3086 (-compose (-prodfn f g ...) (-prodfn f' g' ...)) = (-prodfn (-compose f f') (-compose g g') ...) 3087 (-prodfn f g ...) = (-juxt (-compose f (-partial 'nth 0)) (-compose g (-partial 'nth 1)) ...) 3088 (-compose (-prodfn f g ...) (-juxt f' g' ...)) = (-juxt (-compose f f') (-compose g g') ...) 3089 (-compose (-partial 'nth n) (-prod f1 f2 ...)) = (-compose fn (-partial 'nth n)) 3090 3091 ```el 3092 (funcall (-prodfn '1+ '1- 'number-to-string) '(1 2 3)) ;; => (2 1 "3") 3093 (-map (-prodfn '1+ '1-) '((1 2) (3 4) (5 6) (7 8))) ;; => ((2 1) (4 3) (6 5) (8 7)) 3094 (apply '+ (funcall (-prodfn 'length 'string-to-number) '((1 2 3) "15"))) ;; => 18 3095 ``` 3096 3097 ## Contribute 3098 3099 Yes, please do. Pure functions in the list manipulation realm only, 3100 please. There's a suite of examples/tests in `dev/examples.el`, so 3101 remember to add tests for your additions, or I might break them later. 3102 3103 You'll find the repo at: 3104 3105 https://github.com/magnars/dash.el 3106 3107 Run the tests with: 3108 3109 make check 3110 3111 Regenerate the docs with: 3112 3113 make docs 3114 3115 I highly recommend that you install these as a pre-commit hook, so 3116 that the tests are always running and the docs are always in sync: 3117 3118 cp dev/pre-commit.sh .git/hooks/pre-commit 3119 3120 Oh, and don't edit `README.md` or `dash.texi` directly; they are 3121 auto-generated. Change `readme-template.md` or `dash-template.texi` 3122 instead, respectively. 3123 3124 To ensure that `dash.el` can be distributed with GNU ELPA or Emacs, we 3125 require that all contributors assign copyright to the Free Software 3126 Foundation. For more on this, see [`(info "(emacs) Copyright 3127 Assignment")`](https://gnu.org/software/emacs/manual/html_node/emacs/Copyright-Assignment.html). 3128 3129 ## Contributors 3130 3131 - [Matus Goljer](https://github.com/Fuco1) contributed lots of features and 3132 functions. 3133 - [Takafumi Arakaki](https://github.com/tkf) contributed `-group-by`. 3134 - [tali713](https://github.com/tali713) is the author of `-applify`. 3135 - [VĂctor M. Valenzuela](https://github.com/vemv) contributed `-repeat`. 3136 - [Nic Ferrier](https://github.com/nicferrier) contributed `-cons*`. 3137 - [Wilfred Hughes](https://github.com/Wilfred) contributed `-slice`, 3138 `-first-item`, and `-last-item`. 3139 - [Emanuel Evans](https://github.com/shosti) contributed `-if-let`, `-when-let`, 3140 and `-insert-at`. 3141 - [Johan Andersson](https://github.com/rejeep) contributed `-sum`, `-product`, 3142 and `-same-items?`. 3143 - [Christina Whyte](https://github.com/kurisuwhyte) contributed `-compose`. 3144 - [Steve Lamb](https://github.com/steventlamb) contributed `-cycle`, `-pad`, 3145 `-annotate`, `-zip-fill`, and a variadic version of `-zip`. 3146 - [Fredrik Bergroth](https://github.com/fbergroth) made the `-if-let` family use 3147 `-let` destructuring and improved the script for generating documentation. 3148 - [Mark Oteiza](https://github.com/holomorph) contributed `-iota` and 3149 the script to create an Info manual. 3150 - [Vasilij Schneidermann](https://github.com/wasamasa) contributed `-some`. 3151 - [William West](https://github.com/occidens) made `-fixfn` more robust at 3152 handling floats. 3153 - [Cam Saul](https://github.com/camsaul) contributed `-some->`, `-some->>`, and 3154 `-some-->`. 3155 - [Basil L. Contovounesios](https://github.com/basil-conto) contributed 3156 `-common-prefix`, `-common-suffix`, and various other improvements. 3157 - [Paul Pogonyshev](https://github.com/doublep) contributed `-each-r` and 3158 `-each-r-while`. 3159 3160 Thanks! 3161 3162 New contributors are very welcome. See the 3163 [`Contribute`](#contribute) section above. 3164 3165 ## License 3166 3167 Copyright (C) 2012-2021 Free Software Foundation, Inc. 3168 3169 Author: Magnar Sveen <magnars@gmail.com> 3170 3171 This program is free software: you can redistribute it and/or modify 3172 it under the terms of the GNU General Public License as published by 3173 the Free Software Foundation, either version 3 of the License, or 3174 (at your option) any later version. 3175 3176 This program is distributed in the hope that it will be useful, 3177 but WITHOUT ANY WARRANTY; without even the implied warranty of 3178 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3179 GNU General Public License for more details. 3180 3181 You should have received a copy of the GNU General Public License 3182 along with this program. If not, see <https://www.gnu.org/licenses/>.