• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H26-Aug-2021-3935

dev/H26-Aug-2021-2,3101,972

doc/H26-Aug-2021-1,2241,013

.dir-locals.elH A D26-Aug-2021423 1514

.elpaignoreH A D26-Aug-20216 21

.gitignoreH A D26-Aug-2021177 1611

CaskH A D26-Aug-202125 21

LICENSEH A D26-Aug-202134.3 KiB675553

MakefileH A D26-Aug-20212 KiB7335

NEWS.mdH A D26-Aug-202110 KiB303218

README.mdH A D26-Aug-2021101.6 KiB3,1832,259

dash-functional.elH A D26-Aug-20212 KiB547

dash-template.texiH A D26-Aug-20218.8 KiB318256

dash.elH A D26-Aug-2021121.5 KiB3,5322,807

dash.texiH A D26-Aug-2021116.2 KiB4,7864,084

readme-template.mdH A D26-Aug-20217.6 KiB212154

README.md

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
9A modern list API for Emacs.  No
10[`'cl`](https://gnu.org/software/emacs/manual/html_node/cl/) required.
11
12See 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
26See 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
42Dash is available on [GNU ELPA](https://elpa.gnu.org/), [GNU-devel
43ELPA](https://elpa.gnu.org/devel/), and [MELPA](https://melpa.org/),
44and can be installed with the standard command `package-install`:
45
46    M-x package-install RET dash RET
47
48See [`(info "(emacs) Package
49Installation")`](https://gnu.org/software/emacs/manual/html_node/emacs/Package-Installation.html).
50
51Alternatively, you can just dump `dash.el` in your `load-path`
52somewhere.  See [`(info "(emacs) Lisp
53Libraries")`](https://gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html).
54
55### Using in a package
56
57Add something like this to the library's headers:
58
59    ;; Package-Requires: ((dash "2.19.1"))
60
61See [`(info "(elisp) Library
62Headers")`](https://gnu.org/software/emacs/manual/html_node/elisp/Library-Headers.html).
63
64### Fontification of special variables
65
66Font lock of special Dash variables (`it`, `acc`, etc.) in Emacs Lisp
67buffers can optionally be enabled with the autoloaded minor mode
68`dash-fontify-mode`.  In older Emacs versions which do not dynamically
69detect macros, the minor mode also fontifies Dash macro calls.
70
71To automatically enable the minor mode in all Emacs Lisp buffers, just
72call its autoloaded global counterpart `global-dash-fontify-mode`,
73either interactively or from your `user-init-file`:
74
75```el
76(global-dash-fontify-mode)
77```
78
79### Info symbol lookup
80
81While editing Elisp files, you can use `C-h S` (`info-lookup-symbol`)
82to look up Elisp symbols in the relevant Info manuals (see [`(emacs)
83Info
84Lookup`](https://gnu.org/software/emacs/manual/html_node/emacs/Info-Lookup.html)).
85To enable the same for Dash symbols, use the command
86`dash-register-info-lookup`.  It can be called directly when needed,
87or 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
96All functions and constructs in the library use a dash (`-`) prefix.
97
98The library also provides anaphoric macro versions of functions where
99that makes sense.  The names of these macros are prefixed with two
100dashes (`--`) instead of one.
101
102While `-map` applies a function to each element of a list, its
103anaphoric counterpart `--map` evaluates a form with the local variable
104`it` temporarily bound to the current list element instead.  For
105example:
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
112The 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
122This demonstrates the utility of both versions.
123
124### Maps
125
126Functions in this category take a transforming function, which
127is then applied sequentially to each or selected elements of the
128input list.  The results are collected in order and returned as a
129new 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
144Functions 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
165Functions 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
182Functions 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
208Operations dual to reductions, building lists from a seed
209value 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
216Reductions 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
233Functions 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
254Functions retrieving or sorting based on list indices and
255related 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
267Operations 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
278Other 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
311Functions 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
324Macros that conditionally combine sequential forms for brevity
325or 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
338Macros 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
351Functions 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
362Macros that modify variables holding lists.
363
364* [`!cons`](#cons-car-cdr) `(car cdr)`
365* [`!cdr`](#cdr-list) `(list)`
366
367### Function combinators
368
369Functions 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
390Functions in this category take a transforming function, which
391is then applied sequentially to each or selected elements of the
392input list.  The results are collected in order and returned as a
393new list.
394
395#### -map `(fn list)`
396
397Apply `fn` to each item in `list` and return the list of results.
398
399This 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
409Return a new list where the elements in `list` that do not match the `pred` function
410are unchanged, and where the elements in `list` that do match the `pred` function are mapped
411through the `rep` function.
412
413Alias: `-replace-where`
414
415See 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
425Replace first item in `list` satisfying `pred` with result of `rep` called on this item.
426
427See 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
437Replace last item in `list` satisfying `pred` with result of `rep` called on this item.
438
439See 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
449Apply `fn` to each index and item in `list` and return the list of results.
450This is like [`-map`](#-map-fn-list), but `fn` takes two arguments: the index of the
451current element within `list`, and the element itself.
452
453This function's anaphoric counterpart is `--map-indexed`.
454
455For 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
465Return a list of cons cells where each cell is `fn` applied to each
466element 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
476Splice lists generated by `fun` in place of elements matching `pred` in `list`.
477
478`fun` takes the element matching `pred` as input.
479
480This function can be used as replacement for `,@` in case you
481need to splice several lists at marked positions (for example
482with keywords).
483
484See 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
494Splice `new-list` in place of elements matching `pred` in `list`.
495
496See 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
506Return the concatenation of the result of mapping `fn` over `list`.
507Thus 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
517Create 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
526Functions returning a sublist of the original list.
527
528#### -filter `(pred list)`
529
530Return a new list of the items in `list` for which `pred` returns non-nil.
531
532Alias: `-select`.
533
534This function's anaphoric counterpart is `--filter`.
535
536For 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
546Return a new list of the items in `list` for which `pred` returns nil.
547
548Alias: `-reject`.
549
550This function's anaphoric counterpart is `--remove`.
551
552For 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
562Remove the first item from `list` for which `pred` returns non-nil.
563This is a non-destructive operation, but only the front of `list`
564leading up to the removed item is a copy; the rest is `list`'s
565original tail.  If no item is removed, then the result is a
566complete copy.
567
568Alias: `-reject-first`.
569
570This function's anaphoric counterpart is `--remove-first`.
571
572See 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
582Remove the last item from `list` for which `pred` returns non-nil.
583The result is a copy of `list` regardless of whether an element is
584removed.
585
586Alias: `-reject-last`.
587
588This function's anaphoric counterpart is `--remove-last`.
589
590See 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
600Return a copy of `list` with all occurrences of `item` removed.
601The 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
611Return 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
621Return copy of `list`, starting from index `from` to index `to`.
622
623`from` or `to` may be negative.  These values are then interpreted
624modulo the length of the list.
625
626If `step` is a number, only each `step`th item in the resulting
627section 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
637Return a copy of the first `n` items in `list`.
638Return a copy of `list` if it contains `n` items or fewer.
639Return nil if `n` is zero or less.
640
641See 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
651Return a copy of the last `n` items of `list` in order.
652Return a copy of `list` if it contains `n` items or fewer.
653Return nil if `n` is zero or less.
654
655See 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
665Return the tail (not a copy) of `list` without the first `n` items.
666Return nil if `list` contains `n` items or fewer.
667Return `list` if `n` is zero or less.
668
669For 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
679Return a copy of `list` without its last `n` items.
680Return a copy of `list` if `n` is zero or less.
681Return nil if `list` contains `n` items or fewer.
682
683See 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
693Take successive items from `list` for which `pred` returns non-nil.
694`pred` is a function of one argument.  Return a new list of the
695successive elements from the start of `list` for which `pred` returns
696non-nil.
697
698This function's anaphoric counterpart is `--take-while`.
699
700For 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
710Drop successive items from `list` for which `pred` returns non-nil.
711`pred` is a function of one argument.  Return the tail (not a copy)
712of `list` starting from its first element for which `pred` returns
713nil.
714
715This function's anaphoric counterpart is `--drop-while`.
716
717For 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
727Return a list whose elements are elements from `list` selected
728as `(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
738Select `columns` from `table`.
739
740`table` is a list of lists where each element represents one row.
741It is assumed each row has the same length.
742
743Each row is transformed such that only the specified `columns` are
744selected.
745
746See 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
756Select `column` from `table`.
757
758`table` is a list of lists where each element represents one row.
759It is assumed each row has the same length.
760
761The single selected column is returned as a list.
762
763See 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
771Functions returning a modified copy of the input list.
772
773#### -keep `(fn list)`
774
775Return a new list of the non-nil results of applying `fn` to each item in `list`.
776Like [`-filter`](#-filter-pred-list), but returns the non-nil results of `fn` instead of
777the corresponding elements of `list`.
778
779Its 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
789Return 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
799Take a nested list `l` and return its contents as a single, flat list.
800
801Note that because `nil` represents a list of zero elements (an
802empty list), any mention of nil in `l` will disappear after
803flattening.  If you need to preserve nils, consider [`-flatten-n`](#-flatten-n-num-list)
804or map them to some unique symbol and then map them back.
805
806Conses of two atoms are considered "terminals", that is, they
807aren't flattened further.
808
809See 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
819Flatten `num` levels of a nested `list`.
820
821See 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
831Replace all `old` items in `list` with `new`.
832
833Elements are compared using `equal`.
834
835See 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
845Replace the first occurrence of `old` with `new` in `list`.
846
847Elements are compared using `equal`.
848
849See 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
859Replace the last occurrence of `old` with `new` in `list`.
860
861Elements are compared using `equal`.
862
863See 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
873Return a list with `x` inserted into `list` at position `n`.
874
875See 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
884Return a list with element at `n`th position in `list` replaced with `x`.
885
886See 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
896Return a list with element at `n`th position in `list` replaced with `(func (nth n list))`.
897
898See 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
908Return a list with element at `n`th position in `list` removed.
909
910See 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
920Return a list whose elements are elements from `list` without
921elements selected as `(nth i list)` for all i
922from `indices`.
923
924See 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
934Functions reducing lists to a single value (which may also be a list).
935
936#### -reduce-from `(fn init list)`
937
938Reduce the function `fn` across `list`, starting with `init`.
939Return the result of applying `fn` to `init` and the first element of
940`list`, then applying `fn` to that result and the second element,
941etc.  If `list` is empty, return `init` without calling `fn`.
942
943This function's anaphoric counterpart is `--reduce-from`.
944
945For 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
955Reduce the function `fn` across `list` in reverse, starting with `init`.
956Return 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
958previous result of `fn`, etc.  That is, the first argument of `fn` is
959the current element, and its second argument the accumulated
960value.  If `list` is empty, return `init` without calling `fn`.
961
962This function is like [`-reduce-from`](#-reduce-from-fn-init-list) but the operation associates
963from the right rather than left.  In other words, it starts from
964the end of `list` and flips the arguments to `fn`.  Conceptually, it
965is like replacing the conses in `list` with applications of `fn`, and
966its last link with `init`, and evaluating the resulting expression.
967
968This function's anaphoric counterpart is `--reduce-r-from`.
969
970For 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
980Reduce the function `fn` across `list`.
981Return the result of applying `fn` to the first two elements of
982`list`, then applying `fn` to that result and the third element, etc.
983If `list` contains a single element, return it without calling `fn`.
984If `list` is empty, return the result of calling `fn` with no
985arguments.
986
987This function's anaphoric counterpart is `--reduce`.
988
989For 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
999Reduce the function `fn` across `list` in reverse.
1000Return the result of applying `fn` to the last two elements of
1001`list`, then applying `fn` to the third-to-last element and the
1002previous result of `fn`, etc.  That is, the first argument of `fn` is
1003the current element, and its second argument the accumulated
1004value.  If `list` contains a single element, return it without
1005calling `fn`.  If `list` is empty, return the result of calling `fn`
1006with no arguments.
1007
1008This function is like [`-reduce`](#-reduce-fn-list) but the operation associates from
1009the right rather than left.  In other words, it starts from the
1010end of `list` and flips the arguments to `fn`.  Conceptually, it is
1011like replacing the conses in `list` with applications of `fn`,
1012ignoring its last link, and evaluating the resulting expression.
1013
1014This function's anaphoric counterpart is `--reduce-r`.
1015
1016For 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
1026Return a list of `fn`'s intermediate reductions across `list`.
1027That is, a list of the intermediate values of the accumulator
1028when [`-reduce-from`](#-reduce-from-fn-init-list) (which see) is called with the same
1029arguments.
1030
1031This function's anaphoric counterpart is `--reductions-from`.
1032
1033For 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
1043Return a list of `fn`'s intermediate reductions across reversed `list`.
1044That is, a list of the intermediate values of the accumulator
1045when [`-reduce-r-from`](#-reduce-r-from-fn-init-list) (which see) is called with the same
1046arguments.
1047
1048This function's anaphoric counterpart is `--reductions-r-from`.
1049
1050For 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
1060Return a list of `fn`'s intermediate reductions across `list`.
1061That is, a list of the intermediate values of the accumulator
1062when [`-reduce`](#-reduce-fn-list) (which see) is called with the same arguments.
1063
1064This function's anaphoric counterpart is `--reductions`.
1065
1066For 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
1076Return a list of `fn`'s intermediate reductions across reversed `list`.
1077That is, a list of the intermediate values of the accumulator
1078when [`-reduce-r`](#-reduce-r-fn-list) (which see) is called with the same arguments.
1079
1080This function's anaphoric counterpart is `--reductions-r`.
1081
1082For 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
1093Counts 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
1102Return 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
1112Return 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
1123Return 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
1133Return 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
1144Return 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
1154Return 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
1164Return 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
1174Return 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
1184Return 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
1194Take a comparison function `comparator` and a `list` and return
1195the least element of the list by the comparison function.
1196
1197See also combinator [`-on`](#-on-op-trans) which can transform the values before
1198comparing 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
1208Return 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
1218Take a comparison function `comparator` and a `list` and return
1219the greatest element of the list by the comparison function.
1220
1221See also combinator [`-on`](#-on-op-trans) which can transform the values before
1222comparing 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
1232Operations dual to reductions, building lists from a seed
1233value rather than consuming a list to produce a single value.
1234
1235#### -iterate `(fun init n)`
1236
1237Return a list of iterated applications of `fun` to `init`.
1238
1239This 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
1253Build a list from `seed` using `fun`.
1254
1255This is "dual" operation to [`-reduce-r`](#-reduce-r-fn-list): while -reduce-r
1256consumes a list to produce a single value, [`-unfold`](#-unfold-fun-seed) takes a
1257seed value and builds a (potentially infinite!) list.
1258
1259`fun` should return `nil` to stop the generating process, or a
1260cons (`a` . `b`), where `a` will be prepended to the result and `b` is
1261the 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
1271Reductions of one or more lists to a boolean value.
1272
1273#### -some `(pred list)`
1274
1275Return (`pred` x) for the first `list` item where (`pred` x) is non-nil, else nil.
1276
1277Alias: `-any`.
1278
1279This 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
1289Return non-nil if `pred` returns non-nil for all items in `list`.
1290If so, return the last such result of `pred`.  Otherwise, once an
1291item is reached for which `pred` returns nil, return nil without
1292calling `pred` on any further `list` elements.
1293
1294This function is like `-every-p`, but on success returns the last
1295non-nil result of `pred` instead of just t.
1296
1297This 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
1307Return t if (`pred` x) is non-nil for any x in `list`, else nil.
1308
1309Alias: `-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
1319Return t if (`pred` `x`) is non-nil for all `x` in `list`, else nil.
1320In the latter case, stop after the first `x` for which (`pred` `x`) is
1321nil, without calling `pred` on any subsequent elements of `list`.
1322
1323The similar function [`-every`](#-every-pred-list) is more widely useful, since it
1324returns the last non-nil result of `pred` instead of just t on
1325success.
1326
1327Alias: `-all-p`, `-every-p`, `-every?`.
1328
1329This 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
1339Return t if (`pred` x) is nil for all x in `list`, else nil.
1340
1341Alias: `-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
1351Return `t` if at least one item of `list` matches `pred` and at least one item of `list` does not match `pred`.
1352Return `nil` both if all items match the predicate or if none of the items match the predicate.
1353
1354Alias: `-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
1364Return non-nil if `list` contains `element`.
1365
1366The test for equality is done with `equal`, or with `-compare-fn`
1367if that's non-nil.
1368
1369Alias: `-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
1379Return true if `list` and `list2` has the same items.
1380
1381The order of the elements in the lists does not matter.
1382
1383Alias: `-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
1393Return non-nil if `prefix` is a prefix of `list`.
1394
1395Alias: `-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
1405Return non-nil if `suffix` is a suffix of `list`.
1406
1407Alias: `-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
1417Return non-nil if `infix` is infix of `list`.
1418
1419This operation runs in O(n^2) time
1420
1421Alias: `-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
1431Return non-nil if `obj` is a true cons pair.
1432That is, a cons (`a` . `b`) where `b` is not a list.
1433
1434Alias: `-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
1444Functions partitioning the input list into a list of lists.
1445
1446#### -split-at `(n list)`
1447
1448Split `list` into two sublists after the `n`th element.
1449The result is a list of two elements (`take` `drop`) where `take` is a
1450new list of the first `n` elements of `list`, and `drop` is the
1451remaining elements of `list` (not a copy).  `take` and `drop` are like
1452the results of [`-take`](#-take-n-list) and [`-drop`](#-drop-n-list), respectively, but the split
1453is 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
1463Return 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
1473Split the `list` each time `item` is found.
1474
1475Unlike [`-partition-by`](#-partition-by-fn-list), the `item` is discarded from the results.
1476Empty lists are also removed from the result.
1477
1478Comparison is done by `equal`.
1479
1480See 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
1490Split the `list` on each element where `fn` returns non-nil.
1491
1492Unlike [`-partition-by`](#-partition-by-fn-list), the "matched" element is discarded from
1493the results.  Empty lists are also removed from the result.
1494
1495This 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
1506Return 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
1516Return a new list with the items in `list` grouped into `n`-sized sublists.
1517If there are not enough items to make the last group `n`-sized,
1518those 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
1528Return a new list with the items in `list` grouped into `n`-sized sublists.
1529The 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
1539Return a new list with the items in `list` grouped into `n`-sized sublists at offsets `step` apart.
1540If there are not enough items to make the last group `n`-sized,
1541those 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
1551Return a new list with the items in `list` grouped into `n`-sized sublists at offsets `step` apart.
1552The 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
1562Apply `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
1572Apply `fn` to the first item in `list`. That is the header
1573value. Apply `fn` to each item in `list`, splitting it each time `fn`
1574returns the header value, but only after seeing at least one
1575other 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
1585Partition `list` after each element for which `pred` returns non-nil.
1586
1587This 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
1597Partition 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
1607Partition 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
1617Partition 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
1627Separate `list` into an alist whose keys are `fn` applied to the
1628elements 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
1638Functions retrieving or sorting based on list indices and
1639related predicates.
1640
1641#### -elem-index `(elem list)`
1642
1643Return the index of the first element in the given `list` which
1644is equal to the query element `elem`, or nil if there is no
1645such 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
1655Return the indices of all elements in `list` equal to the query
1656element `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
1666Take a predicate `pred` and a `list` and return the index of the
1667first element in the list satisfying the predicate, or nil if
1668there is no such element.
1669
1670See 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
1680Take a predicate `pred` and a `list` and return the index of the
1681last element in the list satisfying the predicate, or nil if
1682there is no such element.
1683
1684See 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
1694Return the indices of all elements in `list` satisfying the
1695predicate `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
1705Grade elements of `list` using `comparator` relation.
1706This yields a permutation vector such that applying this
1707permutation 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
1716Grade elements of `list` using `comparator` relation.
1717This yields a permutation vector such that applying this
1718permutation 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
1727Operations pretending lists are sets.
1728
1729#### -union `(list list2)`
1730
1731Return a new list containing the elements of `list` and elements of `list2` that are not in `list`.
1732The test for equality is done with `equal`,
1733or 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
1743Return a new list with only the members of `list` that are not in `list2`.
1744The test for equality is done with `equal`,
1745or 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
1755Return a new list containing only the elements that are members of both `list` and `list2`.
1756The test for equality is done with `equal`,
1757or 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
1767Return 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
1776Return 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
1786Return a new list with all duplicates removed.
1787The test for equality is done with `equal`,
1788or with `-compare-fn` if that's non-nil.
1789
1790Alias: `-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
1800Other list functions not fit to be classified elsewhere.
1801
1802#### -rotate `(n list)`
1803
1804Rotate `list` `n` places to the right (left if `n` is negative).
1805The 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
1815Return a new list of length `n` with each element being `x`.
1816Return 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
1826Make a new list from the elements of `args`.
1827The last 2 elements of `args` are used as the final cons of the
1828result, so if the final element of `args` is not a list, the result
1829is 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
1839Append `elem` to the end of the list.
1840
1841This is like `cons`, but operates on the end of list.
1842
1843If `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
1853Return 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
1863Return 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
1873Return a list containing `count` numbers.
1874Starts from `start` and adds `step` each time.  The default `start` is
1875zero, the default `step` is 1.
1876This function takes its name from the corresponding primitive in
1877the `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
1887Zip the two lists `list1` and `list2` using a function `fn`.  This
1888function is applied pairwise taking as first argument element of
1889`list1` and as second argument element of `list2` at corresponding
1890position.
1891
1892The anaphoric form `--zip-with` binds the elements from `list1` as symbol `it`,
1893and 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
1903Zip `lists` together.  Group the head of each list, followed by the
1904second elements of each list, and so on. The lengths of the returned
1905groupings are equal to the length of the shortest input list.
1906
1907If two lists are provided as arguments, return the groupings as a list
1908of cons cells. Otherwise, return the groupings as a list of lists.
1909
1910Use [`-zip-lists`](#-zip-lists-rest-lists) if you need the return value to always be a list
1911of lists.
1912
1913Alias: `-zip-pair`
1914
1915See 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
1925Zip `lists` together.  Group the head of each list, followed by the
1926second elements of each list, and so on. The lengths of the returned
1927groupings are equal to the length of the shortest input list.
1928
1929The return value is always list of lists, which is a difference
1930from `-zip-pair` which returns a cons-cell in case two input
1931lists are provided.
1932
1933See 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
1943Zip `lists`, with `fill-value` padded onto the shorter lists. The
1944lengths of the returned groupings are equal to the length of the
1945longest 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
1953Unzip `lists`.
1954
1955This works just like [`-zip`](#-zip-rest-lists) but takes a list of lists instead of
1956a variable number of arguments, such that
1957
1958    (-unzip (-zip `l1` `l2` `l3` ...))
1959
1960is identity (given that the lists are the same length).
1961
1962Note in particular that calling this on a list of two lists will
1963return a list of cons-cells such that the above identity works.
1964
1965See 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
1975Return an infinite circular copy of `list`.
1976The returned list cycles through the elements of `list` and repeats
1977from 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
1987Appends `fill-value` to the end of each list in `lists` such that they
1988will 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
1998Compute outer product of `lists` using function `fn`.
1999
2000The function `fn` should have the same arity as the number of
2001supplied lists.
2002
2003The outer product is computed by applying fn to all possible
2004combinations created by taking one element from each list in
2005order.  The dimension of the result is (length lists).
2006
2007See 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
2017Compute flat outer product of `lists` using function `fn`.
2018
2019The function `fn` should have the same arity as the number of
2020supplied lists.
2021
2022The outer product is computed by applying fn to all possible
2023combinations created by taking one element from each list in
2024order.  The results are flattened, ignoring the tensor structure
2025of the result.  This is equivalent to calling:
2026
2027    (-flatten-n (1- (length lists)) (apply '-table fn lists))
2028
2029but the implementation here is much more efficient.
2030
2031See 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
2041Return the first item in `list` for which `pred` returns non-nil.
2042Return nil if no such element is found.
2043To get the first item in the list no questions asked, use `car`.
2044
2045Alias: `-find`.
2046
2047This 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
2057Return 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
2067Return the first item of `list`, or nil on an empty list.
2068
2069See 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
2079Return the second item of `list`, or nil if `list` is too short.
2080
2081See 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
2090Return the third item of `list`, or nil if `list` is too short.
2091
2092See 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
2101Return the fourth item of `list`, or nil if `list` is too short.
2102
2103See 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
2112Return the fifth item of `list`, or nil if `list` is too short.
2113
2114See 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
2123Return 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
2133Return 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
2143Sort `list`, stably, comparing elements using `comparator`.
2144Return the sorted list.  `list` is `not` modified by side effects.
2145`comparator` is called with two elements of `list`, and should return non-nil
2146if 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
2156Ensure `arg` is a list.
2157If `arg` is already a list, return it as is (not a copy).
2158Otherwise, return a new list with `arg` as its only element.
2159
2160Another supported calling convention is (-list &rest `args`).
2161In 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
2163backward 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
2173Compute 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
2184Functions pretending lists are trees.
2185
2186#### -tree-seq `(branch children tree)`
2187
2188Return 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
2191passed 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
2194of the passed branch node.
2195
2196Non-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
2206Apply `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
2216Call `fun` on each node of `tree` that satisfies `pred`.
2217
2218If `pred` returns nil, continue descending down this node.  If `pred`
2219returns non-nil, apply `fun` to this node and do not descend
2220further.
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
2230Use `fn` to reduce elements of list `tree`.
2231If elements of `tree` are lists themselves, apply the reduction recursively.
2232
2233`fn` is first applied to first element of the list and second
2234element, then on this result and third element from the list etc.
2235
2236See [`-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
2246Use `fn` to reduce elements of list `tree`.
2247If 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,
2250then on this result and second element from the list etc.
2251
2252The initial value is ignored on cons pairs as they always contain
2253two 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
2262Apply `fn` to each element of `tree`, and make a list of the results.
2263If elements of `tree` are lists themselves, apply `fn` recursively to
2264elements of these nested lists.
2265
2266Then reduce the resulting lists using `folder` and initial value
2267`init-value`. See [`-reduce-r-from`](#-reduce-r-from-fn-init-list).
2268
2269This is the same as calling [`-tree-reduce`](#-tree-reduce-fn-tree) after [`-tree-map`](#-tree-map-fn-tree)
2270but 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
2280Apply `fn` to each element of `tree`, and make a list of the results.
2281If elements of `tree` are lists themselves, apply `fn` recursively to
2282elements of these nested lists.
2283
2284Then reduce the resulting lists using `folder` and initial value
2285`init-value`. See [`-reduce-r-from`](#-reduce-r-from-fn-init-list).
2286
2287This is the same as calling [`-tree-reduce-from`](#-tree-reduce-from-fn-init-value-tree) after [`-tree-map`](#-tree-map-fn-tree)
2288but 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
2298Create a deep copy of `list`.
2299The new list has the same elements and structure but all cons are
2300replaced with new ones.  This is useful when you need to clone a
2301structure 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
2309Macros that conditionally combine sequential forms for brevity
2310or readability.
2311
2312#### -> `(x &optional form &rest more)`
2313
2314Thread the expr through the forms. Insert `x` as the second item
2315in the first form, making a list of it if it is not a list
2316already. If there are more forms, insert the first form as the
2317second 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
2327Thread the expr through the forms. Insert `x` as the last item
2328in the first form, making a list of it if it is not a list
2329already. If there are more forms, insert the first form as the
2330last 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
2340Starting with the value of `x`, thread each expression through `forms`.
2341
2342Insert `x` at the position signified by the symbol `it` in the first
2343form.  If there are more forms, insert the first form at the position
2344signified 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
2354Starting with `value`, thread `variable` through `forms`.
2355
2356In 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
2367When expr is non-nil, thread it through the first form (via [`->`](#--x-optional-form-rest-more)),
2368and 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
2378When expr is non-nil, thread it through the first form (via [`->>`](#--x-optional-form-rest-more)),
2379and 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
2389Thread `expr` through `forms` via [`-->`](#---x-rest-forms), while the result is non-nil.
2390When `expr` evaluates to non-nil, thread the result through the
2391first of `forms`, and when that result is non-nil, thread it
2392through 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
2402Evaluate `init` and pass it as argument to `forms` with [`->`](#--x-optional-form-rest-more).
2403The `result` of evaluating `init` is threaded through each of `forms`
2404individually using [`->`](#--x-optional-form-rest-more), which see.  The return value is `result`,
2405which `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
2415Macros that combine `let` and `let*` with destructuring and flow control.
2416
2417#### -when-let `((var val) &rest body)`
2418
2419If `val` evaluates to non-nil, bind it to `var` and execute body.
2420
2421Note: 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
2431If all `vals` evaluate to true, bind them to their corresponding
2432`vars` and execute body. `vars-vals` should be a list of (`var` `val`)
2433pairs.
2434
2435Note: binding is done according to [`-let*`](#-let-varlist-rest-body).  `vals` are evaluated
2436sequentially, and evaluation stops after the first nil `val` is
2437encountered.
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
2446If `val` evaluates to non-nil, bind it to `var` and do `then`,
2447otherwise do `else`.
2448
2449Note: 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
2458If all `vals` evaluate to true, bind them to their corresponding
2459`vars` and do `then`, otherwise do `else`. `vars-vals` should be a list
2460of (`var` `val`) pairs.
2461
2462Note: binding is done according to [`-let*`](#-let-varlist-rest-body).  `vals` are evaluated
2463sequentially, and evaluation stops after the first nil `val` is
2464encountered.
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
2474Bind 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`
2478is only evaluated once for each `pattern`.  Each `pattern` is matched
2479recursively, and can therefore contain sub-patterns which are
2480matched against corresponding sub-expressions of `source`.
2481
2482All the SOURCEs are evalled before any symbols are
2483bound (i.e. "in parallel").
2484
2485If `varlist` only contains one (`pattern` `source`) element, you can
2486optionally specify it using a vector and discarding the
2487outer-most parens.  Thus
2488
2489    (-let ((`pattern` `source`)) ...)
2490
2491becomes
2492
2493    (-let [`pattern` `source`] ...).
2494
2495[`-let`](#-let-varlist-rest-body) uses a convention of not binding places (symbols) starting
2496with _ whenever it's possible.  You can use this to skip over
2497entries you don't care about.  However, this is not *always*
2498possible (as a result of implementation) and these symbols might
2499get bound to undefined values.
2500
2501Following is the overview of supported patterns.  Remember that
2502patterns can be matched recursively, so every a, b, aK in the
2503following can be a matching construct and not necessarily a
2504symbol/variable.
2505
2506Symbol:
2507
2508    a - bind the `source` to `a`.  This is just like regular `let`.
2509
2510Conses 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
2522Vectors:
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
2539Key/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
2556Further, special keyword &keys supports "inline" matching of
2557plist-like key-value pairs, similarly to &keys keyword of
2558`cl-defun`.
2559
2560    (a1 a2 ... aN &keys key1 b1 ... keyN bK)
2561
2562This binds `n` values from the list to a1 ... aN, then interprets
2563the cdr as a plist (see key/value matching above).
2564
2565`a` shorthand notation for kv-destructuring exists which allows the
2566patterns be optionally left out and derived from the key name in
2567the 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
2573That is, the entire value under the key is bound to the derived
2574variable without any further destructuring.
2575
2576This is possible only when the form following the key is not a
2577valid pattern (i.e. not a symbol, a cons cell or a vector).
2578Otherwise the matching proceeds as usual and in case of an
2579invalid spec fails with an error.
2580
2581Thus 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
2595You can name the source using the syntax `symbol` &as `pattern`.
2596This syntax works with lists (proper or improper), vectors and
2597all types of maps.
2598
2599    (list &as a b c) (list 1 2 3)
2600
2601binds `a` to 1, `b` to 2, `c` to 3 and `list` to (1 2 3).
2602
2603Similarly:
2604
2605    (bounds &as beg . end) (cons 1 2)
2606
2607binds `beg` to 1, `end` to 2 and `bounds` to (1 . 2).
2608
2609    (items &as first . rest) (list 1 2 3)
2610
2611binds `first` to 1, `rest` to (2 3) and `items` to (1 2 3)
2612
2613    [vect &as _ b c] [1 2 3]
2614
2615binds `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
2619binds `b` to 2 and `plist` to (:a 1 :b 2 :c 3).  Same for &alist and &hash.
2620
2621This is especially useful when we want to capture the result of a
2622computation and destructure at the same time.  Consider the
2623form (function-returning-complex-structure) returning a list of
2624two vectors with two items each.  We want to capture this entire
2625result and pass it to another computation, but at the same time
2626we want to get the second item from each vector.  We can achieve
2627it with pattern
2628
2629    (result &as [_ a] [_ b]) (function-returning-complex-structure)
2630
2631Note: Clojure programmers may know this feature as the ":as
2632binding".  The difference is that we put the &as at the front
2633because 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
2643Bind 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
2647only evaluated once for each `pattern`.
2648
2649Each `source` can refer to the symbols already bound by this
2650`varlist`.  This is useful if you want to destructure `source`
2651recursively but also want to name the intermediate structures.
2652
2653See [`-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
2663Return a lambda which destructures its input as `match-form` and executes `body`.
2664
2665Note that you have to enclose the `match-form` in a pair of parens,
2666such that:
2667
2668    (-lambda (x) body)
2669    (-lambda (x y ...) body)
2670
2671has the usual semantics of `lambda`.  Furthermore, these get
2672translated into normal `lambda`, so there is no performance
2673penalty.
2674
2675See [`-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
2685Bind 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
2689This macro allows you to bind multiple variables by destructuring
2690the value, so for example:
2691
2692    (-setq (a b) x
2693           (&plist :c c) plist)
2694
2695expands roughly speaking to the following code
2696
2697    (setq a (car x)
2698          b (cadr x)
2699          c (plist-get plist :c))
2700
2701Care is taken to only evaluate each `val` once so that in case of
2702multiple 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
2712Functions iterating over lists for side effect only.
2713
2714#### -each `(list fn)`
2715
2716Call `fn` on each element of `list`.
2717Return nil; this function is intended for side effects.
2718
2719Its anaphoric counterpart is `--each`.
2720
2721For 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
2732Call `fn` on each `item` in `list`, while (`pred` `item`) is non-nil.
2733Once an `item` is reached for which `pred` returns nil, `fn` is no
2734longer called.  Return nil; this function is intended for side
2735effects.
2736
2737Its 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
2747Call `fn` on each index and element of `list`.
2748For each `item` at `index` in `list`, call (funcall `fn` `index` `item`).
2749Return nil; this function is intended for side effects.
2750
2751See 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
2761Call `fn` on each element of `list` in reversed order.
2762Return nil; this function is intended for side effects.
2763
2764Its 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
2774Call `fn` on each `item` in reversed `list`, while (`pred` `item`) is non-nil.
2775Once an `item` is reached for which `pred` returns nil, `fn` is no
2776longer called.  Return nil; this function is intended for side
2777effects.
2778
2779Its 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
2789Call `fn` `num` times, presumably for side effects.
2790`fn` is called with a single argument on successive integers
2791running from 0, inclusive, to `num`, exclusive.  `fn` is not called
2792if `num` is less than 1.
2793
2794This 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
2804Macros that modify variables holding lists.
2805
2806#### !cons `(car cdr)`
2807
2808Destructive: 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
2817Destructive: 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
2826Functions that manipulate and compose other functions.
2827
2828#### -partial `(fun &rest args)`
2829
2830Return 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`.
2832The result is a new function which does the same as `fun`, except that
2833the first `n` arguments are fixed at the values with which this function
2834was 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
2844Return 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
2846is 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
2848called.  This is like [`-partial`](#-partial-fun-rest-args), except the arguments are fixed
2849starting 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
2859Return a function that is the juxtaposition of `fns`.
2860The returned function takes a variable number of `args`, applies
2861each 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
2871Compose `fns` into a single composite function.
2872Return a function that takes a variable number of `args`, applies
2873the last function in `fns` to `args`, and returns the result of
2874calling each remaining function on the result of the previous
2875function, 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
2886Return a function that applies `fn` to a single list of args.
2887This changes the arity of `fn` from taking `n` distinct arguments to
2888taking 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
2898Return a function that calls `trans` on each arg and `op` on the results.
2899The returned function takes a variable number of arguments, calls
2900the function `trans` on each one in turn, and then passes those
2901results as the list of arguments to `op`, in the same order.
2902
2903For example, the following pairs of expressions are morally
2904equivalent:
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
2917Return a function that calls `fn` with its arguments reversed.
2918The returned function takes the same number of arguments as `fn`.
2919
2920For example, the following two expressions are morally
2921equivalent:
2922
2923    (funcall (-flip #'-) 1 2) = (- 2 1)
2924
2925See 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
2935Return a function that calls `fn` with args rotated `n` places to the right.
2936The returned function takes the same number of arguments as `fn`,
2937rotates the list of arguments `n` places to the right (left if `n` is
2938negative) just like [`-rotate`](#-rotate-n-list), and applies `fn` to the result.
2939
2940See 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
2950Return a function that returns `c` ignoring any additional arguments.
2951
2952In 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
2962Take n-ary function and n arguments and specialize some of them.
2963Arguments denoted by <> will be left unspecialized.
2964
2965See `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
2975Return a predicate that negates the result of `pred`.
2976The returned predicate passes its arguments to `pred`.  If `pred`
2977returns nil, the result is non-nil; otherwise the result is nil.
2978
2979See 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
2989Return a predicate that returns the first non-nil result of `preds`.
2990The returned predicate takes a variable number of arguments,
2991passes them to each predicate in `preds` in turn until one of them
2992returns non-nil, and returns that non-nil result without calling
2993the remaining `preds`.  If all `preds` return nil, or if no `preds` are
2994given, the returned predicate returns nil.
2995
2996See 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
3006Return a predicate that returns non-nil if all `preds` do so.
3007The returned predicate `p` takes a variable number of arguments and
3008passes them to each predicate in `preds` in turn.  If any one of
3009`preds` returns nil, `p` also returns nil without calling the
3010remaining `preds`.  If all `preds` return non-nil, `p` returns the last
3011such value.  If no `preds` are given, `p` always returns non-nil.
3012
3013See 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
3023Return a function `fn` composed `n` times with itself.
3024
3025`fn` is a unary function.  If you need to use a function of higher
3026arity, use [`-applify`](#-applify-fn) first to turn it into a unary function.
3027
3028With n = 0, this acts as identity function.
3029
3030In types: (a -> a) -> Int -> a -> a.
3031
3032This 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
3044Return a function that computes the (least) fixpoint of `fn`.
3045
3046`fn` must be a unary function. The returned lambda takes a single
3047argument, `x`, the initial value for the fixpoint iteration. The
3048iteration 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
3064The return value of the lambda is either the fixpoint or, if
3065iteration halted before converging, a cons with car `halted` and
3066cdr the final output from `halt-test`.
3067
3068In 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
3078Take a list of n functions and return a function that takes a
3079list of length n, applying i-th function to i-th element of the
3080input list.  Returns a list of length n.
3081
3082In types (for n=2): ((a -> b), (c -> d)) -> (a, c) -> (b, d)
3083
3084This 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
3099Yes, please do.  Pure functions in the list manipulation realm only,
3100please.  There's a suite of examples/tests in `dev/examples.el`, so
3101remember to add tests for your additions, or I might break them later.
3102
3103You'll find the repo at:
3104
3105    https://github.com/magnars/dash.el
3106
3107Run the tests with:
3108
3109    make check
3110
3111Regenerate the docs with:
3112
3113    make docs
3114
3115I highly recommend that you install these as a pre-commit hook, so
3116that the tests are always running and the docs are always in sync:
3117
3118    cp dev/pre-commit.sh .git/hooks/pre-commit
3119
3120Oh, and don't edit `README.md` or `dash.texi` directly; they are
3121auto-generated.  Change `readme-template.md` or `dash-template.texi`
3122instead, respectively.
3123
3124To ensure that `dash.el` can be distributed with GNU ELPA or Emacs, we
3125require that all contributors assign copyright to the Free Software
3126Foundation.  For more on this, see [`(info "(emacs) Copyright
3127Assignment")`](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
3160Thanks!
3161
3162New contributors are very welcome.  See the
3163[`Contribute`](#contribute) section above.
3164
3165## License
3166
3167Copyright (C) 2012-2021 Free Software Foundation, Inc.
3168
3169Author: Magnar Sveen <magnars@gmail.com>
3170
3171This program is free software: you can redistribute it and/or modify
3172it under the terms of the GNU General Public License as published by
3173the Free Software Foundation, either version 3 of the License, or
3174(at your option) any later version.
3175
3176This program is distributed in the hope that it will be useful,
3177but WITHOUT ANY WARRANTY; without even the implied warranty of
3178MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3179GNU General Public License for more details.
3180
3181You should have received a copy of the GNU General Public License
3182along with this program.  If not, see <https://www.gnu.org/licenses/>.
3183

readme-template.md

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
9A modern list API for Emacs.  No
10[`'cl`](https://gnu.org/software/emacs/manual/html_node/cl/) required.
11
12See 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
26See 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
42Dash is available on [GNU ELPA](https://elpa.gnu.org/), [GNU-devel
43ELPA](https://elpa.gnu.org/devel/), and [MELPA](https://melpa.org/),
44and can be installed with the standard command `package-install`:
45
46    M-x package-install RET dash RET
47
48See [`(info "(emacs) Package
49Installation")`](https://gnu.org/software/emacs/manual/html_node/emacs/Package-Installation.html).
50
51Alternatively, you can just dump `dash.el` in your `load-path`
52somewhere.  See [`(info "(emacs) Lisp
53Libraries")`](https://gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html).
54
55### Using in a package
56
57Add something like this to the library's headers:
58
59    ;; Package-Requires: ((dash "[[ dash-version ]]"))
60
61See [`(info "(elisp) Library
62Headers")`](https://gnu.org/software/emacs/manual/html_node/elisp/Library-Headers.html).
63
64### Fontification of special variables
65
66Font lock of special Dash variables (`it`, `acc`, etc.) in Emacs Lisp
67buffers can optionally be enabled with the autoloaded minor mode
68`dash-fontify-mode`.  In older Emacs versions which do not dynamically
69detect macros, the minor mode also fontifies Dash macro calls.
70
71To automatically enable the minor mode in all Emacs Lisp buffers, just
72call its autoloaded global counterpart `global-dash-fontify-mode`,
73either interactively or from your `user-init-file`:
74
75```el
76(global-dash-fontify-mode)
77```
78
79### Info symbol lookup
80
81While editing Elisp files, you can use `C-h S` (`info-lookup-symbol`)
82to look up Elisp symbols in the relevant Info manuals (see [`(emacs)
83Info
84Lookup`](https://gnu.org/software/emacs/manual/html_node/emacs/Info-Lookup.html)).
85To enable the same for Dash symbols, use the command
86`dash-register-info-lookup`.  It can be called directly when needed,
87or 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
96All functions and constructs in the library use a dash (`-`) prefix.
97
98The library also provides anaphoric macro versions of functions where
99that makes sense.  The names of these macros are prefixed with two
100dashes (`--`) instead of one.
101
102While `-map` applies a function to each element of a list, its
103anaphoric counterpart `--map` evaluates a form with the local variable
104`it` temporarily bound to the current list element instead.  For
105example:
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
112The 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
122This demonstrates the utility of both versions.
123[[ function-list ]]
124
125[[ function-docs ]]
126## Contribute
127
128Yes, please do.  Pure functions in the list manipulation realm only,
129please.  There's a suite of examples/tests in `dev/examples.el`, so
130remember to add tests for your additions, or I might break them later.
131
132You'll find the repo at:
133
134    https://github.com/magnars/dash.el
135
136Run the tests with:
137
138    make check
139
140Regenerate the docs with:
141
142    make docs
143
144I highly recommend that you install these as a pre-commit hook, so
145that the tests are always running and the docs are always in sync:
146
147    cp dev/pre-commit.sh .git/hooks/pre-commit
148
149Oh, and don't edit `README.md` or `dash.texi` directly; they are
150auto-generated.  Change `readme-template.md` or `dash-template.texi`
151instead, respectively.
152
153To ensure that `dash.el` can be distributed with GNU ELPA or Emacs, we
154require that all contributors assign copyright to the Free Software
155Foundation.  For more on this, see [`(info "(emacs) Copyright
156Assignment")`](https://gnu.org/software/emacs/manual/html_node/emacs/Copyright-Assignment.html).
157
158## Contributors
159
160- [Matus Goljer](https://github.com/Fuco1) contributed lots of features and
161  functions.
162- [Takafumi Arakaki](https://github.com/tkf) contributed `-group-by`.
163- [tali713](https://github.com/tali713) is the author of `-applify`.
164- [Víctor M. Valenzuela](https://github.com/vemv) contributed `-repeat`.
165- [Nic Ferrier](https://github.com/nicferrier) contributed `-cons*`.
166- [Wilfred Hughes](https://github.com/Wilfred) contributed `-slice`,
167  `-first-item`, and `-last-item`.
168- [Emanuel Evans](https://github.com/shosti) contributed `-if-let`, `-when-let`,
169  and `-insert-at`.
170- [Johan Andersson](https://github.com/rejeep) contributed `-sum`, `-product`,
171  and `-same-items?`.
172- [Christina Whyte](https://github.com/kurisuwhyte) contributed `-compose`.
173- [Steve Lamb](https://github.com/steventlamb) contributed `-cycle`, `-pad`,
174  `-annotate`, `-zip-fill`, and a variadic version of `-zip`.
175- [Fredrik Bergroth](https://github.com/fbergroth) made the `-if-let` family use
176  `-let` destructuring and improved the script for generating documentation.
177- [Mark Oteiza](https://github.com/holomorph) contributed `-iota` and
178  the script to create an Info manual.
179- [Vasilij Schneidermann](https://github.com/wasamasa) contributed `-some`.
180- [William West](https://github.com/occidens) made `-fixfn` more robust at
181  handling floats.
182- [Cam Saul](https://github.com/camsaul) contributed `-some->`, `-some->>`, and
183  `-some-->`.
184- [Basil L. Contovounesios](https://github.com/basil-conto) contributed
185  `-common-prefix`, `-common-suffix`, and various other improvements.
186- [Paul Pogonyshev](https://github.com/doublep) contributed `-each-r` and
187  `-each-r-while`.
188
189Thanks!
190
191New contributors are very welcome.  See the
192[`Contribute`](#contribute) section above.
193
194## License
195
196Copyright (C) 2012-2021 Free Software Foundation, Inc.
197
198Author: Magnar Sveen <magnars@gmail.com>
199
200This program is free software: you can redistribute it and/or modify
201it under the terms of the GNU General Public License as published by
202the Free Software Foundation, either version 3 of the License, or
203(at your option) any later version.
204
205This program is distributed in the hope that it will be useful,
206but WITHOUT ANY WARRANTY; without even the implied warranty of
207MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
208GNU General Public License for more details.
209
210You should have received a copy of the GNU General Public License
211along with this program.  If not, see <https://www.gnu.org/licenses/>.
212