dash.texi (118982B)
1 \input texinfo @c -*- texinfo -*- 2 @c %**start of header 3 @setfilename dash.info 4 @set DASHVER 2.19.1 5 @settitle Dash: A modern list library for GNU Emacs. 6 @documentencoding UTF-8 7 @documentlanguage en 8 @c %**end of header 9 10 @copying 11 This manual is for Dash version @value{DASHVER}. 12 13 Copyright @copyright{} 2012--2021 Free Software Foundation, Inc. 14 15 @quotation 16 Permission is granted to copy, distribute and/or modify this document 17 under the terms of the GNU Free Documentation License, Version 1.3 or 18 any later version published by the Free Software Foundation; with the 19 Invariant Sections being ``GNU General Public License,'' and no 20 Front-Cover Texts or Back-Cover Texts. A copy of the license is 21 included in the section entitled ``GNU Free Documentation License''. 22 @end quotation 23 @end copying 24 25 @dircategory Emacs 26 @direntry 27 * Dash: (dash.info). A modern list library for GNU Emacs. 28 @end direntry 29 30 @titlepage 31 @title Dash Manual 32 @subtitle For Dash Version @value{DASHVER}. 33 @author Magnar Sveen 34 @page 35 @vskip 0pt plus 1filll 36 @insertcopying 37 @end titlepage 38 39 @contents 40 41 @ifnottex 42 @node Top 43 @top Dash 44 45 @insertcopying 46 @end ifnottex 47 48 @menu 49 * Installation:: Installing and configuring Dash. 50 * Functions:: Dash API reference. 51 * Development:: Contributing to Dash development. 52 53 Appendices 54 55 * FDL:: The license for this documentation. 56 * GPL:: Conditions for copying and changing Dash. 57 * Index:: Index including functions and macros. 58 59 @detailmenu 60 --- The Detailed Node Listing --- 61 62 Installation 63 64 * Using in a package:: Listing Dash as a package dependency. 65 * Fontification of special variables:: Font Lock of anaphoric macro variables. 66 * Info symbol lookup:: Looking up Dash symbols in this manual. 67 68 Functions 69 70 * Maps:: 71 * Sublist selection:: 72 * List to list:: 73 * Reductions:: 74 * Unfolding:: 75 * Predicates:: 76 * Partitioning:: 77 * Indexing:: 78 * Set operations:: 79 * Other list operations:: 80 * Tree operations:: 81 * Threading macros:: 82 * Binding:: 83 * Side effects:: 84 * Destructive operations:: 85 * Function combinators:: 86 87 Development 88 89 * Contribute:: How to contribute. 90 * Contributors:: List of contributors. 91 @end detailmenu 92 @end menu 93 94 @node Installation 95 @chapter Installation 96 97 Dash is available on @url{https://elpa.gnu.org/, GNU ELPA}, 98 @url{https://elpa.gnu.org/devel/, GNU-devel ELPA}, and 99 @url{https://melpa.org/, MELPA}, and can be installed with the 100 standard command @code{package-install} (@pxref{Package 101 Installation,,, emacs, The GNU Emacs Manual}). 102 103 @table @kbd 104 @item M-x package-install @key{RET} dash @key{RET} 105 Install the Dash library. 106 @end table 107 108 Alternatively, you can just dump @file{dash.el} in your 109 @code{load-path} somewhere (@pxref{Lisp Libraries,,, emacs, The GNU 110 Emacs Manual}). 111 112 @menu 113 * Using in a package:: Listing Dash as a package dependency. 114 * Fontification of special variables:: Font Lock of anaphoric macro variables. 115 * Info symbol lookup:: Looking up Dash symbols in this manual. 116 @end menu 117 118 @node Using in a package 119 @section Using in a package 120 121 If you use Dash in your own package, be sure to list it as a 122 dependency in the library's headers as follows (@pxref{Library 123 Headers,,, elisp, The Emacs Lisp Reference Manual}). 124 125 @lisp 126 ;; Package-Requires: ((dash "@value{DASHVER}")) 127 @end lisp 128 129 @node Fontification of special variables 130 @section Fontification of special variables 131 132 @findex dash-fontify-mode 133 The autoloaded minor mode @code{dash-fontify-mode} is provided for 134 optional fontification of anaphoric Dash variables (@code{it}, 135 @code{acc}, etc.@:) in Emacs Lisp buffers using search-based Font Lock 136 (@pxref{Font Lock,,, emacs, The GNU Emacs Manual}). In older Emacs 137 versions which do not dynamically detect macros, the minor mode also 138 fontifies calls to Dash macros. 139 140 @findex global-dash-fontify-mode 141 To automatically enable the minor mode in all Emacs Lisp buffers, just 142 call its autoloaded global counterpart 143 @code{global-dash-fontify-mode}, either interactively or from your 144 @code{user-init-file}: 145 146 @lisp 147 (global-dash-fontify-mode) 148 @end lisp 149 150 @node Info symbol lookup 151 @section Info symbol lookup 152 153 @findex dash-register-info-lookup 154 While editing Elisp files, you can use @kbd{C-h S} 155 (@code{info-lookup-symbol}) to look up Elisp symbols in the relevant 156 Info manuals (@pxref{Info Lookup,,, emacs, The GNU Emacs Manual}). To 157 enable the same for Dash symbols, use the command 158 @code{dash-register-info-lookup}. It can be called directly when 159 needed, or automatically from your @code{user-init-file}. For 160 example: 161 162 @lisp 163 (with-eval-after-load 'info-look 164 (dash-register-info-lookup)) 165 @end lisp 166 167 @node Functions 168 @chapter Functions 169 170 This chapter contains reference documentation for the Dash 171 @acronym{API, Application Programming Interface}. The names of all 172 public functions defined in the library are prefixed with a dash 173 character (@samp{-}). 174 175 The library also provides anaphoric macro versions of functions where 176 that makes sense. The names of these macros are prefixed with two 177 dashes (@samp{--}) instead of one. 178 179 For instance, while the function @code{-map} applies a function to 180 each element of a list, its anaphoric counterpart @code{--map} 181 evaluates a form with the local variable @code{it} temporarily bound 182 to the current list element instead. 183 184 @lisp 185 @group 186 ;; Normal version. 187 (-map (lambda (n) (* n n)) '(1 2 3 4)) 188 @result{} (1 4 9 16) 189 @end group 190 191 @group 192 ;; Anaphoric version. 193 (--map (* it it) '(1 2 3 4)) 194 @result{} (1 4 9 16) 195 @end group 196 @end lisp 197 198 The normal version can, of course, also be written as in the following 199 example, which demonstrates the utility of both versions. 200 201 @lisp 202 @group 203 (defun my-square (n) 204 "Return N multiplied by itself." 205 (* n n)) 206 207 (-map #'my-square '(1 2 3 4)) 208 @result{} (1 4 9 16) 209 @end group 210 @end lisp 211 212 @menu 213 * Maps:: 214 * Sublist selection:: 215 * List to list:: 216 * Reductions:: 217 * Unfolding:: 218 * Predicates:: 219 * Partitioning:: 220 * Indexing:: 221 * Set operations:: 222 * Other list operations:: 223 * Tree operations:: 224 * Threading macros:: 225 * Binding:: 226 * Side effects:: 227 * Destructive operations:: 228 * Function combinators:: 229 @end menu 230 231 @node Maps 232 @section Maps 233 234 Functions in this category take a transforming function, which 235 is then applied sequentially to each or selected elements of the 236 input list. The results are collected in order and returned as a 237 new list. 238 239 @anchor{-map} 240 @defun -map (fn list) 241 Apply @var{fn} to each item in @var{list} and return the list of results. 242 243 This function's anaphoric counterpart is @code{--map}. 244 245 @example 246 @group 247 (-map (lambda (num) (* num num)) '(1 2 3 4)) 248 @result{} (1 4 9 16) 249 @end group 250 @group 251 (-map #'1+ '(1 2 3 4)) 252 @result{} (2 3 4 5) 253 @end group 254 @group 255 (--map (* it it) '(1 2 3 4)) 256 @result{} (1 4 9 16) 257 @end group 258 @end example 259 @end defun 260 261 @anchor{-map-when} 262 @defun -map-when (pred rep list) 263 Return a new list where the elements in @var{list} that do not match the @var{pred} function 264 are unchanged, and where the elements in @var{list} that do match the @var{pred} function are mapped 265 through the @var{rep} function. 266 267 Alias: @code{-replace-where} 268 269 See also: @code{-update-at} (@pxref{-update-at}) 270 271 @example 272 @group 273 (-map-when 'even? 'square '(1 2 3 4)) 274 @result{} (1 4 3 16) 275 @end group 276 @group 277 (--map-when (> it 2) (* it it) '(1 2 3 4)) 278 @result{} (1 2 9 16) 279 @end group 280 @group 281 (--map-when (= it 2) 17 '(1 2 3 4)) 282 @result{} (1 17 3 4) 283 @end group 284 @end example 285 @end defun 286 287 @anchor{-map-first} 288 @defun -map-first (pred rep list) 289 Replace first item in @var{list} satisfying @var{pred} with result of @var{rep} called on this item. 290 291 See also: @code{-map-when} (@pxref{-map-when}), @code{-replace-first} (@pxref{-replace-first}) 292 293 @example 294 @group 295 (-map-first 'even? 'square '(1 2 3 4)) 296 @result{} (1 4 3 4) 297 @end group 298 @group 299 (--map-first (> it 2) (* it it) '(1 2 3 4)) 300 @result{} (1 2 9 4) 301 @end group 302 @group 303 (--map-first (= it 2) 17 '(1 2 3 2)) 304 @result{} (1 17 3 2) 305 @end group 306 @end example 307 @end defun 308 309 @anchor{-map-last} 310 @defun -map-last (pred rep list) 311 Replace last item in @var{list} satisfying @var{pred} with result of @var{rep} called on this item. 312 313 See also: @code{-map-when} (@pxref{-map-when}), @code{-replace-last} (@pxref{-replace-last}) 314 315 @example 316 @group 317 (-map-last 'even? 'square '(1 2 3 4)) 318 @result{} (1 2 3 16) 319 @end group 320 @group 321 (--map-last (> it 2) (* it it) '(1 2 3 4)) 322 @result{} (1 2 3 16) 323 @end group 324 @group 325 (--map-last (= it 2) 17 '(1 2 3 2)) 326 @result{} (1 2 3 17) 327 @end group 328 @end example 329 @end defun 330 331 @anchor{-map-indexed} 332 @defun -map-indexed (fn list) 333 Apply @var{fn} to each index and item in @var{list} and return the list of results. 334 This is like @code{-map} (@pxref{-map}), but @var{fn} takes two arguments: the index of the 335 current element within @var{list}, and the element itself. 336 337 This function's anaphoric counterpart is @code{--map-indexed}. 338 339 For a side-effecting variant, see also @code{-each-indexed} (@pxref{-each-indexed}). 340 341 @example 342 @group 343 (-map-indexed (lambda (index item) (- item index)) '(1 2 3 4)) 344 @result{} (1 1 1 1) 345 @end group 346 @group 347 (--map-indexed (- it it-index) '(1 2 3 4)) 348 @result{} (1 1 1 1) 349 @end group 350 @group 351 (-map-indexed #'* '(1 2 3 4)) 352 @result{} (0 2 6 12) 353 @end group 354 @end example 355 @end defun 356 357 @anchor{-annotate} 358 @defun -annotate (fn list) 359 Return a list of cons cells where each cell is @var{fn} applied to each 360 element of @var{list} paired with the unmodified element of @var{list}. 361 362 @example 363 @group 364 (-annotate '1+ '(1 2 3)) 365 @result{} ((2 . 1) (3 . 2) (4 . 3)) 366 @end group 367 @group 368 (-annotate 'length '(("h" "e" "l" "l" "o") ("hello" "world"))) 369 @result{} ((5 "h" "e" "l" "l" "o") (2 "hello" "world")) 370 @end group 371 @group 372 (--annotate (< 1 it) '(0 1 2 3)) 373 @result{} ((nil . 0) (nil . 1) (t . 2) (t . 3)) 374 @end group 375 @end example 376 @end defun 377 378 @anchor{-splice} 379 @defun -splice (pred fun list) 380 Splice lists generated by @var{fun} in place of elements matching @var{pred} in @var{list}. 381 382 @var{fun} takes the element matching @var{pred} as input. 383 384 This function can be used as replacement for @code{,@@} in case you 385 need to splice several lists at marked positions (for example 386 with keywords). 387 388 See also: @code{-splice-list} (@pxref{-splice-list}), @code{-insert-at} (@pxref{-insert-at}) 389 390 @example 391 @group 392 (-splice 'even? (lambda (x) (list x x)) '(1 2 3 4)) 393 @result{} (1 2 2 3 4 4) 394 @end group 395 @group 396 (--splice 't (list it it) '(1 2 3 4)) 397 @result{} (1 1 2 2 3 3 4 4) 398 @end group 399 @group 400 (--splice (equal it :magic) '((list of) (magical) (code)) '((foo) (bar) :magic (baz))) 401 @result{} ((foo) (bar) (list of) (magical) (code) (baz)) 402 @end group 403 @end example 404 @end defun 405 406 @anchor{-splice-list} 407 @defun -splice-list (pred new-list list) 408 Splice @var{new-list} in place of elements matching @var{pred} in @var{list}. 409 410 See also: @code{-splice} (@pxref{-splice}), @code{-insert-at} (@pxref{-insert-at}) 411 412 @example 413 @group 414 (-splice-list 'keywordp '(a b c) '(1 :foo 2)) 415 @result{} (1 a b c 2) 416 @end group 417 @group 418 (-splice-list 'keywordp nil '(1 :foo 2)) 419 @result{} (1 2) 420 @end group 421 @group 422 (--splice-list (keywordp it) '(a b c) '(1 :foo 2)) 423 @result{} (1 a b c 2) 424 @end group 425 @end example 426 @end defun 427 428 @anchor{-mapcat} 429 @defun -mapcat (fn list) 430 Return the concatenation of the result of mapping @var{fn} over @var{list}. 431 Thus function @var{fn} should return a list. 432 433 @example 434 @group 435 (-mapcat 'list '(1 2 3)) 436 @result{} (1 2 3) 437 @end group 438 @group 439 (-mapcat (lambda (item) (list 0 item)) '(1 2 3)) 440 @result{} (0 1 0 2 0 3) 441 @end group 442 @group 443 (--mapcat (list 0 it) '(1 2 3)) 444 @result{} (0 1 0 2 0 3) 445 @end group 446 @end example 447 @end defun 448 449 @anchor{-copy} 450 @defun -copy (list) 451 Create a shallow copy of @var{list}. 452 453 @example 454 @group 455 (-copy '(1 2 3)) 456 @result{} (1 2 3) 457 @end group 458 @group 459 (let ((a '(1 2 3))) (eq a (-copy a))) 460 @result{} nil 461 @end group 462 @end example 463 @end defun 464 465 @node Sublist selection 466 @section Sublist selection 467 468 Functions returning a sublist of the original list. 469 470 @anchor{-filter} 471 @defun -filter (pred list) 472 Return a new list of the items in @var{list} for which @var{pred} returns non-nil. 473 474 Alias: @code{-select}. 475 476 This function's anaphoric counterpart is @code{--filter}. 477 478 For similar operations, see also @code{-keep} (@pxref{-keep}) and @code{-remove} (@pxref{-remove}). 479 480 @example 481 @group 482 (-filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) 483 @result{} (2 4) 484 @end group 485 @group 486 (-filter #'natnump '(-2 -1 0 1 2)) 487 @result{} (0 1 2) 488 @end group 489 @group 490 (--filter (= 0 (% it 2)) '(1 2 3 4)) 491 @result{} (2 4) 492 @end group 493 @end example 494 @end defun 495 496 @anchor{-remove} 497 @defun -remove (pred list) 498 Return a new list of the items in @var{list} for which @var{pred} returns nil. 499 500 Alias: @code{-reject}. 501 502 This function's anaphoric counterpart is @code{--remove}. 503 504 For similar operations, see also @code{-keep} (@pxref{-keep}) and @code{-filter} (@pxref{-filter}). 505 506 @example 507 @group 508 (-remove (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) 509 @result{} (1 3) 510 @end group 511 @group 512 (-remove #'natnump '(-2 -1 0 1 2)) 513 @result{} (-2 -1) 514 @end group 515 @group 516 (--remove (= 0 (% it 2)) '(1 2 3 4)) 517 @result{} (1 3) 518 @end group 519 @end example 520 @end defun 521 522 @anchor{-remove-first} 523 @defun -remove-first (pred list) 524 Remove the first item from @var{list} for which @var{pred} returns non-nil. 525 This is a non-destructive operation, but only the front of @var{list} 526 leading up to the removed item is a copy; the rest is @var{list}'s 527 original tail. If no item is removed, then the result is a 528 complete copy. 529 530 Alias: @code{-reject-first}. 531 532 This function's anaphoric counterpart is @code{--remove-first}. 533 534 See also @code{-map-first} (@pxref{-map-first}), @code{-remove-item} (@pxref{-remove-item}), and @code{-remove-last} (@pxref{-remove-last}). 535 536 @example 537 @group 538 (-remove-first #'natnump '(-2 -1 0 1 2)) 539 @result{} (-2 -1 1 2) 540 @end group 541 @group 542 (-remove-first #'stringp '(1 2 "first" "second")) 543 @result{} (1 2 "second") 544 @end group 545 @group 546 (--remove-first (> it 3) '(1 2 3 4 5 6)) 547 @result{} (1 2 3 5 6) 548 @end group 549 @end example 550 @end defun 551 552 @anchor{-remove-last} 553 @defun -remove-last (pred list) 554 Remove the last item from @var{list} for which @var{pred} returns non-nil. 555 The result is a copy of @var{list} regardless of whether an element is 556 removed. 557 558 Alias: @code{-reject-last}. 559 560 This function's anaphoric counterpart is @code{--remove-last}. 561 562 See also @code{-map-last} (@pxref{-map-last}), @code{-remove-item} (@pxref{-remove-item}), and @code{-remove-first} (@pxref{-remove-first}). 563 564 @example 565 @group 566 (-remove-last #'natnump '(1 3 5 4 7 8 10 -11)) 567 @result{} (1 3 5 4 7 8 -11) 568 @end group 569 @group 570 (-remove-last #'stringp '(1 2 "last" "second")) 571 @result{} (1 2 "last") 572 @end group 573 @group 574 (--remove-last (> it 3) '(1 2 3 4 5 6 7 8 9 10)) 575 @result{} (1 2 3 4 5 6 7 8 9) 576 @end group 577 @end example 578 @end defun 579 580 @anchor{-remove-item} 581 @defun -remove-item (item list) 582 Return a copy of @var{list} with all occurrences of @var{item} removed. 583 The comparison is done with @code{equal}. 584 585 @example 586 @group 587 (-remove-item 3 '(1 2 3 2 3 4 5 3)) 588 @result{} (1 2 2 4 5) 589 @end group 590 @group 591 (-remove-item 'foo '(foo bar baz foo)) 592 @result{} (bar baz) 593 @end group 594 @group 595 (-remove-item "bob" '("alice" "bob" "eve" "bob")) 596 @result{} ("alice" "eve") 597 @end group 598 @end example 599 @end defun 600 601 @anchor{-non-nil} 602 @defun -non-nil (list) 603 Return a copy of @var{list} with all nil items removed. 604 605 @example 606 @group 607 (-non-nil '(nil 1 nil 2 nil nil 3 4 nil 5 nil)) 608 @result{} (1 2 3 4 5) 609 @end group 610 @group 611 (-non-nil '((nil))) 612 @result{} ((nil)) 613 @end group 614 @group 615 (-non-nil ()) 616 @result{} () 617 @end group 618 @end example 619 @end defun 620 621 @anchor{-slice} 622 @defun -slice (list from &optional to step) 623 Return copy of @var{list}, starting from index @var{from} to index @var{to}. 624 625 @var{from} or @var{to} may be negative. These values are then interpreted 626 modulo the length of the list. 627 628 If @var{step} is a number, only each STEPth item in the resulting 629 section is returned. Defaults to 1. 630 631 @example 632 @group 633 (-slice '(1 2 3 4 5) 1) 634 @result{} (2 3 4 5) 635 @end group 636 @group 637 (-slice '(1 2 3 4 5) 0 3) 638 @result{} (1 2 3) 639 @end group 640 @group 641 (-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2) 642 @result{} (2 4 6 8) 643 @end group 644 @end example 645 @end defun 646 647 @anchor{-take} 648 @defun -take (n list) 649 Return a copy of the first @var{n} items in @var{list}. 650 Return a copy of @var{list} if it contains @var{n} items or fewer. 651 Return nil if @var{n} is zero or less. 652 653 See also: @code{-take-last} (@pxref{-take-last}). 654 655 @example 656 @group 657 (-take 3 '(1 2 3 4 5)) 658 @result{} (1 2 3) 659 @end group 660 @group 661 (-take 17 '(1 2 3 4 5)) 662 @result{} (1 2 3 4 5) 663 @end group 664 @group 665 (-take 0 '(1 2 3 4 5)) 666 @result{} () 667 @end group 668 @end example 669 @end defun 670 671 @anchor{-take-last} 672 @defun -take-last (n list) 673 Return a copy of the last @var{n} items of @var{list} in order. 674 Return a copy of @var{list} if it contains @var{n} items or fewer. 675 Return nil if @var{n} is zero or less. 676 677 See also: @code{-take} (@pxref{-take}). 678 679 @example 680 @group 681 (-take-last 3 '(1 2 3 4 5)) 682 @result{} (3 4 5) 683 @end group 684 @group 685 (-take-last 17 '(1 2 3 4 5)) 686 @result{} (1 2 3 4 5) 687 @end group 688 @group 689 (-take-last 1 '(1 2 3 4 5)) 690 @result{} (5) 691 @end group 692 @end example 693 @end defun 694 695 @anchor{-drop} 696 @defun -drop (n list) 697 Return the tail (not a copy) of @var{list} without the first @var{n} items. 698 Return nil if @var{list} contains @var{n} items or fewer. 699 Return @var{list} if @var{n} is zero or less. 700 701 For another variant, see also @code{-drop-last} (@pxref{-drop-last}). 702 703 @example 704 @group 705 (-drop 3 '(1 2 3 4 5)) 706 @result{} (4 5) 707 @end group 708 @group 709 (-drop 17 '(1 2 3 4 5)) 710 @result{} () 711 @end group 712 @group 713 (-drop 0 '(1 2 3 4 5)) 714 @result{} (1 2 3 4 5) 715 @end group 716 @end example 717 @end defun 718 719 @anchor{-drop-last} 720 @defun -drop-last (n list) 721 Return a copy of @var{list} without its last @var{n} items. 722 Return a copy of @var{list} if @var{n} is zero or less. 723 Return nil if @var{list} contains @var{n} items or fewer. 724 725 See also: @code{-drop} (@pxref{-drop}). 726 727 @example 728 @group 729 (-drop-last 3 '(1 2 3 4 5)) 730 @result{} (1 2) 731 @end group 732 @group 733 (-drop-last 17 '(1 2 3 4 5)) 734 @result{} () 735 @end group 736 @group 737 (-drop-last 0 '(1 2 3 4 5)) 738 @result{} (1 2 3 4 5) 739 @end group 740 @end example 741 @end defun 742 743 @anchor{-take-while} 744 @defun -take-while (pred list) 745 Take successive items from @var{list} for which @var{pred} returns non-nil. 746 @var{pred} is a function of one argument. Return a new list of the 747 successive elements from the start of @var{list} for which @var{pred} returns 748 non-nil. 749 750 This function's anaphoric counterpart is @code{--take-while}. 751 752 For another variant, see also @code{-drop-while} (@pxref{-drop-while}). 753 754 @example 755 @group 756 (-take-while #'even? '(1 2 3 4)) 757 @result{} () 758 @end group 759 @group 760 (-take-while #'even? '(2 4 5 6)) 761 @result{} (2 4) 762 @end group 763 @group 764 (--take-while (< it 4) '(1 2 3 4 3 2 1)) 765 @result{} (1 2 3) 766 @end group 767 @end example 768 @end defun 769 770 @anchor{-drop-while} 771 @defun -drop-while (pred list) 772 Drop successive items from @var{list} for which @var{pred} returns non-nil. 773 @var{pred} is a function of one argument. Return the tail (not a copy) 774 of @var{list} starting from its first element for which @var{pred} returns 775 nil. 776 777 This function's anaphoric counterpart is @code{--drop-while}. 778 779 For another variant, see also @code{-take-while} (@pxref{-take-while}). 780 781 @example 782 @group 783 (-drop-while #'even? '(1 2 3 4)) 784 @result{} (1 2 3 4) 785 @end group 786 @group 787 (-drop-while #'even? '(2 4 5 6)) 788 @result{} (5 6) 789 @end group 790 @group 791 (--drop-while (< it 4) '(1 2 3 4 3 2 1)) 792 @result{} (4 3 2 1) 793 @end group 794 @end example 795 @end defun 796 797 @anchor{-select-by-indices} 798 @defun -select-by-indices (indices list) 799 Return a list whose elements are elements from @var{list} selected 800 as `(nth i list)` for all i from @var{indices}. 801 802 @example 803 @group 804 (-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p" "t" "o" "r")) 805 @result{} ("c" "o" "l" "o" "r") 806 @end group 807 @group 808 (-select-by-indices '(2 1 0) '("a" "b" "c")) 809 @result{} ("c" "b" "a") 810 @end group 811 @group 812 (-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) 813 @result{} ("f" "a" "r" "f" "a" "l" "l" "a") 814 @end group 815 @end example 816 @end defun 817 818 @anchor{-select-columns} 819 @defun -select-columns (columns table) 820 Select @var{columns} from @var{table}. 821 822 @var{table} is a list of lists where each element represents one row. 823 It is assumed each row has the same length. 824 825 Each row is transformed such that only the specified @var{columns} are 826 selected. 827 828 See also: @code{-select-column} (@pxref{-select-column}), @code{-select-by-indices} (@pxref{-select-by-indices}) 829 830 @example 831 @group 832 (-select-columns '(0 2) '((1 2 3) (a b c) (:a :b :c))) 833 @result{} ((1 3) (a c) (:a :c)) 834 @end group 835 @group 836 (-select-columns '(1) '((1 2 3) (a b c) (:a :b :c))) 837 @result{} ((2) (b) (:b)) 838 @end group 839 @group 840 (-select-columns nil '((1 2 3) (a b c) (:a :b :c))) 841 @result{} (nil nil nil) 842 @end group 843 @end example 844 @end defun 845 846 @anchor{-select-column} 847 @defun -select-column (column table) 848 Select @var{column} from @var{table}. 849 850 @var{table} is a list of lists where each element represents one row. 851 It is assumed each row has the same length. 852 853 The single selected column is returned as a list. 854 855 See also: @code{-select-columns} (@pxref{-select-columns}), @code{-select-by-indices} (@pxref{-select-by-indices}) 856 857 @example 858 @group 859 (-select-column 1 '((1 2 3) (a b c) (:a :b :c))) 860 @result{} (2 b :b) 861 @end group 862 @end example 863 @end defun 864 865 @node List to list 866 @section List to list 867 868 Functions returning a modified copy of the input list. 869 870 @anchor{-keep} 871 @defun -keep (fn list) 872 Return a new list of the non-nil results of applying @var{fn} to each item in @var{list}. 873 Like @code{-filter} (@pxref{-filter}), but returns the non-nil results of @var{fn} instead of 874 the corresponding elements of @var{list}. 875 876 Its anaphoric counterpart is @code{--keep}. 877 878 @example 879 @group 880 (-keep #'cdr '((1 2 3) (4 5) (6))) 881 @result{} ((2 3) (5)) 882 @end group 883 @group 884 (-keep (lambda (n) (and (> n 3) (* 10 n))) '(1 2 3 4 5 6)) 885 @result{} (40 50 60) 886 @end group 887 @group 888 (--keep (and (> it 3) (* 10 it)) '(1 2 3 4 5 6)) 889 @result{} (40 50 60) 890 @end group 891 @end example 892 @end defun 893 894 @anchor{-concat} 895 @defun -concat (&rest lists) 896 Return a new list with the concatenation of the elements in the supplied @var{lists}. 897 898 @example 899 @group 900 (-concat '(1)) 901 @result{} (1) 902 @end group 903 @group 904 (-concat '(1) '(2)) 905 @result{} (1 2) 906 @end group 907 @group 908 (-concat '(1) '(2 3) '(4)) 909 @result{} (1 2 3 4) 910 @end group 911 @end example 912 @end defun 913 914 @anchor{-flatten} 915 @defun -flatten (l) 916 Take a nested list @var{l} and return its contents as a single, flat list. 917 918 Note that because @code{nil} represents a list of zero elements (an 919 empty list), any mention of nil in @var{l} will disappear after 920 flattening. If you need to preserve nils, consider @code{-flatten-n} (@pxref{-flatten-n}) 921 or map them to some unique symbol and then map them back. 922 923 Conses of two atoms are considered "terminals", that is, they 924 aren't flattened further. 925 926 See also: @code{-flatten-n} (@pxref{-flatten-n}) 927 928 @example 929 @group 930 (-flatten '((1))) 931 @result{} (1) 932 @end group 933 @group 934 (-flatten '((1 (2 3) (((4 (5))))))) 935 @result{} (1 2 3 4 5) 936 @end group 937 @group 938 (-flatten '(1 2 (3 . 4))) 939 @result{} (1 2 (3 . 4)) 940 @end group 941 @end example 942 @end defun 943 944 @anchor{-flatten-n} 945 @defun -flatten-n (num list) 946 Flatten @var{num} levels of a nested @var{list}. 947 948 See also: @code{-flatten} (@pxref{-flatten}) 949 950 @example 951 @group 952 (-flatten-n 1 '((1 2) ((3 4) ((5 6))))) 953 @result{} (1 2 (3 4) ((5 6))) 954 @end group 955 @group 956 (-flatten-n 2 '((1 2) ((3 4) ((5 6))))) 957 @result{} (1 2 3 4 (5 6)) 958 @end group 959 @group 960 (-flatten-n 3 '((1 2) ((3 4) ((5 6))))) 961 @result{} (1 2 3 4 5 6) 962 @end group 963 @end example 964 @end defun 965 966 @anchor{-replace} 967 @defun -replace (old new list) 968 Replace all @var{old} items in @var{list} with @var{new}. 969 970 Elements are compared using @code{equal}. 971 972 See also: @code{-replace-at} (@pxref{-replace-at}) 973 974 @example 975 @group 976 (-replace 1 "1" '(1 2 3 4 3 2 1)) 977 @result{} ("1" 2 3 4 3 2 "1") 978 @end group 979 @group 980 (-replace "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo")) 981 @result{} ("a" "nice" "bar" "sentence" "about" "bar") 982 @end group 983 @group 984 (-replace 1 2 nil) 985 @result{} nil 986 @end group 987 @end example 988 @end defun 989 990 @anchor{-replace-first} 991 @defun -replace-first (old new list) 992 Replace the first occurrence of @var{old} with @var{new} in @var{list}. 993 994 Elements are compared using @code{equal}. 995 996 See also: @code{-map-first} (@pxref{-map-first}) 997 998 @example 999 @group 1000 (-replace-first 1 "1" '(1 2 3 4 3 2 1)) 1001 @result{} ("1" 2 3 4 3 2 1) 1002 @end group 1003 @group 1004 (-replace-first "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo")) 1005 @result{} ("a" "nice" "bar" "sentence" "about" "foo") 1006 @end group 1007 @group 1008 (-replace-first 1 2 nil) 1009 @result{} nil 1010 @end group 1011 @end example 1012 @end defun 1013 1014 @anchor{-replace-last} 1015 @defun -replace-last (old new list) 1016 Replace the last occurrence of @var{old} with @var{new} in @var{list}. 1017 1018 Elements are compared using @code{equal}. 1019 1020 See also: @code{-map-last} (@pxref{-map-last}) 1021 1022 @example 1023 @group 1024 (-replace-last 1 "1" '(1 2 3 4 3 2 1)) 1025 @result{} (1 2 3 4 3 2 "1") 1026 @end group 1027 @group 1028 (-replace-last "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo")) 1029 @result{} ("a" "nice" "foo" "sentence" "about" "bar") 1030 @end group 1031 @group 1032 (-replace-last 1 2 nil) 1033 @result{} nil 1034 @end group 1035 @end example 1036 @end defun 1037 1038 @anchor{-insert-at} 1039 @defun -insert-at (n x list) 1040 Return a list with @var{x} inserted into @var{list} at position @var{n}. 1041 1042 See also: @code{-splice} (@pxref{-splice}), @code{-splice-list} (@pxref{-splice-list}) 1043 1044 @example 1045 @group 1046 (-insert-at 1 'x '(a b c)) 1047 @result{} (a x b c) 1048 @end group 1049 @group 1050 (-insert-at 12 'x '(a b c)) 1051 @result{} (a b c x) 1052 @end group 1053 @end example 1054 @end defun 1055 1056 @anchor{-replace-at} 1057 @defun -replace-at (n x list) 1058 Return a list with element at Nth position in @var{list} replaced with @var{x}. 1059 1060 See also: @code{-replace} (@pxref{-replace}) 1061 1062 @example 1063 @group 1064 (-replace-at 0 9 '(0 1 2 3 4 5)) 1065 @result{} (9 1 2 3 4 5) 1066 @end group 1067 @group 1068 (-replace-at 1 9 '(0 1 2 3 4 5)) 1069 @result{} (0 9 2 3 4 5) 1070 @end group 1071 @group 1072 (-replace-at 4 9 '(0 1 2 3 4 5)) 1073 @result{} (0 1 2 3 9 5) 1074 @end group 1075 @end example 1076 @end defun 1077 1078 @anchor{-update-at} 1079 @defun -update-at (n func list) 1080 Return a list with element at Nth position in @var{list} replaced with `(func (nth n list))`. 1081 1082 See also: @code{-map-when} (@pxref{-map-when}) 1083 1084 @example 1085 @group 1086 (-update-at 0 (lambda (x) (+ x 9)) '(0 1 2 3 4 5)) 1087 @result{} (9 1 2 3 4 5) 1088 @end group 1089 @group 1090 (-update-at 1 (lambda (x) (+ x 8)) '(0 1 2 3 4 5)) 1091 @result{} (0 9 2 3 4 5) 1092 @end group 1093 @group 1094 (--update-at 2 (length it) '("foo" "bar" "baz" "quux")) 1095 @result{} ("foo" "bar" 3 "quux") 1096 @end group 1097 @end example 1098 @end defun 1099 1100 @anchor{-remove-at} 1101 @defun -remove-at (n list) 1102 Return a list with element at Nth position in @var{list} removed. 1103 1104 See also: @code{-remove-at-indices} (@pxref{-remove-at-indices}), @code{-remove} (@pxref{-remove}) 1105 1106 @example 1107 @group 1108 (-remove-at 0 '("0" "1" "2" "3" "4" "5")) 1109 @result{} ("1" "2" "3" "4" "5") 1110 @end group 1111 @group 1112 (-remove-at 1 '("0" "1" "2" "3" "4" "5")) 1113 @result{} ("0" "2" "3" "4" "5") 1114 @end group 1115 @group 1116 (-remove-at 2 '("0" "1" "2" "3" "4" "5")) 1117 @result{} ("0" "1" "3" "4" "5") 1118 @end group 1119 @end example 1120 @end defun 1121 1122 @anchor{-remove-at-indices} 1123 @defun -remove-at-indices (indices list) 1124 Return a list whose elements are elements from @var{list} without 1125 elements selected as `(nth i list)` for all i 1126 from @var{indices}. 1127 1128 See also: @code{-remove-at} (@pxref{-remove-at}), @code{-remove} (@pxref{-remove}) 1129 1130 @example 1131 @group 1132 (-remove-at-indices '(0) '("0" "1" "2" "3" "4" "5")) 1133 @result{} ("1" "2" "3" "4" "5") 1134 @end group 1135 @group 1136 (-remove-at-indices '(0 2 4) '("0" "1" "2" "3" "4" "5")) 1137 @result{} ("1" "3" "5") 1138 @end group 1139 @group 1140 (-remove-at-indices '(0 5) '("0" "1" "2" "3" "4" "5")) 1141 @result{} ("1" "2" "3" "4") 1142 @end group 1143 @end example 1144 @end defun 1145 1146 @node Reductions 1147 @section Reductions 1148 1149 Functions reducing lists to a single value (which may also be a list). 1150 1151 @anchor{-reduce-from} 1152 @defun -reduce-from (fn init list) 1153 Reduce the function @var{fn} across @var{list}, starting with @var{init}. 1154 Return the result of applying @var{fn} to @var{init} and the first element of 1155 @var{list}, then applying @var{fn} to that result and the second element, 1156 etc. If @var{list} is empty, return @var{init} without calling @var{fn}. 1157 1158 This function's anaphoric counterpart is @code{--reduce-from}. 1159 1160 For other folds, see also @code{-reduce} (@pxref{-reduce}) and @code{-reduce-r} (@pxref{-reduce-r}). 1161 1162 @example 1163 @group 1164 (-reduce-from #'- 10 '(1 2 3)) 1165 @result{} 4 1166 @end group 1167 @group 1168 (-reduce-from #'list 10 '(1 2 3)) 1169 @result{} (((10 1) 2) 3) 1170 @end group 1171 @group 1172 (--reduce-from (concat acc " " it) "START" '("a" "b" "c")) 1173 @result{} "START a b c" 1174 @end group 1175 @end example 1176 @end defun 1177 1178 @anchor{-reduce-r-from} 1179 @defun -reduce-r-from (fn init list) 1180 Reduce the function @var{fn} across @var{list} in reverse, starting with @var{init}. 1181 Return the result of applying @var{fn} to the last element of @var{list} and 1182 @var{init}, then applying @var{fn} to the second-to-last element and the 1183 previous result of @var{fn}, etc. That is, the first argument of @var{fn} is 1184 the current element, and its second argument the accumulated 1185 value. If @var{list} is empty, return @var{init} without calling @var{fn}. 1186 1187 This function is like @code{-reduce-from} (@pxref{-reduce-from}) but the operation associates 1188 from the right rather than left. In other words, it starts from 1189 the end of @var{list} and flips the arguments to @var{fn}. Conceptually, it 1190 is like replacing the conses in @var{list} with applications of @var{fn}, and 1191 its last link with @var{init}, and evaluating the resulting expression. 1192 1193 This function's anaphoric counterpart is @code{--reduce-r-from}. 1194 1195 For other folds, see also @code{-reduce-r} (@pxref{-reduce-r}) and @code{-reduce} (@pxref{-reduce}). 1196 1197 @example 1198 @group 1199 (-reduce-r-from #'- 10 '(1 2 3)) 1200 @result{} -8 1201 @end group 1202 @group 1203 (-reduce-r-from #'list 10 '(1 2 3)) 1204 @result{} (1 (2 (3 10))) 1205 @end group 1206 @group 1207 (--reduce-r-from (concat it " " acc) "END" '("a" "b" "c")) 1208 @result{} "a b c END" 1209 @end group 1210 @end example 1211 @end defun 1212 1213 @anchor{-reduce} 1214 @defun -reduce (fn list) 1215 Reduce the function @var{fn} across @var{list}. 1216 Return the result of applying @var{fn} to the first two elements of 1217 @var{list}, then applying @var{fn} to that result and the third element, etc. 1218 If @var{list} contains a single element, return it without calling @var{fn}. 1219 If @var{list} is empty, return the result of calling @var{fn} with no 1220 arguments. 1221 1222 This function's anaphoric counterpart is @code{--reduce}. 1223 1224 For other folds, see also @code{-reduce-from} (@pxref{-reduce-from}) and @code{-reduce-r} (@pxref{-reduce-r}). 1225 1226 @example 1227 @group 1228 (-reduce #'- '(1 2 3 4)) 1229 @result{} -8 1230 @end group 1231 @group 1232 (-reduce #'list '(1 2 3 4)) 1233 @result{} (((1 2) 3) 4) 1234 @end group 1235 @group 1236 (--reduce (format "%s-%d" acc it) '(1 2 3)) 1237 @result{} "1-2-3" 1238 @end group 1239 @end example 1240 @end defun 1241 1242 @anchor{-reduce-r} 1243 @defun -reduce-r (fn list) 1244 Reduce the function @var{fn} across @var{list} in reverse. 1245 Return the result of applying @var{fn} to the last two elements of 1246 @var{list}, then applying @var{fn} to the third-to-last element and the 1247 previous result of @var{fn}, etc. That is, the first argument of @var{fn} is 1248 the current element, and its second argument the accumulated 1249 value. If @var{list} contains a single element, return it without 1250 calling @var{fn}. If @var{list} is empty, return the result of calling @var{fn} 1251 with no arguments. 1252 1253 This function is like @code{-reduce} (@pxref{-reduce}) but the operation associates from 1254 the right rather than left. In other words, it starts from the 1255 end of @var{list} and flips the arguments to @var{fn}. Conceptually, it is 1256 like replacing the conses in @var{list} with applications of @var{fn}, 1257 ignoring its last link, and evaluating the resulting expression. 1258 1259 This function's anaphoric counterpart is @code{--reduce-r}. 1260 1261 For other folds, see also @code{-reduce-r-from} (@pxref{-reduce-r-from}) and @code{-reduce} (@pxref{-reduce}). 1262 1263 @example 1264 @group 1265 (-reduce-r #'- '(1 2 3 4)) 1266 @result{} -2 1267 @end group 1268 @group 1269 (-reduce-r #'list '(1 2 3 4)) 1270 @result{} (1 (2 (3 4))) 1271 @end group 1272 @group 1273 (--reduce-r (format "%s-%d" acc it) '(1 2 3)) 1274 @result{} "3-2-1" 1275 @end group 1276 @end example 1277 @end defun 1278 1279 @anchor{-reductions-from} 1280 @defun -reductions-from (fn init list) 1281 Return a list of @var{fn}'s intermediate reductions across @var{list}. 1282 That is, a list of the intermediate values of the accumulator 1283 when @code{-reduce-from} (@pxref{-reduce-from}) (which see) is called with the same 1284 arguments. 1285 1286 This function's anaphoric counterpart is @code{--reductions-from}. 1287 1288 For other folds, see also @code{-reductions} (@pxref{-reductions}) and @code{-reductions-r} (@pxref{-reductions-r}). 1289 1290 @example 1291 @group 1292 (-reductions-from #'max 0 '(2 1 4 3)) 1293 @result{} (0 2 2 4 4) 1294 @end group 1295 @group 1296 (-reductions-from #'* 1 '(1 2 3 4)) 1297 @result{} (1 1 2 6 24) 1298 @end group 1299 @group 1300 (--reductions-from (format "(FN %s %d)" acc it) "INIT" '(1 2 3)) 1301 @result{} ("INIT" "(FN INIT 1)" "(FN (FN INIT 1) 2)" "(FN (FN (FN INIT 1) 2) 3)") 1302 @end group 1303 @end example 1304 @end defun 1305 1306 @anchor{-reductions-r-from} 1307 @defun -reductions-r-from (fn init list) 1308 Return a list of @var{fn}'s intermediate reductions across reversed @var{list}. 1309 That is, a list of the intermediate values of the accumulator 1310 when @code{-reduce-r-from} (@pxref{-reduce-r-from}) (which see) is called with the same 1311 arguments. 1312 1313 This function's anaphoric counterpart is @code{--reductions-r-from}. 1314 1315 For other folds, see also @code{-reductions} (@pxref{-reductions}) and @code{-reductions-r} (@pxref{-reductions-r}). 1316 1317 @example 1318 @group 1319 (-reductions-r-from #'max 0 '(2 1 4 3)) 1320 @result{} (4 4 4 3 0) 1321 @end group 1322 @group 1323 (-reductions-r-from #'* 1 '(1 2 3 4)) 1324 @result{} (24 24 12 4 1) 1325 @end group 1326 @group 1327 (--reductions-r-from (format "(FN %d %s)" it acc) "INIT" '(1 2 3)) 1328 @result{} ("(FN 1 (FN 2 (FN 3 INIT)))" "(FN 2 (FN 3 INIT))" "(FN 3 INIT)" "INIT") 1329 @end group 1330 @end example 1331 @end defun 1332 1333 @anchor{-reductions} 1334 @defun -reductions (fn list) 1335 Return a list of @var{fn}'s intermediate reductions across @var{list}. 1336 That is, a list of the intermediate values of the accumulator 1337 when @code{-reduce} (@pxref{-reduce}) (which see) is called with the same arguments. 1338 1339 This function's anaphoric counterpart is @code{--reductions}. 1340 1341 For other folds, see also @code{-reductions} (@pxref{-reductions}) and @code{-reductions-r} (@pxref{-reductions-r}). 1342 1343 @example 1344 @group 1345 (-reductions #'+ '(1 2 3 4)) 1346 @result{} (1 3 6 10) 1347 @end group 1348 @group 1349 (-reductions #'* '(1 2 3 4)) 1350 @result{} (1 2 6 24) 1351 @end group 1352 @group 1353 (--reductions (format "(FN %s %d)" acc it) '(1 2 3)) 1354 @result{} (1 "(FN 1 2)" "(FN (FN 1 2) 3)") 1355 @end group 1356 @end example 1357 @end defun 1358 1359 @anchor{-reductions-r} 1360 @defun -reductions-r (fn list) 1361 Return a list of @var{fn}'s intermediate reductions across reversed @var{list}. 1362 That is, a list of the intermediate values of the accumulator 1363 when @code{-reduce-r} (@pxref{-reduce-r}) (which see) is called with the same arguments. 1364 1365 This function's anaphoric counterpart is @code{--reductions-r}. 1366 1367 For other folds, see also @code{-reductions-r-from} (@pxref{-reductions-r-from}) and 1368 @code{-reductions} (@pxref{-reductions}). 1369 1370 @example 1371 @group 1372 (-reductions-r #'+ '(1 2 3 4)) 1373 @result{} (10 9 7 4) 1374 @end group 1375 @group 1376 (-reductions-r #'* '(1 2 3 4)) 1377 @result{} (24 24 12 4) 1378 @end group 1379 @group 1380 (--reductions-r (format "(FN %d %s)" it acc) '(1 2 3)) 1381 @result{} ("(FN 1 (FN 2 3))" "(FN 2 3)" 3) 1382 @end group 1383 @end example 1384 @end defun 1385 1386 @anchor{-count} 1387 @defun -count (pred list) 1388 Counts the number of items in @var{list} where (@var{pred} item) is non-nil. 1389 1390 @example 1391 @group 1392 (-count 'even? '(1 2 3 4 5)) 1393 @result{} 2 1394 @end group 1395 @group 1396 (--count (< it 4) '(1 2 3 4)) 1397 @result{} 3 1398 @end group 1399 @end example 1400 @end defun 1401 1402 @anchor{-sum} 1403 @defun -sum (list) 1404 Return the sum of @var{list}. 1405 1406 @example 1407 @group 1408 (-sum ()) 1409 @result{} 0 1410 @end group 1411 @group 1412 (-sum '(1)) 1413 @result{} 1 1414 @end group 1415 @group 1416 (-sum '(1 2 3 4)) 1417 @result{} 10 1418 @end group 1419 @end example 1420 @end defun 1421 1422 @anchor{-running-sum} 1423 @defun -running-sum (list) 1424 Return a list with running sums of items in @var{list}. 1425 @var{list} must be non-empty. 1426 1427 @example 1428 @group 1429 (-running-sum '(1 2 3 4)) 1430 @result{} (1 3 6 10) 1431 @end group 1432 @group 1433 (-running-sum '(1)) 1434 @result{} (1) 1435 @end group 1436 @group 1437 (-running-sum ()) 1438 @error{} Wrong type argument: consp, nil 1439 @end group 1440 @end example 1441 @end defun 1442 1443 @anchor{-product} 1444 @defun -product (list) 1445 Return the product of @var{list}. 1446 1447 @example 1448 @group 1449 (-product ()) 1450 @result{} 1 1451 @end group 1452 @group 1453 (-product '(1)) 1454 @result{} 1 1455 @end group 1456 @group 1457 (-product '(1 2 3 4)) 1458 @result{} 24 1459 @end group 1460 @end example 1461 @end defun 1462 1463 @anchor{-running-product} 1464 @defun -running-product (list) 1465 Return a list with running products of items in @var{list}. 1466 @var{list} must be non-empty. 1467 1468 @example 1469 @group 1470 (-running-product '(1 2 3 4)) 1471 @result{} (1 2 6 24) 1472 @end group 1473 @group 1474 (-running-product '(1)) 1475 @result{} (1) 1476 @end group 1477 @group 1478 (-running-product ()) 1479 @error{} Wrong type argument: consp, nil 1480 @end group 1481 @end example 1482 @end defun 1483 1484 @anchor{-inits} 1485 @defun -inits (list) 1486 Return all prefixes of @var{list}. 1487 1488 @example 1489 @group 1490 (-inits '(1 2 3 4)) 1491 @result{} (nil (1) (1 2) (1 2 3) (1 2 3 4)) 1492 @end group 1493 @group 1494 (-inits nil) 1495 @result{} (nil) 1496 @end group 1497 @group 1498 (-inits '(1)) 1499 @result{} (nil (1)) 1500 @end group 1501 @end example 1502 @end defun 1503 1504 @anchor{-tails} 1505 @defun -tails (list) 1506 Return all suffixes of @var{list} 1507 1508 @example 1509 @group 1510 (-tails '(1 2 3 4)) 1511 @result{} ((1 2 3 4) (2 3 4) (3 4) (4) nil) 1512 @end group 1513 @group 1514 (-tails nil) 1515 @result{} (nil) 1516 @end group 1517 @group 1518 (-tails '(1)) 1519 @result{} ((1) nil) 1520 @end group 1521 @end example 1522 @end defun 1523 1524 @anchor{-common-prefix} 1525 @defun -common-prefix (&rest lists) 1526 Return the longest common prefix of @var{lists}. 1527 1528 @example 1529 @group 1530 (-common-prefix '(1)) 1531 @result{} (1) 1532 @end group 1533 @group 1534 (-common-prefix '(1 2) '(3 4) '(1 2)) 1535 @result{} () 1536 @end group 1537 @group 1538 (-common-prefix '(1 2) '(1 2 3) '(1 2 3 4)) 1539 @result{} (1 2) 1540 @end group 1541 @end example 1542 @end defun 1543 1544 @anchor{-common-suffix} 1545 @defun -common-suffix (&rest lists) 1546 Return the longest common suffix of @var{lists}. 1547 1548 @example 1549 @group 1550 (-common-suffix '(1)) 1551 @result{} (1) 1552 @end group 1553 @group 1554 (-common-suffix '(1 2) '(3 4) '(1 2)) 1555 @result{} () 1556 @end group 1557 @group 1558 (-common-suffix '(1 2 3 4) '(2 3 4) '(3 4)) 1559 @result{} (3 4) 1560 @end group 1561 @end example 1562 @end defun 1563 1564 @anchor{-min} 1565 @defun -min (list) 1566 Return the smallest value from @var{list} of numbers or markers. 1567 1568 @example 1569 @group 1570 (-min '(0)) 1571 @result{} 0 1572 @end group 1573 @group 1574 (-min '(3 2 1)) 1575 @result{} 1 1576 @end group 1577 @group 1578 (-min '(1 2 3)) 1579 @result{} 1 1580 @end group 1581 @end example 1582 @end defun 1583 1584 @anchor{-min-by} 1585 @defun -min-by (comparator list) 1586 Take a comparison function @var{comparator} and a @var{list} and return 1587 the least element of the list by the comparison function. 1588 1589 See also combinator @code{-on} (@pxref{-on}) which can transform the values before 1590 comparing them. 1591 1592 @example 1593 @group 1594 (-min-by '> '(4 3 6 1)) 1595 @result{} 1 1596 @end group 1597 @group 1598 (--min-by (> (car it) (car other)) '((1 2 3) (2) (3 2))) 1599 @result{} (1 2 3) 1600 @end group 1601 @group 1602 (--min-by (> (length it) (length other)) '((1 2 3) (2) (3 2))) 1603 @result{} (2) 1604 @end group 1605 @end example 1606 @end defun 1607 1608 @anchor{-max} 1609 @defun -max (list) 1610 Return the largest value from @var{list} of numbers or markers. 1611 1612 @example 1613 @group 1614 (-max '(0)) 1615 @result{} 0 1616 @end group 1617 @group 1618 (-max '(3 2 1)) 1619 @result{} 3 1620 @end group 1621 @group 1622 (-max '(1 2 3)) 1623 @result{} 3 1624 @end group 1625 @end example 1626 @end defun 1627 1628 @anchor{-max-by} 1629 @defun -max-by (comparator list) 1630 Take a comparison function @var{comparator} and a @var{list} and return 1631 the greatest element of the list by the comparison function. 1632 1633 See also combinator @code{-on} (@pxref{-on}) which can transform the values before 1634 comparing them. 1635 1636 @example 1637 @group 1638 (-max-by '> '(4 3 6 1)) 1639 @result{} 6 1640 @end group 1641 @group 1642 (--max-by (> (car it) (car other)) '((1 2 3) (2) (3 2))) 1643 @result{} (3 2) 1644 @end group 1645 @group 1646 (--max-by (> (length it) (length other)) '((1 2 3) (2) (3 2))) 1647 @result{} (1 2 3) 1648 @end group 1649 @end example 1650 @end defun 1651 1652 @node Unfolding 1653 @section Unfolding 1654 1655 Operations dual to reductions, building lists from a seed 1656 value rather than consuming a list to produce a single value. 1657 1658 @anchor{-iterate} 1659 @defun -iterate (fun init n) 1660 Return a list of iterated applications of @var{fun} to @var{init}. 1661 1662 This means a list of the form: 1663 1664 (@var{init} (@var{fun} @var{init}) (@var{fun} (@var{fun} @var{init})) @dots{}) 1665 1666 @var{n} is the length of the returned list. 1667 1668 @example 1669 @group 1670 (-iterate #'1+ 1 10) 1671 @result{} (1 2 3 4 5 6 7 8 9 10) 1672 @end group 1673 @group 1674 (-iterate (lambda (x) (+ x x)) 2 5) 1675 @result{} (2 4 8 16 32) 1676 @end group 1677 @group 1678 (--iterate (* it it) 2 5) 1679 @result{} (2 4 16 256 65536) 1680 @end group 1681 @end example 1682 @end defun 1683 1684 @anchor{-unfold} 1685 @defun -unfold (fun seed) 1686 Build a list from @var{seed} using @var{fun}. 1687 1688 This is "dual" operation to @code{-reduce-r} (@pxref{-reduce-r}): while -reduce-r 1689 consumes a list to produce a single value, @code{-unfold} (@pxref{-unfold}) takes a 1690 seed value and builds a (potentially infinite!) list. 1691 1692 @var{fun} should return @code{nil} to stop the generating process, or a 1693 cons (@var{a} . @var{b}), where @var{a} will be prepended to the result and @var{b} is 1694 the new seed. 1695 1696 @example 1697 @group 1698 (-unfold (lambda (x) (unless (= x 0) (cons x (1- x)))) 10) 1699 @result{} (10 9 8 7 6 5 4 3 2 1) 1700 @end group 1701 @group 1702 (--unfold (when it (cons it (cdr it))) '(1 2 3 4)) 1703 @result{} ((1 2 3 4) (2 3 4) (3 4) (4)) 1704 @end group 1705 @group 1706 (--unfold (when it (cons it (butlast it))) '(1 2 3 4)) 1707 @result{} ((1 2 3 4) (1 2 3) (1 2) (1)) 1708 @end group 1709 @end example 1710 @end defun 1711 1712 @node Predicates 1713 @section Predicates 1714 1715 Reductions of one or more lists to a boolean value. 1716 1717 @anchor{-some} 1718 @defun -some (pred list) 1719 Return (@var{pred} x) for the first @var{list} item where (@var{pred} x) is non-nil, else nil. 1720 1721 Alias: @code{-any}. 1722 1723 This function's anaphoric counterpart is @code{--some}. 1724 1725 @example 1726 @group 1727 (-some #'stringp '(1 "2" 3)) 1728 @result{} t 1729 @end group 1730 @group 1731 (--some (string-match-p "x" it) '("foo" "axe" "xor")) 1732 @result{} 1 1733 @end group 1734 @group 1735 (--some (= it-index 3) '(0 1 2)) 1736 @result{} nil 1737 @end group 1738 @end example 1739 @end defun 1740 1741 @anchor{-every} 1742 @defun -every (pred list) 1743 Return non-nil if @var{pred} returns non-nil for all items in @var{list}. 1744 If so, return the last such result of @var{pred}. Otherwise, once an 1745 item is reached for which @var{pred} returns nil, return nil without 1746 calling @var{pred} on any further @var{list} elements. 1747 1748 This function is like @code{-every-p}, but on success returns the last 1749 non-nil result of @var{pred} instead of just t. 1750 1751 This function's anaphoric counterpart is @code{--every}. 1752 1753 @example 1754 @group 1755 (-every #'numberp '(1 2 3)) 1756 @result{} t 1757 @end group 1758 @group 1759 (--every (string-match-p "x" it) '("axe" "xor")) 1760 @result{} 0 1761 @end group 1762 @group 1763 (--every (= it it-index) '(0 1 3)) 1764 @result{} nil 1765 @end group 1766 @end example 1767 @end defun 1768 1769 @anchor{-any?} 1770 @defun -any? (pred list) 1771 Return t if (@var{pred} x) is non-nil for any x in @var{list}, else nil. 1772 1773 Alias: @code{-any-p}, @code{-some?}, @code{-some-p} 1774 1775 @example 1776 @group 1777 (-any? #'numberp '(nil 0 t)) 1778 @result{} t 1779 @end group 1780 @group 1781 (-any? #'numberp '(nil t t)) 1782 @result{} nil 1783 @end group 1784 @group 1785 (-any? #'null '(1 3 5)) 1786 @result{} nil 1787 @end group 1788 @end example 1789 @end defun 1790 1791 @anchor{-all?} 1792 @defun -all? (pred list) 1793 Return t if (@var{pred} @var{x}) is non-nil for all @var{x} in @var{list}, else nil. 1794 In the latter case, stop after the first @var{x} for which (@var{pred} @var{x}) is 1795 nil, without calling @var{pred} on any subsequent elements of @var{list}. 1796 1797 The similar function @code{-every} (@pxref{-every}) is more widely useful, since it 1798 returns the last non-nil result of @var{pred} instead of just t on 1799 success. 1800 1801 Alias: @code{-all-p}, @code{-every-p}, @code{-every?}. 1802 1803 This function's anaphoric counterpart is @code{--all?}. 1804 1805 @example 1806 @group 1807 (-all? #'numberp '(1 2 3)) 1808 @result{} t 1809 @end group 1810 @group 1811 (-all? #'numberp '(2 t 6)) 1812 @result{} nil 1813 @end group 1814 @group 1815 (--all? (= 0 (% it 2)) '(2 4 6)) 1816 @result{} t 1817 @end group 1818 @end example 1819 @end defun 1820 1821 @anchor{-none?} 1822 @defun -none? (pred list) 1823 Return t if (@var{pred} x) is nil for all x in @var{list}, else nil. 1824 1825 Alias: @code{-none-p} 1826 1827 @example 1828 @group 1829 (-none? 'even? '(1 2 3)) 1830 @result{} nil 1831 @end group 1832 @group 1833 (-none? 'even? '(1 3 5)) 1834 @result{} t 1835 @end group 1836 @group 1837 (--none? (= 0 (% it 2)) '(1 2 3)) 1838 @result{} nil 1839 @end group 1840 @end example 1841 @end defun 1842 1843 @anchor{-only-some?} 1844 @defun -only-some? (pred list) 1845 Return `t` if at least one item of @var{list} matches @var{pred} and at least one item of @var{list} does not match @var{pred}. 1846 Return `nil` both if all items match the predicate or if none of the items match the predicate. 1847 1848 Alias: @code{-only-some-p} 1849 1850 @example 1851 @group 1852 (-only-some? 'even? '(1 2 3)) 1853 @result{} t 1854 @end group 1855 @group 1856 (-only-some? 'even? '(1 3 5)) 1857 @result{} nil 1858 @end group 1859 @group 1860 (-only-some? 'even? '(2 4 6)) 1861 @result{} nil 1862 @end group 1863 @end example 1864 @end defun 1865 1866 @anchor{-contains?} 1867 @defun -contains? (list element) 1868 Return non-nil if @var{list} contains @var{element}. 1869 1870 The test for equality is done with @code{equal}, or with @code{-compare-fn} 1871 if that's non-nil. 1872 1873 Alias: @code{-contains-p} 1874 1875 @example 1876 @group 1877 (-contains? '(1 2 3) 1) 1878 @result{} t 1879 @end group 1880 @group 1881 (-contains? '(1 2 3) 2) 1882 @result{} t 1883 @end group 1884 @group 1885 (-contains? '(1 2 3) 4) 1886 @result{} nil 1887 @end group 1888 @end example 1889 @end defun 1890 1891 @anchor{-same-items?} 1892 @defun -same-items? (list list2) 1893 Return true if @var{list} and @var{list2} has the same items. 1894 1895 The order of the elements in the lists does not matter. 1896 1897 Alias: @code{-same-items-p} 1898 1899 @example 1900 @group 1901 (-same-items? '(1 2 3) '(1 2 3)) 1902 @result{} t 1903 @end group 1904 @group 1905 (-same-items? '(1 2 3) '(3 2 1)) 1906 @result{} t 1907 @end group 1908 @group 1909 (-same-items? '(1 2 3) '(1 2 3 4)) 1910 @result{} nil 1911 @end group 1912 @end example 1913 @end defun 1914 1915 @anchor{-is-prefix?} 1916 @defun -is-prefix? (prefix list) 1917 Return non-nil if @var{prefix} is a prefix of @var{list}. 1918 1919 Alias: @code{-is-prefix-p}. 1920 1921 @example 1922 @group 1923 (-is-prefix? '(1 2 3) '(1 2 3 4 5)) 1924 @result{} t 1925 @end group 1926 @group 1927 (-is-prefix? '(1 2 3 4 5) '(1 2 3)) 1928 @result{} nil 1929 @end group 1930 @group 1931 (-is-prefix? '(1 3) '(1 2 3 4 5)) 1932 @result{} nil 1933 @end group 1934 @end example 1935 @end defun 1936 1937 @anchor{-is-suffix?} 1938 @defun -is-suffix? (suffix list) 1939 Return non-nil if @var{suffix} is a suffix of @var{list}. 1940 1941 Alias: @code{-is-suffix-p}. 1942 1943 @example 1944 @group 1945 (-is-suffix? '(3 4 5) '(1 2 3 4 5)) 1946 @result{} t 1947 @end group 1948 @group 1949 (-is-suffix? '(1 2 3 4 5) '(3 4 5)) 1950 @result{} nil 1951 @end group 1952 @group 1953 (-is-suffix? '(3 5) '(1 2 3 4 5)) 1954 @result{} nil 1955 @end group 1956 @end example 1957 @end defun 1958 1959 @anchor{-is-infix?} 1960 @defun -is-infix? (infix list) 1961 Return non-nil if @var{infix} is infix of @var{list}. 1962 1963 This operation runs in @var{o}(n^2) time 1964 1965 Alias: @code{-is-infix-p} 1966 1967 @example 1968 @group 1969 (-is-infix? '(1 2 3) '(1 2 3 4 5)) 1970 @result{} t 1971 @end group 1972 @group 1973 (-is-infix? '(2 3 4) '(1 2 3 4 5)) 1974 @result{} t 1975 @end group 1976 @group 1977 (-is-infix? '(3 4 5) '(1 2 3 4 5)) 1978 @result{} t 1979 @end group 1980 @end example 1981 @end defun 1982 1983 @anchor{-cons-pair?} 1984 @defun -cons-pair? (obj) 1985 Return non-nil if @var{obj} is a true cons pair. 1986 That is, a cons (@var{a} . @var{b}) where @var{b} is not a list. 1987 1988 Alias: @code{-cons-pair-p}. 1989 1990 @example 1991 @group 1992 (-cons-pair? '(1 . 2)) 1993 @result{} t 1994 @end group 1995 @group 1996 (-cons-pair? '(1 2)) 1997 @result{} nil 1998 @end group 1999 @group 2000 (-cons-pair? '(1)) 2001 @result{} nil 2002 @end group 2003 @end example 2004 @end defun 2005 2006 @node Partitioning 2007 @section Partitioning 2008 2009 Functions partitioning the input list into a list of lists. 2010 2011 @anchor{-split-at} 2012 @defun -split-at (n list) 2013 Split @var{list} into two sublists after the Nth element. 2014 The result is a list of two elements (@var{take} @var{drop}) where @var{take} is a 2015 new list of the first @var{n} elements of @var{list}, and @var{drop} is the 2016 remaining elements of @var{list} (not a copy). @var{take} and @var{drop} are like 2017 the results of @code{-take} (@pxref{-take}) and @code{-drop} (@pxref{-drop}), respectively, but the split 2018 is done in a single list traversal. 2019 2020 @example 2021 @group 2022 (-split-at 3 '(1 2 3 4 5)) 2023 @result{} ((1 2 3) (4 5)) 2024 @end group 2025 @group 2026 (-split-at 17 '(1 2 3 4 5)) 2027 @result{} ((1 2 3 4 5) nil) 2028 @end group 2029 @group 2030 (-split-at 0 '(1 2 3 4 5)) 2031 @result{} (nil (1 2 3 4 5)) 2032 @end group 2033 @end example 2034 @end defun 2035 2036 @anchor{-split-with} 2037 @defun -split-with (pred list) 2038 Return a list of ((-take-while @var{pred} @var{list}) (-drop-while @var{pred} @var{list})), in no more than one pass through the list. 2039 2040 @example 2041 @group 2042 (-split-with 'even? '(1 2 3 4)) 2043 @result{} (nil (1 2 3 4)) 2044 @end group 2045 @group 2046 (-split-with 'even? '(2 4 5 6)) 2047 @result{} ((2 4) (5 6)) 2048 @end group 2049 @group 2050 (--split-with (< it 4) '(1 2 3 4 3 2 1)) 2051 @result{} ((1 2 3) (4 3 2 1)) 2052 @end group 2053 @end example 2054 @end defun 2055 2056 @anchor{-split-on} 2057 @defmac -split-on (item list) 2058 Split the @var{list} each time @var{item} is found. 2059 2060 Unlike @code{-partition-by} (@pxref{-partition-by}), the @var{item} is discarded from the results. 2061 Empty lists are also removed from the result. 2062 2063 Comparison is done by @code{equal}. 2064 2065 See also @code{-split-when} (@pxref{-split-when}) 2066 2067 @example 2068 @group 2069 (-split-on '| '(Nil | Leaf a | Node [Tree a])) 2070 @result{} ((Nil) (Leaf a) (Node [Tree a])) 2071 @end group 2072 @group 2073 (-split-on :endgroup '("a" "b" :endgroup "c" :endgroup "d" "e")) 2074 @result{} (("a" "b") ("c") ("d" "e")) 2075 @end group 2076 @group 2077 (-split-on :endgroup '("a" "b" :endgroup :endgroup "d" "e")) 2078 @result{} (("a" "b") ("d" "e")) 2079 @end group 2080 @end example 2081 @end defmac 2082 2083 @anchor{-split-when} 2084 @defun -split-when (fn list) 2085 Split the @var{list} on each element where @var{fn} returns non-nil. 2086 2087 Unlike @code{-partition-by} (@pxref{-partition-by}), the "matched" element is discarded from 2088 the results. Empty lists are also removed from the result. 2089 2090 This function can be thought of as a generalization of 2091 @code{split-string}. 2092 2093 @example 2094 @group 2095 (-split-when 'even? '(1 2 3 4 5 6)) 2096 @result{} ((1) (3) (5)) 2097 @end group 2098 @group 2099 (-split-when 'even? '(1 2 3 4 6 8 9)) 2100 @result{} ((1) (3) (9)) 2101 @end group 2102 @group 2103 (--split-when (memq it '(&optional &rest)) '(a b &optional c d &rest args)) 2104 @result{} ((a b) (c d) (args)) 2105 @end group 2106 @end example 2107 @end defun 2108 2109 @anchor{-separate} 2110 @defun -separate (pred list) 2111 Return a list of ((-filter @var{pred} @var{list}) (-remove @var{pred} @var{list})), in one pass through the list. 2112 2113 @example 2114 @group 2115 (-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7)) 2116 @result{} ((2 4 6) (1 3 5 7)) 2117 @end group 2118 @group 2119 (--separate (< it 5) '(3 7 5 9 3 2 1 4 6)) 2120 @result{} ((3 3 2 1 4) (7 5 9 6)) 2121 @end group 2122 @group 2123 (-separate 'cdr '((1 2) (1) (1 2 3) (4))) 2124 @result{} (((1 2) (1 2 3)) ((1) (4))) 2125 @end group 2126 @end example 2127 @end defun 2128 2129 @anchor{-partition} 2130 @defun -partition (n list) 2131 Return a new list with the items in @var{list} grouped into @var{n}-sized sublists. 2132 If there are not enough items to make the last group @var{n}-sized, 2133 those items are discarded. 2134 2135 @example 2136 @group 2137 (-partition 2 '(1 2 3 4 5 6)) 2138 @result{} ((1 2) (3 4) (5 6)) 2139 @end group 2140 @group 2141 (-partition 2 '(1 2 3 4 5 6 7)) 2142 @result{} ((1 2) (3 4) (5 6)) 2143 @end group 2144 @group 2145 (-partition 3 '(1 2 3 4 5 6 7)) 2146 @result{} ((1 2 3) (4 5 6)) 2147 @end group 2148 @end example 2149 @end defun 2150 2151 @anchor{-partition-all} 2152 @defun -partition-all (n list) 2153 Return a new list with the items in @var{list} grouped into @var{n}-sized sublists. 2154 The last group may contain less than @var{n} items. 2155 2156 @example 2157 @group 2158 (-partition-all 2 '(1 2 3 4 5 6)) 2159 @result{} ((1 2) (3 4) (5 6)) 2160 @end group 2161 @group 2162 (-partition-all 2 '(1 2 3 4 5 6 7)) 2163 @result{} ((1 2) (3 4) (5 6) (7)) 2164 @end group 2165 @group 2166 (-partition-all 3 '(1 2 3 4 5 6 7)) 2167 @result{} ((1 2 3) (4 5 6) (7)) 2168 @end group 2169 @end example 2170 @end defun 2171 2172 @anchor{-partition-in-steps} 2173 @defun -partition-in-steps (n step list) 2174 Return a new list with the items in @var{list} grouped into @var{n}-sized sublists at offsets @var{step} apart. 2175 If there are not enough items to make the last group @var{n}-sized, 2176 those items are discarded. 2177 2178 @example 2179 @group 2180 (-partition-in-steps 2 1 '(1 2 3 4)) 2181 @result{} ((1 2) (2 3) (3 4)) 2182 @end group 2183 @group 2184 (-partition-in-steps 3 2 '(1 2 3 4)) 2185 @result{} ((1 2 3)) 2186 @end group 2187 @group 2188 (-partition-in-steps 3 2 '(1 2 3 4 5)) 2189 @result{} ((1 2 3) (3 4 5)) 2190 @end group 2191 @end example 2192 @end defun 2193 2194 @anchor{-partition-all-in-steps} 2195 @defun -partition-all-in-steps (n step list) 2196 Return a new list with the items in @var{list} grouped into @var{n}-sized sublists at offsets @var{step} apart. 2197 The last groups may contain less than @var{n} items. 2198 2199 @example 2200 @group 2201 (-partition-all-in-steps 2 1 '(1 2 3 4)) 2202 @result{} ((1 2) (2 3) (3 4) (4)) 2203 @end group 2204 @group 2205 (-partition-all-in-steps 3 2 '(1 2 3 4)) 2206 @result{} ((1 2 3) (3 4)) 2207 @end group 2208 @group 2209 (-partition-all-in-steps 3 2 '(1 2 3 4 5)) 2210 @result{} ((1 2 3) (3 4 5) (5)) 2211 @end group 2212 @end example 2213 @end defun 2214 2215 @anchor{-partition-by} 2216 @defun -partition-by (fn list) 2217 Apply @var{fn} to each item in @var{list}, splitting it each time @var{fn} returns a new value. 2218 2219 @example 2220 @group 2221 (-partition-by 'even? ()) 2222 @result{} () 2223 @end group 2224 @group 2225 (-partition-by 'even? '(1 1 2 2 2 3 4 6 8)) 2226 @result{} ((1 1) (2 2 2) (3) (4 6 8)) 2227 @end group 2228 @group 2229 (--partition-by (< it 3) '(1 2 3 4 3 2 1)) 2230 @result{} ((1 2) (3 4 3) (2 1)) 2231 @end group 2232 @end example 2233 @end defun 2234 2235 @anchor{-partition-by-header} 2236 @defun -partition-by-header (fn list) 2237 Apply @var{fn} to the first item in @var{list}. That is the header 2238 value. Apply @var{fn} to each item in @var{list}, splitting it each time @var{fn} 2239 returns the header value, but only after seeing at least one 2240 other value (the body). 2241 2242 @example 2243 @group 2244 (--partition-by-header (= it 1) '(1 2 3 1 2 1 2 3 4)) 2245 @result{} ((1 2 3) (1 2) (1 2 3 4)) 2246 @end group 2247 @group 2248 (--partition-by-header (> it 0) '(1 2 0 1 0 1 2 3 0)) 2249 @result{} ((1 2 0) (1 0) (1 2 3 0)) 2250 @end group 2251 @group 2252 (-partition-by-header 'even? '(2 1 1 1 4 1 3 5 6 6 1)) 2253 @result{} ((2 1 1 1) (4 1 3 5) (6 6 1)) 2254 @end group 2255 @end example 2256 @end defun 2257 2258 @anchor{-partition-after-pred} 2259 @defun -partition-after-pred (pred list) 2260 Partition @var{list} after each element for which @var{pred} returns non-nil. 2261 2262 This function's anaphoric counterpart is @code{--partition-after-pred}. 2263 2264 @example 2265 @group 2266 (-partition-after-pred #'booleanp ()) 2267 @result{} () 2268 @end group 2269 @group 2270 (-partition-after-pred #'booleanp '(t t)) 2271 @result{} ((t) (t)) 2272 @end group 2273 @group 2274 (-partition-after-pred #'booleanp '(0 0 t t 0 t)) 2275 @result{} ((0 0 t) (t) (0 t)) 2276 @end group 2277 @end example 2278 @end defun 2279 2280 @anchor{-partition-before-pred} 2281 @defun -partition-before-pred (pred list) 2282 Partition directly before each time @var{pred} is true on an element of @var{list}. 2283 2284 @example 2285 @group 2286 (-partition-before-pred #'booleanp ()) 2287 @result{} () 2288 @end group 2289 @group 2290 (-partition-before-pred #'booleanp '(0 t)) 2291 @result{} ((0) (t)) 2292 @end group 2293 @group 2294 (-partition-before-pred #'booleanp '(0 0 t 0 t t)) 2295 @result{} ((0 0) (t 0) (t) (t)) 2296 @end group 2297 @end example 2298 @end defun 2299 2300 @anchor{-partition-before-item} 2301 @defun -partition-before-item (item list) 2302 Partition directly before each time @var{item} appears in @var{list}. 2303 2304 @example 2305 @group 2306 (-partition-before-item 3 ()) 2307 @result{} () 2308 @end group 2309 @group 2310 (-partition-before-item 3 '(1)) 2311 @result{} ((1)) 2312 @end group 2313 @group 2314 (-partition-before-item 3 '(3)) 2315 @result{} ((3)) 2316 @end group 2317 @end example 2318 @end defun 2319 2320 @anchor{-partition-after-item} 2321 @defun -partition-after-item (item list) 2322 Partition directly after each time @var{item} appears in @var{list}. 2323 2324 @example 2325 @group 2326 (-partition-after-item 3 ()) 2327 @result{} () 2328 @end group 2329 @group 2330 (-partition-after-item 3 '(1)) 2331 @result{} ((1)) 2332 @end group 2333 @group 2334 (-partition-after-item 3 '(3)) 2335 @result{} ((3)) 2336 @end group 2337 @end example 2338 @end defun 2339 2340 @anchor{-group-by} 2341 @defun -group-by (fn list) 2342 Separate @var{list} into an alist whose keys are @var{fn} applied to the 2343 elements of @var{list}. Keys are compared by @code{equal}. 2344 2345 @example 2346 @group 2347 (-group-by 'even? ()) 2348 @result{} () 2349 @end group 2350 @group 2351 (-group-by 'even? '(1 1 2 2 2 3 4 6 8)) 2352 @result{} ((nil 1 1 3) (t 2 2 2 4 6 8)) 2353 @end group 2354 @group 2355 (--group-by (car (split-string it "/")) '("a/b" "c/d" "a/e")) 2356 @result{} (("a" "a/b" "a/e") ("c" "c/d")) 2357 @end group 2358 @end example 2359 @end defun 2360 2361 @node Indexing 2362 @section Indexing 2363 2364 Functions retrieving or sorting based on list indices and 2365 related predicates. 2366 2367 @anchor{-elem-index} 2368 @defun -elem-index (elem list) 2369 Return the index of the first element in the given @var{list} which 2370 is equal to the query element @var{elem}, or nil if there is no 2371 such element. 2372 2373 @example 2374 @group 2375 (-elem-index 2 '(6 7 8 2 3 4)) 2376 @result{} 3 2377 @end group 2378 @group 2379 (-elem-index "bar" '("foo" "bar" "baz")) 2380 @result{} 1 2381 @end group 2382 @group 2383 (-elem-index '(1 2) '((3) (5 6) (1 2) nil)) 2384 @result{} 2 2385 @end group 2386 @end example 2387 @end defun 2388 2389 @anchor{-elem-indices} 2390 @defun -elem-indices (elem list) 2391 Return the indices of all elements in @var{list} equal to the query 2392 element @var{elem}, in ascending order. 2393 2394 @example 2395 @group 2396 (-elem-indices 2 '(6 7 8 2 3 4 2 1)) 2397 @result{} (3 6) 2398 @end group 2399 @group 2400 (-elem-indices "bar" '("foo" "bar" "baz")) 2401 @result{} (1) 2402 @end group 2403 @group 2404 (-elem-indices '(1 2) '((3) (1 2) (5 6) (1 2) nil)) 2405 @result{} (1 3) 2406 @end group 2407 @end example 2408 @end defun 2409 2410 @anchor{-find-index} 2411 @defun -find-index (pred list) 2412 Take a predicate @var{pred} and a @var{list} and return the index of the 2413 first element in the list satisfying the predicate, or nil if 2414 there is no such element. 2415 2416 See also @code{-first} (@pxref{-first}). 2417 2418 @example 2419 @group 2420 (-find-index 'even? '(2 4 1 6 3 3 5 8)) 2421 @result{} 0 2422 @end group 2423 @group 2424 (--find-index (< 5 it) '(2 4 1 6 3 3 5 8)) 2425 @result{} 3 2426 @end group 2427 @group 2428 (-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz")) 2429 @result{} 1 2430 @end group 2431 @end example 2432 @end defun 2433 2434 @anchor{-find-last-index} 2435 @defun -find-last-index (pred list) 2436 Take a predicate @var{pred} and a @var{list} and return the index of the 2437 last element in the list satisfying the predicate, or nil if 2438 there is no such element. 2439 2440 See also @code{-last} (@pxref{-last}). 2441 2442 @example 2443 @group 2444 (-find-last-index 'even? '(2 4 1 6 3 3 5 8)) 2445 @result{} 7 2446 @end group 2447 @group 2448 (--find-last-index (< 5 it) '(2 7 1 6 3 8 5 2)) 2449 @result{} 5 2450 @end group 2451 @group 2452 (-find-last-index (-partial 'string-lessp "baz") '("q" "foo" "baz")) 2453 @result{} 1 2454 @end group 2455 @end example 2456 @end defun 2457 2458 @anchor{-find-indices} 2459 @defun -find-indices (pred list) 2460 Return the indices of all elements in @var{list} satisfying the 2461 predicate @var{pred}, in ascending order. 2462 2463 @example 2464 @group 2465 (-find-indices 'even? '(2 4 1 6 3 3 5 8)) 2466 @result{} (0 1 3 7) 2467 @end group 2468 @group 2469 (--find-indices (< 5 it) '(2 4 1 6 3 3 5 8)) 2470 @result{} (3 7) 2471 @end group 2472 @group 2473 (-find-indices (-partial 'string-lessp "baz") '("bar" "foo" "baz")) 2474 @result{} (1) 2475 @end group 2476 @end example 2477 @end defun 2478 2479 @anchor{-grade-up} 2480 @defun -grade-up (comparator list) 2481 Grade elements of @var{list} using @var{comparator} relation. 2482 This yields a permutation vector such that applying this 2483 permutation to @var{list} sorts it in ascending order. 2484 2485 @example 2486 @group 2487 (-grade-up #'< '(3 1 4 2 1 3 3)) 2488 @result{} (1 4 3 0 5 6 2) 2489 @end group 2490 @group 2491 (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up #'< l) l)) 2492 @result{} (1 1 2 3 3 3 4) 2493 @end group 2494 @end example 2495 @end defun 2496 2497 @anchor{-grade-down} 2498 @defun -grade-down (comparator list) 2499 Grade elements of @var{list} using @var{comparator} relation. 2500 This yields a permutation vector such that applying this 2501 permutation to @var{list} sorts it in descending order. 2502 2503 @example 2504 @group 2505 (-grade-down #'< '(3 1 4 2 1 3 3)) 2506 @result{} (2 0 5 6 3 1 4) 2507 @end group 2508 @group 2509 (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down #'< l) l)) 2510 @result{} (4 3 3 3 2 1 1) 2511 @end group 2512 @end example 2513 @end defun 2514 2515 @node Set operations 2516 @section Set operations 2517 2518 Operations pretending lists are sets. 2519 2520 @anchor{-union} 2521 @defun -union (list list2) 2522 Return a new list containing the elements of @var{list} and elements of @var{list2} that are not in @var{list}. 2523 The test for equality is done with @code{equal}, 2524 or with @code{-compare-fn} if that's non-nil. 2525 2526 @example 2527 @group 2528 (-union '(1 2 3) '(3 4 5)) 2529 @result{} (1 2 3 4 5) 2530 @end group 2531 @group 2532 (-union '(1 2 3 4) ()) 2533 @result{} (1 2 3 4) 2534 @end group 2535 @group 2536 (-union '(1 1 2 2) '(3 2 1)) 2537 @result{} (1 1 2 2 3) 2538 @end group 2539 @end example 2540 @end defun 2541 2542 @anchor{-difference} 2543 @defun -difference (list list2) 2544 Return a new list with only the members of @var{list} that are not in @var{list2}. 2545 The test for equality is done with @code{equal}, 2546 or with @code{-compare-fn} if that's non-nil. 2547 2548 @example 2549 @group 2550 (-difference () ()) 2551 @result{} () 2552 @end group 2553 @group 2554 (-difference '(1 2 3) '(4 5 6)) 2555 @result{} (1 2 3) 2556 @end group 2557 @group 2558 (-difference '(1 2 3 4) '(3 4 5 6)) 2559 @result{} (1 2) 2560 @end group 2561 @end example 2562 @end defun 2563 2564 @anchor{-intersection} 2565 @defun -intersection (list list2) 2566 Return a new list containing only the elements that are members of both @var{list} and @var{list2}. 2567 The test for equality is done with @code{equal}, 2568 or with @code{-compare-fn} if that's non-nil. 2569 2570 @example 2571 @group 2572 (-intersection () ()) 2573 @result{} () 2574 @end group 2575 @group 2576 (-intersection '(1 2 3) '(4 5 6)) 2577 @result{} () 2578 @end group 2579 @group 2580 (-intersection '(1 2 3 4) '(3 4 5 6)) 2581 @result{} (3 4) 2582 @end group 2583 @end example 2584 @end defun 2585 2586 @anchor{-powerset} 2587 @defun -powerset (list) 2588 Return the power set of @var{list}. 2589 2590 @example 2591 @group 2592 (-powerset ()) 2593 @result{} (nil) 2594 @end group 2595 @group 2596 (-powerset '(x y z)) 2597 @result{} ((x y z) (x y) (x z) (x) (y z) (y) (z) nil) 2598 @end group 2599 @end example 2600 @end defun 2601 2602 @anchor{-permutations} 2603 @defun -permutations (list) 2604 Return the permutations of @var{list}. 2605 2606 @example 2607 @group 2608 (-permutations ()) 2609 @result{} (nil) 2610 @end group 2611 @group 2612 (-permutations '(1 2)) 2613 @result{} ((1 2) (2 1)) 2614 @end group 2615 @group 2616 (-permutations '(a b c)) 2617 @result{} ((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)) 2618 @end group 2619 @end example 2620 @end defun 2621 2622 @anchor{-distinct} 2623 @defun -distinct (list) 2624 Return a new list with all duplicates removed. 2625 The test for equality is done with @code{equal}, 2626 or with @code{-compare-fn} if that's non-nil. 2627 2628 Alias: @code{-uniq} 2629 2630 @example 2631 @group 2632 (-distinct ()) 2633 @result{} () 2634 @end group 2635 @group 2636 (-distinct '(1 2 2 4)) 2637 @result{} (1 2 4) 2638 @end group 2639 @group 2640 (-distinct '(t t t)) 2641 @result{} (t) 2642 @end group 2643 @end example 2644 @end defun 2645 2646 @node Other list operations 2647 @section Other list operations 2648 2649 Other list functions not fit to be classified elsewhere. 2650 2651 @anchor{-rotate} 2652 @defun -rotate (n list) 2653 Rotate @var{list} @var{n} places to the right (left if @var{n} is negative). 2654 The time complexity is @var{o}(n). 2655 2656 @example 2657 @group 2658 (-rotate 3 '(1 2 3 4 5 6 7)) 2659 @result{} (5 6 7 1 2 3 4) 2660 @end group 2661 @group 2662 (-rotate -3 '(1 2 3 4 5 6 7)) 2663 @result{} (4 5 6 7 1 2 3) 2664 @end group 2665 @group 2666 (-rotate 16 '(1 2 3 4 5 6 7)) 2667 @result{} (6 7 1 2 3 4 5) 2668 @end group 2669 @end example 2670 @end defun 2671 2672 @anchor{-repeat} 2673 @defun -repeat (n x) 2674 Return a new list of length @var{n} with each element being @var{x}. 2675 Return nil if @var{n} is less than 1. 2676 2677 @example 2678 @group 2679 (-repeat 3 :a) 2680 @result{} (:a :a :a) 2681 @end group 2682 @group 2683 (-repeat 1 :a) 2684 @result{} (:a) 2685 @end group 2686 @group 2687 (-repeat 0 :a) 2688 @result{} nil 2689 @end group 2690 @end example 2691 @end defun 2692 2693 @anchor{-cons*} 2694 @defun -cons* (&rest args) 2695 Make a new list from the elements of @var{args}. 2696 The last 2 elements of @var{args} are used as the final cons of the 2697 result, so if the final element of @var{args} is not a list, the result 2698 is a dotted list. With no @var{args}, return nil. 2699 2700 @example 2701 @group 2702 (-cons* 1 2) 2703 @result{} (1 . 2) 2704 @end group 2705 @group 2706 (-cons* 1 2 3) 2707 @result{} (1 2 . 3) 2708 @end group 2709 @group 2710 (-cons* 1) 2711 @result{} 1 2712 @end group 2713 @end example 2714 @end defun 2715 2716 @anchor{-snoc} 2717 @defun -snoc (list elem &rest elements) 2718 Append @var{elem} to the end of the list. 2719 2720 This is like @code{cons}, but operates on the end of list. 2721 2722 If @var{elements} is non nil, append these to the list as well. 2723 2724 @example 2725 @group 2726 (-snoc '(1 2 3) 4) 2727 @result{} (1 2 3 4) 2728 @end group 2729 @group 2730 (-snoc '(1 2 3) 4 5 6) 2731 @result{} (1 2 3 4 5 6) 2732 @end group 2733 @group 2734 (-snoc '(1 2 3) '(4 5 6)) 2735 @result{} (1 2 3 (4 5 6)) 2736 @end group 2737 @end example 2738 @end defun 2739 2740 @anchor{-interpose} 2741 @defun -interpose (sep list) 2742 Return a new list of all elements in @var{list} separated by @var{sep}. 2743 2744 @example 2745 @group 2746 (-interpose "-" ()) 2747 @result{} () 2748 @end group 2749 @group 2750 (-interpose "-" '("a")) 2751 @result{} ("a") 2752 @end group 2753 @group 2754 (-interpose "-" '("a" "b" "c")) 2755 @result{} ("a" "-" "b" "-" "c") 2756 @end group 2757 @end example 2758 @end defun 2759 2760 @anchor{-interleave} 2761 @defun -interleave (&rest lists) 2762 Return a new list of the first item in each list, then the second etc. 2763 2764 @example 2765 @group 2766 (-interleave '(1 2) '("a" "b")) 2767 @result{} (1 "a" 2 "b") 2768 @end group 2769 @group 2770 (-interleave '(1 2) '("a" "b") '("A" "B")) 2771 @result{} (1 "a" "A" 2 "b" "B") 2772 @end group 2773 @group 2774 (-interleave '(1 2 3) '("a" "b")) 2775 @result{} (1 "a" 2 "b") 2776 @end group 2777 @end example 2778 @end defun 2779 2780 @anchor{-iota} 2781 @defun -iota (count &optional start step) 2782 Return a list containing @var{count} numbers. 2783 Starts from @var{start} and adds @var{step} each time. The default @var{start} is 2784 zero, the default @var{step} is 1. 2785 This function takes its name from the corresponding primitive in 2786 the @var{apl} language. 2787 2788 @example 2789 @group 2790 (-iota 6) 2791 @result{} (0 1 2 3 4 5) 2792 @end group 2793 @group 2794 (-iota 4 2.5 -2) 2795 @result{} (2.5 0.5 -1.5 -3.5) 2796 @end group 2797 @group 2798 (-iota -1) 2799 @error{} Wrong type argument: natnump, -1 2800 @end group 2801 @end example 2802 @end defun 2803 2804 @anchor{-zip-with} 2805 @defun -zip-with (fn list1 list2) 2806 Zip the two lists @var{list1} and @var{list2} using a function @var{fn}. This 2807 function is applied pairwise taking as first argument element of 2808 @var{list1} and as second argument element of @var{list2} at corresponding 2809 position. 2810 2811 The anaphoric form @code{--zip-with} binds the elements from @var{list1} as symbol @code{it}, 2812 and the elements from @var{list2} as symbol @code{other}. 2813 2814 @example 2815 @group 2816 (-zip-with '+ '(1 2 3) '(4 5 6)) 2817 @result{} (5 7 9) 2818 @end group 2819 @group 2820 (-zip-with 'cons '(1 2 3) '(4 5 6)) 2821 @result{} ((1 . 4) (2 . 5) (3 . 6)) 2822 @end group 2823 @group 2824 (--zip-with (concat it " and " other) '("Batman" "Jekyll") '("Robin" "Hyde")) 2825 @result{} ("Batman and Robin" "Jekyll and Hyde") 2826 @end group 2827 @end example 2828 @end defun 2829 2830 @anchor{-zip} 2831 @defun -zip (&rest lists) 2832 Zip @var{lists} together. Group the head of each list, followed by the 2833 second elements of each list, and so on. The lengths of the returned 2834 groupings are equal to the length of the shortest input list. 2835 2836 If two lists are provided as arguments, return the groupings as a list 2837 of cons cells. Otherwise, return the groupings as a list of lists. 2838 2839 Use @code{-zip-lists} (@pxref{-zip-lists}) if you need the return value to always be a list 2840 of lists. 2841 2842 Alias: @code{-zip-pair} 2843 2844 See also: @code{-zip-lists} (@pxref{-zip-lists}) 2845 2846 @example 2847 @group 2848 (-zip '(1 2 3) '(4 5 6)) 2849 @result{} ((1 . 4) (2 . 5) (3 . 6)) 2850 @end group 2851 @group 2852 (-zip '(1 2 3) '(4 5 6 7)) 2853 @result{} ((1 . 4) (2 . 5) (3 . 6)) 2854 @end group 2855 @group 2856 (-zip '(1 2) '(3 4 5) '(6)) 2857 @result{} ((1 3 6)) 2858 @end group 2859 @end example 2860 @end defun 2861 2862 @anchor{-zip-lists} 2863 @defun -zip-lists (&rest lists) 2864 Zip @var{lists} together. Group the head of each list, followed by the 2865 second elements of each list, and so on. The lengths of the returned 2866 groupings are equal to the length of the shortest input list. 2867 2868 The return value is always list of lists, which is a difference 2869 from @code{-zip-pair} which returns a cons-cell in case two input 2870 lists are provided. 2871 2872 See also: @code{-zip} (@pxref{-zip}) 2873 2874 @example 2875 @group 2876 (-zip-lists '(1 2 3) '(4 5 6)) 2877 @result{} ((1 4) (2 5) (3 6)) 2878 @end group 2879 @group 2880 (-zip-lists '(1 2 3) '(4 5 6 7)) 2881 @result{} ((1 4) (2 5) (3 6)) 2882 @end group 2883 @group 2884 (-zip-lists '(1 2) '(3 4 5) '(6)) 2885 @result{} ((1 3 6)) 2886 @end group 2887 @end example 2888 @end defun 2889 2890 @anchor{-zip-fill} 2891 @defun -zip-fill (fill-value &rest lists) 2892 Zip @var{lists}, with @var{fill-value} padded onto the shorter lists. The 2893 lengths of the returned groupings are equal to the length of the 2894 longest input list. 2895 2896 @example 2897 @group 2898 (-zip-fill 0 '(1 2 3 4 5) '(6 7 8 9)) 2899 @result{} ((1 . 6) (2 . 7) (3 . 8) (4 . 9) (5 . 0)) 2900 @end group 2901 @end example 2902 @end defun 2903 2904 @anchor{-unzip} 2905 @defun -unzip (lists) 2906 Unzip @var{lists}. 2907 2908 This works just like @code{-zip} (@pxref{-zip}) but takes a list of lists instead of 2909 a variable number of arguments, such that 2910 2911 (-unzip (-zip @var{l1} @var{l2} @var{l3} @dots{})) 2912 2913 is identity (given that the lists are the same length). 2914 2915 Note in particular that calling this on a list of two lists will 2916 return a list of cons-cells such that the above identity works. 2917 2918 See also: @code{-zip} (@pxref{-zip}) 2919 2920 @example 2921 @group 2922 (-unzip (-zip '(1 2 3) '(a b c) '("e" "f" "g"))) 2923 @result{} ((1 2 3) (a b c) ("e" "f" "g")) 2924 @end group 2925 @group 2926 (-unzip '((1 2) (3 4) (5 6) (7 8) (9 10))) 2927 @result{} ((1 3 5 7 9) (2 4 6 8 10)) 2928 @end group 2929 @group 2930 (-unzip '((1 2) (3 4))) 2931 @result{} ((1 . 3) (2 . 4)) 2932 @end group 2933 @end example 2934 @end defun 2935 2936 @anchor{-cycle} 2937 @defun -cycle (list) 2938 Return an infinite circular copy of @var{list}. 2939 The returned list cycles through the elements of @var{list} and repeats 2940 from the beginning. 2941 2942 @example 2943 @group 2944 (-take 5 (-cycle '(1 2 3))) 2945 @result{} (1 2 3 1 2) 2946 @end group 2947 @group 2948 (-take 7 (-cycle '(1 "and" 3))) 2949 @result{} (1 "and" 3 1 "and" 3 1) 2950 @end group 2951 @group 2952 (-zip (-cycle '(1 2 3)) '(1 2)) 2953 @result{} ((1 . 1) (2 . 2)) 2954 @end group 2955 @end example 2956 @end defun 2957 2958 @anchor{-pad} 2959 @defun -pad (fill-value &rest lists) 2960 Appends @var{fill-value} to the end of each list in @var{lists} such that they 2961 will all have the same length. 2962 2963 @example 2964 @group 2965 (-pad 0 ()) 2966 @result{} (nil) 2967 @end group 2968 @group 2969 (-pad 0 '(1)) 2970 @result{} ((1)) 2971 @end group 2972 @group 2973 (-pad 0 '(1 2 3) '(4 5)) 2974 @result{} ((1 2 3) (4 5 0)) 2975 @end group 2976 @end example 2977 @end defun 2978 2979 @anchor{-table} 2980 @defun -table (fn &rest lists) 2981 Compute outer product of @var{lists} using function @var{fn}. 2982 2983 The function @var{fn} should have the same arity as the number of 2984 supplied lists. 2985 2986 The outer product is computed by applying fn to all possible 2987 combinations created by taking one element from each list in 2988 order. The dimension of the result is (length lists). 2989 2990 See also: @code{-table-flat} (@pxref{-table-flat}) 2991 2992 @example 2993 @group 2994 (-table '* '(1 2 3) '(1 2 3)) 2995 @result{} ((1 2 3) (2 4 6) (3 6 9)) 2996 @end group 2997 @group 2998 (-table (lambda (a b) (-sum (-zip-with '* a b))) '((1 2) (3 4)) '((1 3) (2 4))) 2999 @result{} ((7 15) (10 22)) 3000 @end group 3001 @group 3002 (apply '-table 'list (-repeat 3 '(1 2))) 3003 @result{} ((((1 1 1) (2 1 1)) ((1 2 1) (2 2 1))) (((1 1 2) (2 1 2)) ((1 2 2) (2 2 2)))) 3004 @end group 3005 @end example 3006 @end defun 3007 3008 @anchor{-table-flat} 3009 @defun -table-flat (fn &rest lists) 3010 Compute flat outer product of @var{lists} using function @var{fn}. 3011 3012 The function @var{fn} should have the same arity as the number of 3013 supplied lists. 3014 3015 The outer product is computed by applying fn to all possible 3016 combinations created by taking one element from each list in 3017 order. The results are flattened, ignoring the tensor structure 3018 of the result. This is equivalent to calling: 3019 3020 (-flatten-n (1- (length lists)) (apply '-table fn lists)) 3021 3022 but the implementation here is much more efficient. 3023 3024 See also: @code{-flatten-n} (@pxref{-flatten-n}), @code{-table} (@pxref{-table}) 3025 3026 @example 3027 @group 3028 (-table-flat 'list '(1 2 3) '(a b c)) 3029 @result{} ((1 a) (2 a) (3 a) (1 b) (2 b) (3 b) (1 c) (2 c) (3 c)) 3030 @end group 3031 @group 3032 (-table-flat '* '(1 2 3) '(1 2 3)) 3033 @result{} (1 2 3 2 4 6 3 6 9) 3034 @end group 3035 @group 3036 (apply '-table-flat 'list (-repeat 3 '(1 2))) 3037 @result{} ((1 1 1) (2 1 1) (1 2 1) (2 2 1) (1 1 2) (2 1 2) (1 2 2) (2 2 2)) 3038 @end group 3039 @end example 3040 @end defun 3041 3042 @anchor{-first} 3043 @defun -first (pred list) 3044 Return the first item in @var{list} for which @var{pred} returns non-nil. 3045 Return nil if no such element is found. 3046 To get the first item in the list no questions asked, use @code{car}. 3047 3048 Alias: @code{-find}. 3049 3050 This function's anaphoric counterpart is @code{--first}. 3051 3052 @example 3053 @group 3054 (-first #'natnump '(-1 0 1)) 3055 @result{} 0 3056 @end group 3057 @group 3058 (-first #'null '(1 2 3)) 3059 @result{} nil 3060 @end group 3061 @group 3062 (--first (> it 2) '(1 2 3)) 3063 @result{} 3 3064 @end group 3065 @end example 3066 @end defun 3067 3068 @anchor{-last} 3069 @defun -last (pred list) 3070 Return the last x in @var{list} where (@var{pred} x) is non-nil, else nil. 3071 3072 @example 3073 @group 3074 (-last 'even? '(1 2 3 4 5 6 3 3 3)) 3075 @result{} 6 3076 @end group 3077 @group 3078 (-last 'even? '(1 3 7 5 9)) 3079 @result{} nil 3080 @end group 3081 @group 3082 (--last (> (length it) 3) '("a" "looong" "word" "and" "short" "one")) 3083 @result{} "short" 3084 @end group 3085 @end example 3086 @end defun 3087 3088 @anchor{-first-item} 3089 @defun -first-item (list) 3090 Return the first item of @var{list}, or nil on an empty list. 3091 3092 See also: @code{-second-item} (@pxref{-second-item}), @code{-last-item} (@pxref{-last-item}). 3093 3094 @example 3095 @group 3096 (-first-item '(1 2 3)) 3097 @result{} 1 3098 @end group 3099 @group 3100 (-first-item nil) 3101 @result{} nil 3102 @end group 3103 @group 3104 (let ((list (list 1 2 3))) (setf (-first-item list) 5) list) 3105 @result{} (5 2 3) 3106 @end group 3107 @end example 3108 @end defun 3109 3110 @anchor{-second-item} 3111 @defun -second-item (list) 3112 Return the second item of @var{list}, or nil if @var{list} is too short. 3113 3114 See also: @code{-third-item} (@pxref{-third-item}). 3115 3116 @example 3117 @group 3118 (-second-item '(1 2 3)) 3119 @result{} 2 3120 @end group 3121 @group 3122 (-second-item nil) 3123 @result{} nil 3124 @end group 3125 @end example 3126 @end defun 3127 3128 @anchor{-third-item} 3129 @defun -third-item (list) 3130 Return the third item of @var{list}, or nil if @var{list} is too short. 3131 3132 See also: @code{-fourth-item} (@pxref{-fourth-item}). 3133 3134 @example 3135 @group 3136 (-third-item '(1 2 3)) 3137 @result{} 3 3138 @end group 3139 @group 3140 (-third-item nil) 3141 @result{} nil 3142 @end group 3143 @end example 3144 @end defun 3145 3146 @anchor{-fourth-item} 3147 @defun -fourth-item (list) 3148 Return the fourth item of @var{list}, or nil if @var{list} is too short. 3149 3150 See also: @code{-fifth-item} (@pxref{-fifth-item}). 3151 3152 @example 3153 @group 3154 (-fourth-item '(1 2 3 4)) 3155 @result{} 4 3156 @end group 3157 @group 3158 (-fourth-item nil) 3159 @result{} nil 3160 @end group 3161 @end example 3162 @end defun 3163 3164 @anchor{-fifth-item} 3165 @defun -fifth-item (list) 3166 Return the fifth item of @var{list}, or nil if @var{list} is too short. 3167 3168 See also: @code{-last-item} (@pxref{-last-item}). 3169 3170 @example 3171 @group 3172 (-fifth-item '(1 2 3 4 5)) 3173 @result{} 5 3174 @end group 3175 @group 3176 (-fifth-item nil) 3177 @result{} nil 3178 @end group 3179 @end example 3180 @end defun 3181 3182 @anchor{-last-item} 3183 @defun -last-item (list) 3184 Return the last item of @var{list}, or nil on an empty list. 3185 3186 @example 3187 @group 3188 (-last-item '(1 2 3)) 3189 @result{} 3 3190 @end group 3191 @group 3192 (-last-item nil) 3193 @result{} nil 3194 @end group 3195 @group 3196 (let ((list (list 1 2 3))) (setf (-last-item list) 5) list) 3197 @result{} (1 2 5) 3198 @end group 3199 @end example 3200 @end defun 3201 3202 @anchor{-butlast} 3203 @defun -butlast (list) 3204 Return a list of all items in list except for the last. 3205 3206 @example 3207 @group 3208 (-butlast '(1 2 3)) 3209 @result{} (1 2) 3210 @end group 3211 @group 3212 (-butlast '(1 2)) 3213 @result{} (1) 3214 @end group 3215 @group 3216 (-butlast '(1)) 3217 @result{} nil 3218 @end group 3219 @end example 3220 @end defun 3221 3222 @anchor{-sort} 3223 @defun -sort (comparator list) 3224 Sort @var{list}, stably, comparing elements using @var{comparator}. 3225 Return the sorted list. @var{list} is @var{not} modified by side effects. 3226 @var{comparator} is called with two elements of @var{list}, and should return non-nil 3227 if the first element should sort before the second. 3228 3229 @example 3230 @group 3231 (-sort '< '(3 1 2)) 3232 @result{} (1 2 3) 3233 @end group 3234 @group 3235 (-sort '> '(3 1 2)) 3236 @result{} (3 2 1) 3237 @end group 3238 @group 3239 (--sort (< it other) '(3 1 2)) 3240 @result{} (1 2 3) 3241 @end group 3242 @end example 3243 @end defun 3244 3245 @anchor{-list} 3246 @defun -list (arg) 3247 Ensure @var{arg} is a list. 3248 If @var{arg} is already a list, return it as is (not a copy). 3249 Otherwise, return a new list with @var{arg} as its only element. 3250 3251 Another supported calling convention is (-list &rest @var{args}). 3252 In this case, if @var{arg} is not a list, a new list with all of 3253 @var{args} as elements is returned. This use is supported for 3254 backward compatibility and is otherwise deprecated. 3255 3256 @example 3257 @group 3258 (-list 1) 3259 @result{} (1) 3260 @end group 3261 @group 3262 (-list ()) 3263 @result{} () 3264 @end group 3265 @group 3266 (-list '(1 2 3)) 3267 @result{} (1 2 3) 3268 @end group 3269 @end example 3270 @end defun 3271 3272 @anchor{-fix} 3273 @defun -fix (fn list) 3274 Compute the (least) fixpoint of @var{fn} with initial input @var{list}. 3275 3276 @var{fn} is called at least once, results are compared with @code{equal}. 3277 3278 @example 3279 @group 3280 (-fix (lambda (l) (-non-nil (--mapcat (-split-at (/ (length it) 2) it) l))) '((1 2 3))) 3281 @result{} ((1) (2) (3)) 3282 @end group 3283 @group 3284 (let ((l '((starwars scifi) (jedi starwars warrior)))) (--fix (-uniq (--mapcat (cons it (cdr (assq it l))) it)) '(jedi book))) 3285 @result{} (jedi starwars warrior scifi book) 3286 @end group 3287 @end example 3288 @end defun 3289 3290 @node Tree operations 3291 @section Tree operations 3292 3293 Functions pretending lists are trees. 3294 3295 @anchor{-tree-seq} 3296 @defun -tree-seq (branch children tree) 3297 Return a sequence of the nodes in @var{tree}, in depth-first search order. 3298 3299 @var{branch} is a predicate of one argument that returns non-nil if the 3300 passed argument is a branch, that is, a node that can have children. 3301 3302 @var{children} is a function of one argument that returns the children 3303 of the passed branch node. 3304 3305 Non-branch nodes are simply copied. 3306 3307 @example 3308 @group 3309 (-tree-seq 'listp 'identity '(1 (2 3) 4 (5 (6 7)))) 3310 @result{} ((1 (2 3) 4 (5 (6 7))) 1 (2 3) 2 3 4 (5 (6 7)) 5 (6 7) 6 7) 3311 @end group 3312 @group 3313 (-tree-seq 'listp 'reverse '(1 (2 3) 4 (5 (6 7)))) 3314 @result{} ((1 (2 3) 4 (5 (6 7))) (5 (6 7)) (6 7) 7 6 5 4 (2 3) 3 2 1) 3315 @end group 3316 @group 3317 (--tree-seq (vectorp it) (append it nil) [1 [2 3] 4 [5 [6 7]]]) 3318 @result{} ([1 [2 3] 4 [5 [6 7]]] 1 [2 3] 2 3 4 [5 [6 7]] 5 [6 7] 6 7) 3319 @end group 3320 @end example 3321 @end defun 3322 3323 @anchor{-tree-map} 3324 @defun -tree-map (fn tree) 3325 Apply @var{fn} to each element of @var{tree} while preserving the tree structure. 3326 3327 @example 3328 @group 3329 (-tree-map '1+ '(1 (2 3) (4 (5 6) 7))) 3330 @result{} (2 (3 4) (5 (6 7) 8)) 3331 @end group 3332 @group 3333 (-tree-map '(lambda (x) (cons x (expt 2 x))) '(1 (2 3) 4)) 3334 @result{} ((1 . 2) ((2 . 4) (3 . 8)) (4 . 16)) 3335 @end group 3336 @group 3337 (--tree-map (length it) '("<body>" ("<p>" "text" "</p>") "</body>")) 3338 @result{} (6 (3 4 4) 7) 3339 @end group 3340 @end example 3341 @end defun 3342 3343 @anchor{-tree-map-nodes} 3344 @defun -tree-map-nodes (pred fun tree) 3345 Call @var{fun} on each node of @var{tree} that satisfies @var{pred}. 3346 3347 If @var{pred} returns nil, continue descending down this node. If @var{pred} 3348 returns non-nil, apply @var{fun} to this node and do not descend 3349 further. 3350 3351 @example 3352 @group 3353 (-tree-map-nodes 'vectorp (lambda (x) (-sum (append x nil))) '(1 [2 3] 4 (5 [6 7] 8))) 3354 @result{} (1 5 4 (5 13 8)) 3355 @end group 3356 @group 3357 (-tree-map-nodes 'keywordp (lambda (x) (symbol-name x)) '(1 :foo 4 ((5 6 :bar) :baz 8))) 3358 @result{} (1 ":foo" 4 ((5 6 ":bar") ":baz" 8)) 3359 @end group 3360 @group 3361 (--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)))) 3362 @result{} (with-mode emacs-lisp-mode (foo bar) (add-mode a b :mode emacs-lisp-mode) (baz (add-mode c d :mode emacs-lisp-mode))) 3363 @end group 3364 @end example 3365 @end defun 3366 3367 @anchor{-tree-reduce} 3368 @defun -tree-reduce (fn tree) 3369 Use @var{fn} to reduce elements of list @var{tree}. 3370 If elements of @var{tree} are lists themselves, apply the reduction recursively. 3371 3372 @var{fn} is first applied to first element of the list and second 3373 element, then on this result and third element from the list etc. 3374 3375 See @code{-reduce-r} (@pxref{-reduce-r}) for how exactly are lists of zero or one element handled. 3376 3377 @example 3378 @group 3379 (-tree-reduce '+ '(1 (2 3) (4 5))) 3380 @result{} 15 3381 @end group 3382 @group 3383 (-tree-reduce 'concat '("strings" (" on" " various") ((" levels")))) 3384 @result{} "strings on various levels" 3385 @end group 3386 @group 3387 (--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"))) 3388 @result{} "<body><p>some words</p> <div>more <b>bold</b> words</div></body>" 3389 @end group 3390 @end example 3391 @end defun 3392 3393 @anchor{-tree-reduce-from} 3394 @defun -tree-reduce-from (fn init-value tree) 3395 Use @var{fn} to reduce elements of list @var{tree}. 3396 If elements of @var{tree} are lists themselves, apply the reduction recursively. 3397 3398 @var{fn} is first applied to @var{init-value} and first element of the list, 3399 then on this result and second element from the list etc. 3400 3401 The initial value is ignored on cons pairs as they always contain 3402 two elements. 3403 3404 @example 3405 @group 3406 (-tree-reduce-from '+ 1 '(1 (1 1) ((1)))) 3407 @result{} 8 3408 @end group 3409 @group 3410 (--tree-reduce-from (-concat acc (list it)) nil '(1 (2 3 (4 5)) (6 7))) 3411 @result{} ((7 6) ((5 4) 3 2) 1) 3412 @end group 3413 @end example 3414 @end defun 3415 3416 @anchor{-tree-mapreduce} 3417 @defun -tree-mapreduce (fn folder tree) 3418 Apply @var{fn} to each element of @var{tree}, and make a list of the results. 3419 If elements of @var{tree} are lists themselves, apply @var{fn} recursively to 3420 elements of these nested lists. 3421 3422 Then reduce the resulting lists using @var{folder} and initial value 3423 @var{init-value}. See @code{-reduce-r-from} (@pxref{-reduce-r-from}). 3424 3425 This is the same as calling @code{-tree-reduce} (@pxref{-tree-reduce}) after @code{-tree-map} (@pxref{-tree-map}) 3426 but is twice as fast as it only traverse the structure once. 3427 3428 @example 3429 @group 3430 (-tree-mapreduce 'list 'append '(1 (2 (3 4) (5 6)) (7 (8 9)))) 3431 @result{} (1 2 3 4 5 6 7 8 9) 3432 @end group 3433 @group 3434 (--tree-mapreduce 1 (+ it acc) '(1 (2 (4 9) (2 1)) (7 (4 3)))) 3435 @result{} 9 3436 @end group 3437 @group 3438 (--tree-mapreduce 0 (max acc (1+ it)) '(1 (2 (4 9) (2 1)) (7 (4 3)))) 3439 @result{} 3 3440 @end group 3441 @end example 3442 @end defun 3443 3444 @anchor{-tree-mapreduce-from} 3445 @defun -tree-mapreduce-from (fn folder init-value tree) 3446 Apply @var{fn} to each element of @var{tree}, and make a list of the results. 3447 If elements of @var{tree} are lists themselves, apply @var{fn} recursively to 3448 elements of these nested lists. 3449 3450 Then reduce the resulting lists using @var{folder} and initial value 3451 @var{init-value}. See @code{-reduce-r-from} (@pxref{-reduce-r-from}). 3452 3453 This is the same as calling @code{-tree-reduce-from} (@pxref{-tree-reduce-from}) after @code{-tree-map} (@pxref{-tree-map}) 3454 but is twice as fast as it only traverse the structure once. 3455 3456 @example 3457 @group 3458 (-tree-mapreduce-from 'identity '* 1 '(1 (2 (3 4) (5 6)) (7 (8 9)))) 3459 @result{} 362880 3460 @end group 3461 @group 3462 (--tree-mapreduce-from (+ it it) (cons it acc) nil '(1 (2 (4 9) (2 1)) (7 (4 3)))) 3463 @result{} (2 (4 (8 18) (4 2)) (14 (8 6))) 3464 @end group 3465 @group 3466 (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))))) 3467 @result{} "@{elisp-mode : @{foo : @{bar -> booze@}, baz -> qux@}, c-mode : @{foo -> bla, bum -> bam@}@}" 3468 @end group 3469 @end example 3470 @end defun 3471 3472 @anchor{-clone} 3473 @defun -clone (list) 3474 Create a deep copy of @var{list}. 3475 The new list has the same elements and structure but all cons are 3476 replaced with new ones. This is useful when you need to clone a 3477 structure such as plist or alist. 3478 3479 @example 3480 @group 3481 (let* ((a '(1 2 3)) (b (-clone a))) (nreverse a) b) 3482 @result{} (1 2 3) 3483 @end group 3484 @end example 3485 @end defun 3486 3487 @node Threading macros 3488 @section Threading macros 3489 3490 Macros that conditionally combine sequential forms for brevity 3491 or readability. 3492 3493 @anchor{->} 3494 @defmac -> (x &optional form &rest more) 3495 Thread the expr through the forms. Insert @var{x} as the second item 3496 in the first form, making a list of it if it is not a list 3497 already. If there are more forms, insert the first form as the 3498 second item in second form, etc. 3499 3500 @example 3501 @group 3502 (-> '(2 3 5)) 3503 @result{} (2 3 5) 3504 @end group 3505 @group 3506 (-> '(2 3 5) (append '(8 13))) 3507 @result{} (2 3 5 8 13) 3508 @end group 3509 @group 3510 (-> '(2 3 5) (append '(8 13)) (-slice 1 -1)) 3511 @result{} (3 5 8) 3512 @end group 3513 @end example 3514 @end defmac 3515 3516 @anchor{->>} 3517 @defmac ->> (x &optional form &rest more) 3518 Thread the expr through the forms. Insert @var{x} as the last item 3519 in the first form, making a list of it if it is not a list 3520 already. If there are more forms, insert the first form as the 3521 last item in second form, etc. 3522 3523 @example 3524 @group 3525 (->> '(1 2 3) (-map 'square)) 3526 @result{} (1 4 9) 3527 @end group 3528 @group 3529 (->> '(1 2 3) (-map 'square) (-remove 'even?)) 3530 @result{} (1 9) 3531 @end group 3532 @group 3533 (->> '(1 2 3) (-map 'square) (-reduce '+)) 3534 @result{} 14 3535 @end group 3536 @end example 3537 @end defmac 3538 3539 @anchor{-->} 3540 @defmac --> (x &rest forms) 3541 Starting with the value of @var{x}, thread each expression through @var{forms}. 3542 3543 Insert @var{x} at the position signified by the symbol @code{it} in the first 3544 form. If there are more forms, insert the first form at the position 3545 signified by @code{it} in in second form, etc. 3546 3547 @example 3548 @group 3549 (--> "def" (concat "abc" it "ghi")) 3550 @result{} "abcdefghi" 3551 @end group 3552 @group 3553 (--> "def" (concat "abc" it "ghi") (upcase it)) 3554 @result{} "ABCDEFGHI" 3555 @end group 3556 @group 3557 (--> "def" (concat "abc" it "ghi") upcase) 3558 @result{} "ABCDEFGHI" 3559 @end group 3560 @end example 3561 @end defmac 3562 3563 @anchor{-as->} 3564 @defmac -as-> (value variable &rest forms) 3565 Starting with @var{value}, thread @var{variable} through @var{forms}. 3566 3567 In the first form, bind @var{variable} to @var{value}. In the second form, bind 3568 @var{variable} to the result of the first form, and so forth. 3569 3570 @example 3571 @group 3572 (-as-> 3 my-var (1+ my-var) (list my-var) (mapcar (lambda (ele) (* 2 ele)) my-var)) 3573 @result{} (8) 3574 @end group 3575 @group 3576 (-as-> 3 my-var 1+) 3577 @result{} 4 3578 @end group 3579 @group 3580 (-as-> 3 my-var) 3581 @result{} 3 3582 @end group 3583 @end example 3584 @end defmac 3585 3586 @anchor{-some->} 3587 @defmac -some-> (x &optional form &rest more) 3588 When expr is non-nil, thread it through the first form (via @code{->} (@pxref{->})), 3589 and when that result is non-nil, through the next form, etc. 3590 3591 @example 3592 @group 3593 (-some-> '(2 3 5)) 3594 @result{} (2 3 5) 3595 @end group 3596 @group 3597 (-some-> 5 square) 3598 @result{} 25 3599 @end group 3600 @group 3601 (-some-> 5 even? square) 3602 @result{} nil 3603 @end group 3604 @end example 3605 @end defmac 3606 3607 @anchor{-some->>} 3608 @defmac -some->> (x &optional form &rest more) 3609 When expr is non-nil, thread it through the first form (via @code{->>} (@pxref{->>})), 3610 and when that result is non-nil, through the next form, etc. 3611 3612 @example 3613 @group 3614 (-some->> '(1 2 3) (-map 'square)) 3615 @result{} (1 4 9) 3616 @end group 3617 @group 3618 (-some->> '(1 3 5) (-last 'even?) (+ 100)) 3619 @result{} nil 3620 @end group 3621 @group 3622 (-some->> '(2 4 6) (-last 'even?) (+ 100)) 3623 @result{} 106 3624 @end group 3625 @end example 3626 @end defmac 3627 3628 @anchor{-some-->} 3629 @defmac -some--> (expr &rest forms) 3630 Thread @var{expr} through @var{forms} via @code{-->} (@pxref{-->}), while the result is non-nil. 3631 When @var{expr} evaluates to non-nil, thread the result through the 3632 first of @var{forms}, and when that result is non-nil, thread it 3633 through the next form, etc. 3634 3635 @example 3636 @group 3637 (-some--> "def" (concat "abc" it "ghi")) 3638 @result{} "abcdefghi" 3639 @end group 3640 @group 3641 (-some--> nil (concat "abc" it "ghi")) 3642 @result{} nil 3643 @end group 3644 @group 3645 (-some--> '(0 1) (-remove #'natnump it) (append it it) (-map #'1+ it)) 3646 @result{} () 3647 @end group 3648 @end example 3649 @end defmac 3650 3651 @anchor{-doto} 3652 @defmac -doto (init &rest forms) 3653 Evaluate @var{init} and pass it as argument to @var{forms} with @code{->} (@pxref{->}). 3654 The @var{result} of evaluating @var{init} is threaded through each of @var{forms} 3655 individually using @code{->} (@pxref{->}), which see. The return value is @var{result}, 3656 which @var{forms} may have modified by side effect. 3657 3658 @example 3659 @group 3660 (-doto (list 1 2 3) pop pop) 3661 @result{} (3) 3662 @end group 3663 @group 3664 (-doto (cons 1 2) (setcar 3) (setcdr 4)) 3665 @result{} (3 . 4) 3666 @end group 3667 @group 3668 (gethash 'k (--doto (make-hash-table) (puthash 'k 'v it))) 3669 @result{} v 3670 @end group 3671 @end example 3672 @end defmac 3673 3674 @node Binding 3675 @section Binding 3676 3677 Macros that combine @code{let} and @code{let*} with destructuring and flow control. 3678 3679 @anchor{-when-let} 3680 @defmac -when-let ((var val) &rest body) 3681 If @var{val} evaluates to non-nil, bind it to @var{var} and execute body. 3682 3683 Note: binding is done according to @code{-let} (@pxref{-let}). 3684 3685 @example 3686 @group 3687 (-when-let (match-index (string-match "d" "abcd")) (+ match-index 2)) 3688 @result{} 5 3689 @end group 3690 @group 3691 (-when-let ((&plist :foo foo) (list :foo "foo")) foo) 3692 @result{} "foo" 3693 @end group 3694 @group 3695 (-when-let ((&plist :foo foo) (list :bar "bar")) foo) 3696 @result{} nil 3697 @end group 3698 @end example 3699 @end defmac 3700 3701 @anchor{-when-let*} 3702 @defmac -when-let* (vars-vals &rest body) 3703 If all @var{vals} evaluate to true, bind them to their corresponding 3704 @var{vars} and execute body. @var{vars-vals} should be a list of (@var{var} @var{val}) 3705 pairs. 3706 3707 Note: binding is done according to @code{-let*} (@pxref{-let*}). @var{vals} are evaluated 3708 sequentially, and evaluation stops after the first nil @var{val} is 3709 encountered. 3710 3711 @example 3712 @group 3713 (-when-let* ((x 5) (y 3) (z (+ y 4))) (+ x y z)) 3714 @result{} 15 3715 @end group 3716 @group 3717 (-when-let* ((x 5) (y nil) (z 7)) (+ x y z)) 3718 @result{} nil 3719 @end group 3720 @end example 3721 @end defmac 3722 3723 @anchor{-if-let} 3724 @defmac -if-let ((var val) then &rest else) 3725 If @var{val} evaluates to non-nil, bind it to @var{var} and do @var{then}, 3726 otherwise do @var{else}. 3727 3728 Note: binding is done according to @code{-let} (@pxref{-let}). 3729 3730 @example 3731 @group 3732 (-if-let (match-index (string-match "d" "abc")) (+ match-index 3) 7) 3733 @result{} 7 3734 @end group 3735 @group 3736 (--if-let (even? 4) it nil) 3737 @result{} t 3738 @end group 3739 @end example 3740 @end defmac 3741 3742 @anchor{-if-let*} 3743 @defmac -if-let* (vars-vals then &rest else) 3744 If all @var{vals} evaluate to true, bind them to their corresponding 3745 @var{vars} and do @var{then}, otherwise do @var{else}. @var{vars-vals} should be a list 3746 of (@var{var} @var{val}) pairs. 3747 3748 Note: binding is done according to @code{-let*} (@pxref{-let*}). @var{vals} are evaluated 3749 sequentially, and evaluation stops after the first nil @var{val} is 3750 encountered. 3751 3752 @example 3753 @group 3754 (-if-let* ((x 5) (y 3) (z 7)) (+ x y z) "foo") 3755 @result{} 15 3756 @end group 3757 @group 3758 (-if-let* ((x 5) (y nil) (z 7)) (+ x y z) "foo") 3759 @result{} "foo" 3760 @end group 3761 @group 3762 (-if-let* (((_ _ x) '(nil nil 7))) x) 3763 @result{} 7 3764 @end group 3765 @end example 3766 @end defmac 3767 3768 @anchor{-let} 3769 @defmac -let (varlist &rest body) 3770 Bind variables according to @var{varlist} then eval @var{body}. 3771 3772 @var{varlist} is a list of lists of the form (@var{pattern} @var{source}). Each 3773 @var{pattern} is matched against the @var{source} "structurally". @var{source} 3774 is only evaluated once for each @var{pattern}. Each @var{pattern} is matched 3775 recursively, and can therefore contain sub-patterns which are 3776 matched against corresponding sub-expressions of @var{source}. 3777 3778 All the SOURCEs are evalled before any symbols are 3779 bound (i.e. "in parallel"). 3780 3781 If @var{varlist} only contains one (@var{pattern} @var{source}) element, you can 3782 optionally specify it using a vector and discarding the 3783 outer-most parens. Thus 3784 3785 (-let ((@var{pattern} @var{source})) @dots{}) 3786 3787 becomes 3788 3789 (-let [@var{pattern} @var{source}] @dots{}). 3790 3791 @code{-let} (@pxref{-let}) uses a convention of not binding places (symbols) starting 3792 with _ whenever it's possible. You can use this to skip over 3793 entries you don't care about. However, this is not *always* 3794 possible (as a result of implementation) and these symbols might 3795 get bound to undefined values. 3796 3797 Following is the overview of supported patterns. Remember that 3798 patterns can be matched recursively, so every a, b, aK in the 3799 following can be a matching construct and not necessarily a 3800 symbol/variable. 3801 3802 Symbol: 3803 3804 a - bind the @var{source} to @var{a}. This is just like regular @code{let}. 3805 3806 Conses and lists: 3807 3808 (a) - bind @code{car} of cons/list to @var{a} 3809 3810 (a . b) - bind car of cons to @var{a} and @code{cdr} to @var{b} 3811 3812 (a b) - bind car of list to @var{a} and @code{cadr} to @var{b} 3813 3814 (a1 a2 a3 @dots{}) - bind 0th car of list to @var{a1}, 1st to @var{a2}, 2nd to @var{a3}@enddots{} 3815 3816 (a1 a2 a3 @dots{} aN . rest) - as above, but bind the Nth cdr to @var{rest}. 3817 3818 Vectors: 3819 3820 [a] - bind 0th element of a non-list sequence to @var{a} (works with 3821 vectors, strings, bit arrays@dots{}) 3822 3823 [a1 a2 a3 @dots{}] - bind 0th element of non-list sequence to @var{a0}, 1st to 3824 @var{a1}, 2nd to @var{a2}, @enddots{} 3825 If the @var{pattern} is shorter than @var{source}, the values at 3826 places not in @var{pattern} are ignored. 3827 If the @var{pattern} is longer than @var{source}, an @code{error} is 3828 thrown. 3829 3830 [a1 a2 a3 @dots{} &rest rest] - as above, but bind the rest of 3831 the sequence to @var{rest}. This is 3832 conceptually the same as improper list 3833 matching (a1 a2 @dots{} aN . rest) 3834 3835 Key/value stores: 3836 3837 (&plist key0 a0 @dots{} keyN aN) - bind value mapped by keyK in the 3838 @var{source} plist to aK. If the 3839 value is not found, aK is nil. 3840 Uses @code{plist-get} to fetch values. 3841 3842 (&alist key0 a0 @dots{} keyN aN) - bind value mapped by keyK in the 3843 @var{source} alist to aK. If the 3844 value is not found, aK is nil. 3845 Uses @code{assoc} to fetch values. 3846 3847 (&hash key0 a0 @dots{} keyN aN) - bind value mapped by keyK in the 3848 @var{source} hash table to aK. If the 3849 value is not found, aK is nil. 3850 Uses @code{gethash} to fetch values. 3851 3852 Further, special keyword &keys supports "inline" matching of 3853 plist-like key-value pairs, similarly to &keys keyword of 3854 @code{cl-defun}. 3855 3856 (a1 a2 @dots{} aN &keys key1 b1 @dots{} keyN bK) 3857 3858 This binds @var{n} values from the list to a1 @dots{} aN, then interprets 3859 the cdr as a plist (see key/value matching above). 3860 3861 @var{a} shorthand notation for kv-destructuring exists which allows the 3862 patterns be optionally left out and derived from the key name in 3863 the following fashion: 3864 3865 - a key :foo is converted into @code{foo} pattern, 3866 - a key 'bar is converted into @code{bar} pattern, 3867 - a key "baz" is converted into @code{baz} pattern. 3868 3869 That is, the entire value under the key is bound to the derived 3870 variable without any further destructuring. 3871 3872 This is possible only when the form following the key is not a 3873 valid pattern (i.e. not a symbol, a cons cell or a vector). 3874 Otherwise the matching proceeds as usual and in case of an 3875 invalid spec fails with an error. 3876 3877 Thus the patterns are normalized as follows: 3878 3879 ;; derive all the missing patterns 3880 (&plist :foo 'bar "baz") => (&plist :foo foo 'bar bar "baz" baz) 3881 3882 ;; we can specify some but not others 3883 (&plist :foo 'bar explicit-bar) => (&plist :foo foo 'bar explicit-bar) 3884 3885 ;; nothing happens, we store :foo in x 3886 (&plist :foo x) => (&plist :foo x) 3887 3888 ;; nothing happens, we match recursively 3889 (&plist :foo (a b c)) => (&plist :foo (a b c)) 3890 3891 You can name the source using the syntax @var{symbol} &as @var{pattern}. 3892 This syntax works with lists (proper or improper), vectors and 3893 all types of maps. 3894 3895 (list &as a b c) (list 1 2 3) 3896 3897 binds @var{a} to 1, @var{b} to 2, @var{c} to 3 and @var{list} to (1 2 3). 3898 3899 Similarly: 3900 3901 (bounds &as beg . end) (cons 1 2) 3902 3903 binds @var{beg} to 1, @var{end} to 2 and @var{bounds} to (1 . 2). 3904 3905 (items &as first . rest) (list 1 2 3) 3906 3907 binds @var{first} to 1, @var{rest} to (2 3) and @var{items} to (1 2 3) 3908 3909 [vect &as _ b c] [1 2 3] 3910 3911 binds @var{b} to 2, @var{c} to 3 and @var{vect} to [1 2 3] (_ avoids binding as usual). 3912 3913 (plist &as &plist :b b) (list :a 1 :b 2 :c 3) 3914 3915 binds @var{b} to 2 and @var{plist} to (:a 1 :b 2 :c 3). Same for &alist and &hash. 3916 3917 This is especially useful when we want to capture the result of a 3918 computation and destructure at the same time. Consider the 3919 form (function-returning-complex-structure) returning a list of 3920 two vectors with two items each. We want to capture this entire 3921 result and pass it to another computation, but at the same time 3922 we want to get the second item from each vector. We can achieve 3923 it with pattern 3924 3925 (result &as [_ a] [_ b]) (function-returning-complex-structure) 3926 3927 Note: Clojure programmers may know this feature as the ":as 3928 binding". The difference is that we put the &as at the front 3929 because we need to support improper list binding. 3930 3931 @example 3932 @group 3933 (-let (([a (b c) d] [1 (2 3) 4])) (list a b c d)) 3934 @result{} (1 2 3 4) 3935 @end group 3936 @group 3937 (-let [(a b c . d) (list 1 2 3 4 5 6)] (list a b c d)) 3938 @result{} (1 2 3 (4 5 6)) 3939 @end group 3940 @group 3941 (-let [(&plist :foo foo :bar bar) (list :baz 3 :foo 1 :qux 4 :bar 2)] (list foo bar)) 3942 @result{} (1 2) 3943 @end group 3944 @end example 3945 @end defmac 3946 3947 @anchor{-let*} 3948 @defmac -let* (varlist &rest body) 3949 Bind variables according to @var{varlist} then eval @var{body}. 3950 3951 @var{varlist} is a list of lists of the form (@var{pattern} @var{source}). Each 3952 @var{pattern} is matched against the @var{source} structurally. @var{source} is 3953 only evaluated once for each @var{pattern}. 3954 3955 Each @var{source} can refer to the symbols already bound by this 3956 @var{varlist}. This is useful if you want to destructure @var{source} 3957 recursively but also want to name the intermediate structures. 3958 3959 See @code{-let} (@pxref{-let}) for the list of all possible patterns. 3960 3961 @example 3962 @group 3963 (-let* (((a . b) (cons 1 2)) ((c . d) (cons 3 4))) (list a b c d)) 3964 @result{} (1 2 3 4) 3965 @end group 3966 @group 3967 (-let* (((a . b) (cons 1 (cons 2 3))) ((c . d) b)) (list a b c d)) 3968 @result{} (1 (2 . 3) 2 3) 3969 @end group 3970 @group 3971 (-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)) 3972 @result{} (1 a b c (a b c)) 3973 @end group 3974 @end example 3975 @end defmac 3976 3977 @anchor{-lambda} 3978 @defmac -lambda (match-form &rest body) 3979 Return a lambda which destructures its input as @var{match-form} and executes @var{body}. 3980 3981 Note that you have to enclose the @var{match-form} in a pair of parens, 3982 such that: 3983 3984 (-lambda (x) body) 3985 (-lambda (x y @dots{}) body) 3986 3987 has the usual semantics of @code{lambda}. Furthermore, these get 3988 translated into normal @code{lambda}, so there is no performance 3989 penalty. 3990 3991 See @code{-let} (@pxref{-let}) for a description of the destructuring mechanism. 3992 3993 @example 3994 @group 3995 (-map (-lambda ((x y)) (+ x y)) '((1 2) (3 4) (5 6))) 3996 @result{} (3 7 11) 3997 @end group 3998 @group 3999 (-map (-lambda ([x y]) (+ x y)) '([1 2] [3 4] [5 6])) 4000 @result{} (3 7 11) 4001 @end group 4002 @group 4003 (funcall (-lambda ((_ . a) (_ . b)) (-concat a b)) '(1 2 3) '(4 5 6)) 4004 @result{} (2 3 5 6) 4005 @end group 4006 @end example 4007 @end defmac 4008 4009 @anchor{-setq} 4010 @defmac -setq ([match-form val] ...) 4011 Bind each @var{match-form} to the value of its @var{val}. 4012 4013 @var{match-form} destructuring is done according to the rules of @code{-let} (@pxref{-let}). 4014 4015 This macro allows you to bind multiple variables by destructuring 4016 the value, so for example: 4017 4018 (-setq (a b) x 4019 (&plist :c c) plist) 4020 4021 expands roughly speaking to the following code 4022 4023 (setq a (car x) 4024 b (cadr x) 4025 c (plist-get plist :c)) 4026 4027 Care is taken to only evaluate each @var{val} once so that in case of 4028 multiple assignments it does not cause unexpected side effects. 4029 4030 @example 4031 @group 4032 (let (a) (-setq a 1) a) 4033 @result{} 1 4034 @end group 4035 @group 4036 (let (a b) (-setq (a b) (list 1 2)) (list a b)) 4037 @result{} (1 2) 4038 @end group 4039 @group 4040 (let (c) (-setq (&plist :c c) (list :c "c")) c) 4041 @result{} "c" 4042 @end group 4043 @end example 4044 @end defmac 4045 4046 @node Side effects 4047 @section Side effects 4048 4049 Functions iterating over lists for side effect only. 4050 4051 @anchor{-each} 4052 @defun -each (list fn) 4053 Call @var{fn} on each element of @var{list}. 4054 Return nil; this function is intended for side effects. 4055 4056 Its anaphoric counterpart is @code{--each}. 4057 4058 For access to the current element's index in @var{list}, see 4059 @code{-each-indexed} (@pxref{-each-indexed}). 4060 4061 @example 4062 @group 4063 (let (l) (-each '(1 2 3) (lambda (x) (push x l))) l) 4064 @result{} (3 2 1) 4065 @end group 4066 @group 4067 (let (l) (--each '(1 2 3) (push it l)) l) 4068 @result{} (3 2 1) 4069 @end group 4070 @group 4071 (-each '(1 2 3) #'identity) 4072 @result{} nil 4073 @end group 4074 @end example 4075 @end defun 4076 4077 @anchor{-each-while} 4078 @defun -each-while (list pred fn) 4079 Call @var{fn} on each @var{item} in @var{list}, while (@var{pred} @var{item}) is non-nil. 4080 Once an @var{item} is reached for which @var{pred} returns nil, @var{fn} is no 4081 longer called. Return nil; this function is intended for side 4082 effects. 4083 4084 Its anaphoric counterpart is @code{--each-while}. 4085 4086 @example 4087 @group 4088 (let (l) (-each-while '(2 4 5 6) #'even? (lambda (x) (push x l))) l) 4089 @result{} (4 2) 4090 @end group 4091 @group 4092 (let (l) (--each-while '(1 2 3 4) (< it 3) (push it l)) l) 4093 @result{} (2 1) 4094 @end group 4095 @group 4096 (let ((s 0)) (--each-while '(1 3 4 5) (< it 5) (setq s (+ s it))) s) 4097 @result{} 8 4098 @end group 4099 @end example 4100 @end defun 4101 4102 @anchor{-each-indexed} 4103 @defun -each-indexed (list fn) 4104 Call @var{fn} on each index and element of @var{list}. 4105 For each @var{item} at @var{index} in @var{list}, call (funcall @var{fn} @var{index} @var{item}). 4106 Return nil; this function is intended for side effects. 4107 4108 See also: @code{-map-indexed} (@pxref{-map-indexed}). 4109 4110 @example 4111 @group 4112 (let (l) (-each-indexed '(a b c) (lambda (i x) (push (list x i) l))) l) 4113 @result{} ((c 2) (b 1) (a 0)) 4114 @end group 4115 @group 4116 (let (l) (--each-indexed '(a b c) (push (list it it-index) l)) l) 4117 @result{} ((c 2) (b 1) (a 0)) 4118 @end group 4119 @group 4120 (let (l) (--each-indexed () (push it l)) l) 4121 @result{} () 4122 @end group 4123 @end example 4124 @end defun 4125 4126 @anchor{-each-r} 4127 @defun -each-r (list fn) 4128 Call @var{fn} on each element of @var{list} in reversed order. 4129 Return nil; this function is intended for side effects. 4130 4131 Its anaphoric counterpart is @code{--each-r}. 4132 4133 @example 4134 @group 4135 (let (l) (-each-r '(1 2 3) (lambda (x) (push x l))) l) 4136 @result{} (1 2 3) 4137 @end group 4138 @group 4139 (let (l) (--each-r '(1 2 3) (push it l)) l) 4140 @result{} (1 2 3) 4141 @end group 4142 @group 4143 (-each-r '(1 2 3) #'identity) 4144 @result{} nil 4145 @end group 4146 @end example 4147 @end defun 4148 4149 @anchor{-each-r-while} 4150 @defun -each-r-while (list pred fn) 4151 Call @var{fn} on each @var{item} in reversed @var{list}, while (@var{pred} @var{item}) is non-nil. 4152 Once an @var{item} is reached for which @var{pred} returns nil, @var{fn} is no 4153 longer called. Return nil; this function is intended for side 4154 effects. 4155 4156 Its anaphoric counterpart is @code{--each-r-while}. 4157 4158 @example 4159 @group 4160 (let (l) (-each-r-while '(2 4 5 6) #'even? (lambda (x) (push x l))) l) 4161 @result{} (6) 4162 @end group 4163 @group 4164 (let (l) (--each-r-while '(1 2 3 4) (>= it 3) (push it l)) l) 4165 @result{} (3 4) 4166 @end group 4167 @group 4168 (let ((s 0)) (--each-r-while '(1 2 3 5) (> it 1) (setq s (+ s it))) s) 4169 @result{} 10 4170 @end group 4171 @end example 4172 @end defun 4173 4174 @anchor{-dotimes} 4175 @defun -dotimes (num fn) 4176 Call @var{fn} @var{num} times, presumably for side effects. 4177 @var{fn} is called with a single argument on successive integers 4178 running from 0, inclusive, to @var{num}, exclusive. @var{fn} is not called 4179 if @var{num} is less than 1. 4180 4181 This function's anaphoric counterpart is @code{--dotimes}. 4182 4183 @example 4184 @group 4185 (let (s) (-dotimes 3 (lambda (n) (push n s))) s) 4186 @result{} (2 1 0) 4187 @end group 4188 @group 4189 (let (s) (-dotimes 0 (lambda (n) (push n s))) s) 4190 @result{} () 4191 @end group 4192 @group 4193 (let (s) (--dotimes 5 (push it s)) s) 4194 @result{} (4 3 2 1 0) 4195 @end group 4196 @end example 4197 @end defun 4198 4199 @node Destructive operations 4200 @section Destructive operations 4201 4202 Macros that modify variables holding lists. 4203 4204 @anchor{!cons} 4205 @defmac !cons (car cdr) 4206 Destructive: Set @var{cdr} to the cons of @var{car} and @var{cdr}. 4207 4208 @example 4209 @group 4210 (let (l) (!cons 5 l) l) 4211 @result{} (5) 4212 @end group 4213 @group 4214 (let ((l '(3))) (!cons 5 l) l) 4215 @result{} (5 3) 4216 @end group 4217 @end example 4218 @end defmac 4219 4220 @anchor{!cdr} 4221 @defmac !cdr (list) 4222 Destructive: Set @var{list} to the cdr of @var{list}. 4223 4224 @example 4225 @group 4226 (let ((l '(3))) (!cdr l) l) 4227 @result{} () 4228 @end group 4229 @group 4230 (let ((l '(3 5))) (!cdr l) l) 4231 @result{} (5) 4232 @end group 4233 @end example 4234 @end defmac 4235 4236 @node Function combinators 4237 @section Function combinators 4238 4239 Functions that manipulate and compose other functions. 4240 4241 @anchor{-partial} 4242 @defun -partial (fun &rest args) 4243 Return a function that is a partial application of @var{fun} to @var{args}. 4244 @var{args} is a list of the first @var{n} arguments to pass to @var{fun}. 4245 The result is a new function which does the same as @var{fun}, except that 4246 the first @var{n} arguments are fixed at the values with which this function 4247 was called. 4248 4249 @example 4250 @group 4251 (funcall (-partial #'+ 5)) 4252 @result{} 5 4253 @end group 4254 @group 4255 (funcall (-partial #'- 5) 3) 4256 @result{} 2 4257 @end group 4258 @group 4259 (funcall (-partial #'+ 5 2) 3) 4260 @result{} 10 4261 @end group 4262 @end example 4263 @end defun 4264 4265 @anchor{-rpartial} 4266 @defun -rpartial (fn &rest args) 4267 Return a function that is a partial application of @var{fn} to @var{args}. 4268 @var{args} is a list of the last @var{n} arguments to pass to @var{fn}. The result 4269 is a new function which does the same as @var{fn}, except that the last 4270 @var{n} arguments are fixed at the values with which this function was 4271 called. This is like @code{-partial} (@pxref{-partial}), except the arguments are fixed 4272 starting from the right rather than the left. 4273 4274 @example 4275 @group 4276 (funcall (-rpartial #'- 5)) 4277 @result{} -5 4278 @end group 4279 @group 4280 (funcall (-rpartial #'- 5) 8) 4281 @result{} 3 4282 @end group 4283 @group 4284 (funcall (-rpartial #'- 5 2) 10) 4285 @result{} 3 4286 @end group 4287 @end example 4288 @end defun 4289 4290 @anchor{-juxt} 4291 @defun -juxt (&rest fns) 4292 Return a function that is the juxtaposition of @var{fns}. 4293 The returned function takes a variable number of @var{args}, applies 4294 each of @var{fns} in turn to @var{args}, and returns the list of results. 4295 4296 @example 4297 @group 4298 (funcall (-juxt) 1 2) 4299 @result{} () 4300 @end group 4301 @group 4302 (funcall (-juxt #'+ #'- #'* #'/) 7 5) 4303 @result{} (12 2 35 1) 4304 @end group 4305 @group 4306 (mapcar (-juxt #'number-to-string #'1+) '(1 2)) 4307 @result{} (("1" 2) ("2" 3)) 4308 @end group 4309 @end example 4310 @end defun 4311 4312 @anchor{-compose} 4313 @defun -compose (&rest fns) 4314 Compose @var{fns} into a single composite function. 4315 Return a function that takes a variable number of @var{args}, applies 4316 the last function in @var{fns} to @var{args}, and returns the result of 4317 calling each remaining function on the result of the previous 4318 function, right-to-left. If no @var{fns} are given, return a variadic 4319 @code{identity} function. 4320 4321 @example 4322 @group 4323 (funcall (-compose #'- #'1+ #'+) 1 2 3) 4324 @result{} -7 4325 @end group 4326 @group 4327 (funcall (-compose #'identity #'1+) 3) 4328 @result{} 4 4329 @end group 4330 @group 4331 (mapcar (-compose #'not #'stringp) '(nil "")) 4332 @result{} (t nil) 4333 @end group 4334 @end example 4335 @end defun 4336 4337 @anchor{-applify} 4338 @defun -applify (fn) 4339 Return a function that applies @var{fn} to a single list of args. 4340 This changes the arity of @var{fn} from taking @var{n} distinct arguments to 4341 taking 1 argument which is a list of @var{n} arguments. 4342 4343 @example 4344 @group 4345 (funcall (-applify #'+) nil) 4346 @result{} 0 4347 @end group 4348 @group 4349 (mapcar (-applify #'+) '((1 1 1) (1 2 3) (5 5 5))) 4350 @result{} (3 6 15) 4351 @end group 4352 @group 4353 (funcall (-applify #'<) '(3 6)) 4354 @result{} t 4355 @end group 4356 @end example 4357 @end defun 4358 4359 @anchor{-on} 4360 @defun -on (op trans) 4361 Return a function that calls @var{trans} on each arg and @var{op} on the results. 4362 The returned function takes a variable number of arguments, calls 4363 the function @var{trans} on each one in turn, and then passes those 4364 results as the list of arguments to @var{op}, in the same order. 4365 4366 For example, the following pairs of expressions are morally 4367 equivalent: 4368 4369 (funcall (-on #'+ #'1+) 1 2 3) = (+ (1+ 1) (1+ 2) (1+ 3)) 4370 (funcall (-on #'+ #'1+)) = (+) 4371 4372 @example 4373 @group 4374 (-sort (-on #'< #'length) '((1 2 3) (1) (1 2))) 4375 @result{} ((1) (1 2) (1 2 3)) 4376 @end group 4377 @group 4378 (funcall (-on #'min #'string-to-number) "22" "2" "1" "12") 4379 @result{} 1 4380 @end group 4381 @group 4382 (-min-by (-on #'> #'length) '((1 2 3) (4) (1 2))) 4383 @result{} (4) 4384 @end group 4385 @end example 4386 @end defun 4387 4388 @anchor{-flip} 4389 @defun -flip (fn) 4390 Return a function that calls @var{fn} with its arguments reversed. 4391 The returned function takes the same number of arguments as @var{fn}. 4392 4393 For example, the following two expressions are morally 4394 equivalent: 4395 4396 (funcall (-flip #'-) 1 2) = (- 2 1) 4397 4398 See also: @code{-rotate-args} (@pxref{-rotate-args}). 4399 4400 @example 4401 @group 4402 (-sort (-flip #'<) '(4 3 6 1)) 4403 @result{} (6 4 3 1) 4404 @end group 4405 @group 4406 (funcall (-flip #'-) 3 2 1 10) 4407 @result{} 4 4408 @end group 4409 @group 4410 (funcall (-flip #'1+) 1) 4411 @result{} 2 4412 @end group 4413 @end example 4414 @end defun 4415 4416 @anchor{-rotate-args} 4417 @defun -rotate-args (n fn) 4418 Return a function that calls @var{fn} with args rotated @var{n} places to the right. 4419 The returned function takes the same number of arguments as @var{fn}, 4420 rotates the list of arguments @var{n} places to the right (left if @var{n} is 4421 negative) just like @code{-rotate} (@pxref{-rotate}), and applies @var{fn} to the result. 4422 4423 See also: @code{-flip} (@pxref{-flip}). 4424 4425 @example 4426 @group 4427 (funcall (-rotate-args -1 #'list) 1 2 3 4) 4428 @result{} (2 3 4 1) 4429 @end group 4430 @group 4431 (funcall (-rotate-args 1 #'-) 1 10 100) 4432 @result{} 89 4433 @end group 4434 @group 4435 (funcall (-rotate-args 2 #'list) 3 4 5 1 2) 4436 @result{} (1 2 3 4 5) 4437 @end group 4438 @end example 4439 @end defun 4440 4441 @anchor{-const} 4442 @defun -const (c) 4443 Return a function that returns @var{c} ignoring any additional arguments. 4444 4445 In types: a -> b -> a 4446 4447 @example 4448 @group 4449 (funcall (-const 2) 1 3 "foo") 4450 @result{} 2 4451 @end group 4452 @group 4453 (mapcar (-const 1) '("a" "b" "c" "d")) 4454 @result{} (1 1 1 1) 4455 @end group 4456 @group 4457 (-sum (mapcar (-const 1) '("a" "b" "c" "d"))) 4458 @result{} 4 4459 @end group 4460 @end example 4461 @end defun 4462 4463 @anchor{-cut} 4464 @defmac -cut (&rest params) 4465 Take n-ary function and n arguments and specialize some of them. 4466 Arguments denoted by <> will be left unspecialized. 4467 4468 See @var{srfi-26} for detailed description. 4469 4470 @example 4471 @group 4472 (funcall (-cut list 1 <> 3 <> 5) 2 4) 4473 @result{} (1 2 3 4 5) 4474 @end group 4475 @group 4476 (-map (-cut funcall <> 5) `(1+ 1- ,(lambda (x) (/ 1.0 x)))) 4477 @result{} (6 4 0.2) 4478 @end group 4479 @group 4480 (-map (-cut <> 1 2 3) '(list vector string)) 4481 @result{} ((1 2 3) [1 2 3] "\1\2\3") 4482 @end group 4483 @end example 4484 @end defmac 4485 4486 @anchor{-not} 4487 @defun -not (pred) 4488 Return a predicate that negates the result of @var{pred}. 4489 The returned predicate passes its arguments to @var{pred}. If @var{pred} 4490 returns nil, the result is non-nil; otherwise the result is nil. 4491 4492 See also: @code{-andfn} (@pxref{-andfn}) and @code{-orfn} (@pxref{-orfn}). 4493 4494 @example 4495 @group 4496 (funcall (-not #'numberp) "5") 4497 @result{} t 4498 @end group 4499 @group 4500 (-sort (-not #'<) '(5 2 1 0 6)) 4501 @result{} (6 5 2 1 0) 4502 @end group 4503 @group 4504 (-filter (-not (-partial #'< 4)) '(1 2 3 4 5 6 7 8)) 4505 @result{} (1 2 3 4) 4506 @end group 4507 @end example 4508 @end defun 4509 4510 @anchor{-orfn} 4511 @defun -orfn (&rest preds) 4512 Return a predicate that returns the first non-nil result of @var{preds}. 4513 The returned predicate takes a variable number of arguments, 4514 passes them to each predicate in @var{preds} in turn until one of them 4515 returns non-nil, and returns that non-nil result without calling 4516 the remaining @var{preds}. If all @var{preds} return nil, or if no @var{preds} are 4517 given, the returned predicate returns nil. 4518 4519 See also: @code{-andfn} (@pxref{-andfn}) and @code{-not} (@pxref{-not}). 4520 4521 @example 4522 @group 4523 (-filter (-orfn #'natnump #'booleanp) '(1 nil "a" -4 b c t)) 4524 @result{} (1 nil t) 4525 @end group 4526 @group 4527 (funcall (-orfn #'symbolp (-cut string-match-p "x" <>)) "axe") 4528 @result{} 1 4529 @end group 4530 @group 4531 (funcall (-orfn #'= #'+) 1 1) 4532 @result{} t 4533 @end group 4534 @end example 4535 @end defun 4536 4537 @anchor{-andfn} 4538 @defun -andfn (&rest preds) 4539 Return a predicate that returns non-nil if all @var{preds} do so. 4540 The returned predicate @var{p} takes a variable number of arguments and 4541 passes them to each predicate in @var{preds} in turn. If any one of 4542 @var{preds} returns nil, @var{p} also returns nil without calling the 4543 remaining @var{preds}. If all @var{preds} return non-nil, @var{p} returns the last 4544 such value. If no @var{preds} are given, @var{p} always returns non-nil. 4545 4546 See also: @code{-orfn} (@pxref{-orfn}) and @code{-not} (@pxref{-not}). 4547 4548 @example 4549 @group 4550 (-filter (-andfn #'numberp (-cut < <> 5)) '(a 1 b 6 c 2)) 4551 @result{} (1 2) 4552 @end group 4553 @group 4554 (mapcar (-andfn #'numberp #'1+) '(a 1 b 6)) 4555 @result{} (nil 2 nil 7) 4556 @end group 4557 @group 4558 (funcall (-andfn #'= #'+) 1 1) 4559 @result{} 2 4560 @end group 4561 @end example 4562 @end defun 4563 4564 @anchor{-iteratefn} 4565 @defun -iteratefn (fn n) 4566 Return a function @var{fn} composed @var{n} times with itself. 4567 4568 @var{fn} is a unary function. If you need to use a function of higher 4569 arity, use @code{-applify} (@pxref{-applify}) first to turn it into a unary function. 4570 4571 With n = 0, this acts as identity function. 4572 4573 In types: (a -> a) -> Int -> a -> a. 4574 4575 This function satisfies the following law: 4576 4577 (funcall (-iteratefn fn n) init) = (-last-item (-iterate fn init (1+ n))). 4578 4579 @example 4580 @group 4581 (funcall (-iteratefn (lambda (x) (* x x)) 3) 2) 4582 @result{} 256 4583 @end group 4584 @group 4585 (funcall (-iteratefn '1+ 3) 1) 4586 @result{} 4 4587 @end group 4588 @group 4589 (funcall (-iteratefn 'cdr 3) '(1 2 3 4 5)) 4590 @result{} (4 5) 4591 @end group 4592 @end example 4593 @end defun 4594 4595 @anchor{-fixfn} 4596 @defun -fixfn (fn &optional equal-test halt-test) 4597 Return a function that computes the (least) fixpoint of @var{fn}. 4598 4599 @var{fn} must be a unary function. The returned lambda takes a single 4600 argument, @var{x}, the initial value for the fixpoint iteration. The 4601 iteration halts when either of the following conditions is satisfied: 4602 4603 1. Iteration converges to the fixpoint, with equality being 4604 tested using @var{equal-test}. If @var{equal-test} is not specified, 4605 @code{equal} is used. For functions over the floating point 4606 numbers, it may be necessary to provide an appropriate 4607 approximate comparison test. 4608 4609 2. @var{halt-test} returns a non-nil value. @var{halt-test} defaults to a 4610 simple counter that returns t after @code{-fixfn-max-iterations}, 4611 to guard against infinite iteration. Otherwise, @var{halt-test} 4612 must be a function that accepts a single argument, the 4613 current value of @var{x}, and returns non-nil as long as iteration 4614 should continue. In this way, a more sophisticated 4615 convergence test may be supplied by the caller. 4616 4617 The return value of the lambda is either the fixpoint or, if 4618 iteration halted before converging, a cons with car @code{halted} and 4619 cdr the final output from @var{halt-test}. 4620 4621 In types: (a -> a) -> a -> a. 4622 4623 @example 4624 @group 4625 (funcall (-fixfn #'cos #'approx=) 0.7) 4626 @result{} 0.7390851332151607 4627 @end group 4628 @group 4629 (funcall (-fixfn (lambda (x) (expt (+ x 10) 0.25))) 2.0) 4630 @result{} 1.8555845286409378 4631 @end group 4632 @group 4633 (funcall (-fixfn #'sin #'approx=) 0.1) 4634 @result{} (halted . t) 4635 @end group 4636 @end example 4637 @end defun 4638 4639 @anchor{-prodfn} 4640 @defun -prodfn (&rest fns) 4641 Take a list of n functions and return a function that takes a 4642 list of length n, applying i-th function to i-th element of the 4643 input list. Returns a list of length n. 4644 4645 In types (for n=2): ((a -> b), (c -> d)) -> (a, c) -> (b, d) 4646 4647 This function satisfies the following laws: 4648 4649 (-compose (-prodfn f g @dots{}) (-prodfn f' g' @dots{})) = (-prodfn (-compose f f') (-compose g g') @dots{}) 4650 (-prodfn f g @dots{}) = (-juxt (-compose f (-partial 'nth 0)) (-compose g (-partial 'nth 1)) @dots{}) 4651 (-compose (-prodfn f g @dots{}) (-juxt f' g' @dots{})) = (-juxt (-compose f f') (-compose g g') @dots{}) 4652 (-compose (-partial 'nth n) (-prod f1 f2 @dots{})) = (-compose fn (-partial 'nth n)) 4653 4654 @example 4655 @group 4656 (funcall (-prodfn '1+ '1- 'number-to-string) '(1 2 3)) 4657 @result{} (2 1 "3") 4658 @end group 4659 @group 4660 (-map (-prodfn '1+ '1-) '((1 2) (3 4) (5 6) (7 8))) 4661 @result{} ((2 1) (4 3) (6 5) (8 7)) 4662 @end group 4663 @group 4664 (apply '+ (funcall (-prodfn 'length 'string-to-number) '((1 2 3) "15"))) 4665 @result{} 18 4666 @end group 4667 @end example 4668 @end defun 4669 4670 @node Development 4671 @chapter Development 4672 4673 The Dash repository is hosted on GitHub at 4674 @url{https://github.com/magnars/dash.el}. 4675 4676 @menu 4677 * Contribute:: How to contribute. 4678 * Contributors:: List of contributors. 4679 @end menu 4680 4681 @node Contribute 4682 @section Contribute 4683 4684 Yes, please do. Pure functions in the list manipulation realm only, 4685 please. There's a suite of examples/tests in @file{dev/examples.el}, 4686 so remember to add tests for your additions, or they may get broken 4687 later. 4688 4689 Run the tests with @samp{make check}. Regenerate the docs with 4690 @samp{make docs}. Contributors are encouraged to install these 4691 commands as a Git pre-commit hook, so that the tests are always 4692 running and the docs are always in sync: 4693 4694 @example 4695 $ cp dev/pre-commit.sh .git/hooks/pre-commit 4696 @end example 4697 4698 Oh, and don't edit @file{README.md} or @file{dash.texi} directly, as 4699 they are auto-generated. Instead, change their respective templates 4700 @file{readme-template.md} or @file{dash-template.texi}. 4701 4702 To ensure that Dash can be distributed with GNU ELPA or Emacs, we 4703 require that all contributors assign copyright to the Free Software 4704 Foundation. For more on this, @pxref{Copyright Assignment,,, emacs, 4705 The GNU Emacs Manual}. 4706 4707 @node Contributors 4708 @section Contributors 4709 4710 @itemize 4711 @item 4712 @url{https://github.com/Fuco1, Matus Goljer} contributed lots of 4713 features and functions. 4714 @item 4715 @url{https://github.com/tkf, Takafumi Arakaki} contributed 4716 @code{-group-by}. 4717 @item 4718 @url{https://github.com/tali713, tali713} is the author of 4719 @code{-applify}. 4720 @item 4721 @url{https://github.com/vemv, V@'{i}ctor M. Valenzuela} contributed 4722 @code{-repeat}. 4723 @item 4724 @url{https://github.com/nicferrier, Nic Ferrier} contributed 4725 @code{-cons*}. 4726 @item 4727 @url{https://github.com/Wilfred, Wilfred Hughes} contributed 4728 @code{-slice}, @code{-first-item}, and @code{-last-item}. 4729 @item 4730 @url{https://github.com/shosti, Emanuel Evans} contributed 4731 @code{-if-let}, @code{-when-let}, and @code{-insert-at}. 4732 @item 4733 @url{https://github.com/rejeep, Johan Andersson} contributed 4734 @code{-sum}, @code{-product}, and @code{-same-items?}. 4735 @item 4736 @url{https://github.com/kurisuwhyte, Christina Whyte} contributed 4737 @code{-compose}. 4738 @item 4739 @url{https://github.com/steventlamb, Steve Lamb} contributed 4740 @code{-cycle}, @code{-pad}, @code{-annotate}, @code{-zip-fill}, and a 4741 variadic version of @code{-zip}. 4742 @item 4743 @url{https://github.com/fbergroth, Fredrik Bergroth} made the 4744 @code{-if-let} family use @code{-let} destructuring and improved the 4745 script for generating documentation. 4746 @item 4747 @url{https://github.com/holomorph, Mark Oteiza} contributed 4748 @code{-iota} and the script to create an Info manual. 4749 @item 4750 @url{https://github.com/wasamasa, Vasilij Schneidermann} contributed 4751 @code{-some}. 4752 @item 4753 @url{https://github.com/occidens, William West} made @code{-fixfn} 4754 more robust at handling floats. 4755 @item 4756 @url{https://github.com/camsaul, Cam Saul} contributed @code{-some->}, 4757 @code{-some->>}, and @code{-some-->}. 4758 @item 4759 @url{https://github.com/basil-conto, Basil L. Contovounesios} 4760 contributed @code{-common-prefix}, @code{-common-suffix}, and various 4761 other improvements. 4762 @item 4763 @url{https://github.com/doublep, Paul Pogonyshev} contributed 4764 @code{-each-r} and @code{-each-r-while}. 4765 @end itemize 4766 4767 Thanks! 4768 4769 New contributors are very welcome. @xref{Contribute}. 4770 4771 @c Appendices. 4772 4773 @node FDL 4774 @appendix GNU Free Documentation License 4775 @include doc/fdl.texi 4776 4777 @node GPL 4778 @appendix GNU General Public License 4779 @include doc/gpl.texi 4780 4781 @node Index 4782 @unnumbered Index 4783 @printindex fn 4784 4785 @bye