diff --git a/README.md b/README.md index 35bf929..041dfe3 100644 --- a/README.md +++ b/README.md @@ -265,7 +265,7 @@ While `-map` takes a function to map over the list, you can also use the anaphoric form with double dashes - which will then be executed with `it` exposed as the list item. Here's an example: -```cl +```el (-map (lambda (n) (* n n)) '(1 2 3 4)) ;; normal version (--map (* it it) '(1 2 3 4)) ;; anaphoric version @@ -273,7 +273,7 @@ with `it` exposed as the list item. Here's an example: of course the original can also be written like -```cl +```el (defun square (n) (* n n)) (-map 'square '(1 2 3 4)) @@ -294,7 +294,7 @@ new list. Return a new list consisting of the result of applying `fn` to the items in `list`. -```cl +```el (-map (lambda (num) (* num num)) '(1 2 3 4)) ;; => '(1 4 9 16) (-map 'square '(1 2 3 4)) ;; => '(1 4 9 16) (--map (* it it) '(1 2 3 4)) ;; => '(1 4 9 16) @@ -308,9 +308,9 @@ through the `rep` function. Alias: `-replace-where` -See also: `-update-at` +See also: [`-update-at`](#-update-at-n-func-list) -```cl +```el (-map-when 'even? 'square '(1 2 3 4)) ;; => '(1 4 3 16) (--map-when (> it 2) (* it it) '(1 2 3 4)) ;; => '(1 2 9 16) (--map-when (= it 2) 17 '(1 2 3 4)) ;; => '(1 17 3 4) @@ -322,7 +322,7 @@ Return a new list consisting of the result of (`fn` index item) for each item in In the anaphoric form `--map-indexed`, the index is exposed as `it-index`. -```cl +```el (-map-indexed (lambda (index item) (- item index)) '(1 2 3 4)) ;; => '(1 1 1 1) (--map-indexed (- it it-index) '(1 2 3 4)) ;; => '(1 1 1 1) ``` @@ -332,7 +332,7 @@ In the anaphoric form `--map-indexed`, the index is exposed as `it-index`. Return a list of cons cells where each cell is `fn` applied to each element of `list` paired with the unmodified element of `list`. -```cl +```el (-annotate '1+ '(1 2 3)) ;; => '((2 . 1) (3 . 2) (4 . 3)) (-annotate 'length '(("h" "e" "l" "l" "o") ("hello" "world"))) ;; => '((5 "h" "e" "l" "l" "o") (2 "hello" "world")) (--annotate (< 1 it) '(0 1 2 3)) ;; => '((nil . 0) (nil . 1) (t . 2) (t . 3)) @@ -348,9 +348,9 @@ This function can be used as replacement for `,@` in case you need to splice several lists at marked positions (for example with keywords). -See also: `-splice-list`, `-insert-at` +See also: [`-splice-list`](#-splice-list-pred-new-list-list), [`-insert-at`](#-insert-at-n-x-list) -```cl +```el (-splice 'even? (lambda (x) (list x x)) '(1 2 3 4)) ;; => '(1 2 2 3 4 4) (--splice 't (list it it) '(1 2 3 4)) ;; => '(1 1 2 2 3 3 4 4) (--splice (equal it :magic) '((list of) (magical) (code)) '((foo) (bar) :magic (baz))) ;; => '((foo) (bar) (list of) (magical) (code) (baz)) @@ -360,9 +360,9 @@ See also: `-splice-list`, `-insert-at` Splice `new-list` in place of elements matching `pred` in `list`. -See also: `-splice`, `-insert-at` +See also: [`-splice`](#-splice-pred-fun-list), [`-insert-at`](#-insert-at-n-x-list) -```cl +```el (-splice-list 'keywordp '(a b c) '(1 :foo 2)) ;; => '(1 a b c 2) (-splice-list 'keywordp nil '(1 :foo 2)) ;; => '(1 2) ``` @@ -372,7 +372,7 @@ See also: `-splice`, `-insert-at` Return the concatenation of the result of mapping `fn` over `list`. Thus function `fn` should return a list. -```cl +```el (-mapcat 'list '(1 2 3)) ;; => '(1 2 3) (-mapcat (lambda (item) (list 0 item)) '(1 2 3)) ;; => '(0 1 0 2 0 3) (--mapcat (list 0 it) '(1 2 3)) ;; => '(0 1 0 2 0 3) @@ -382,7 +382,7 @@ Thus function `fn` should return a list. Create a shallow copy of `list`. -```cl +```el (-copy '(1 2 3)) ;; => '(1 2 3) (let ((a '(1 2 3))) (eq a (-copy a))) ;; => nil ``` @@ -399,7 +399,7 @@ Return a new list of the items in `list` for which `pred` returns a non-nil valu Alias: `-select` -```cl +```el (-filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) ;; => '(2 4) (-filter 'even? '(1 2 3 4)) ;; => '(2 4) (--filter (= 0 (% it 2)) '(1 2 3 4)) ;; => '(2 4) @@ -411,7 +411,7 @@ Return a new list of the items in `list` for which `pred` returns nil. Alias: `-reject` -```cl +```el (-remove (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) ;; => '(1 3) (-remove 'even? '(1 2 3 4)) ;; => '(1 3) (--remove (= 0 (% it 2)) '(1 2 3 4)) ;; => '(1 3) @@ -421,7 +421,7 @@ Alias: `-reject` Return all non-nil elements of `list`. -```cl +```el (-non-nil '(1 nil 2 nil nil 3 4 nil 5 nil)) ;; => '(1 2 3 4 5) ``` @@ -435,7 +435,7 @@ modulo the length of the list. If `step` is a number, only each STEPth item in the resulting section is returned. Defaults to 1. -```cl +```el (-slice '(1 2 3 4 5) 1) ;; => '(2 3 4 5) (-slice '(1 2 3 4 5) 0 3) ;; => '(1 2 3) (-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2) ;; => '(2 4 6 8) @@ -445,7 +445,7 @@ section is returned. Defaults to 1. Return a new list of the first `n` items in `list`, or all items if there are fewer than `n`. -```cl +```el (-take 3 '(1 2 3 4 5)) ;; => '(1 2 3) (-take 17 '(1 2 3 4 5)) ;; => '(1 2 3 4 5) ``` @@ -454,7 +454,7 @@ Return a new list of the first `n` items in `list`, or all items if there are fe Return the tail of `list` without the first `n` items. -```cl +```el (-drop 3 '(1 2 3 4 5)) ;; => '(4 5) (-drop 17 '(1 2 3 4 5)) ;; => '() ``` @@ -463,7 +463,7 @@ Return the tail of `list` without the first `n` items. Return a new list of successive items from `list` while (`pred` item) returns a non-nil value. -```cl +```el (-take-while 'even? '(1 2 3 4)) ;; => '() (-take-while 'even? '(2 4 5 6)) ;; => '(2 4) (--take-while (< it 4) '(1 2 3 4 3 2 1)) ;; => '(1 2 3) @@ -473,7 +473,7 @@ Return a new list of successive items from `list` while (`pred` item) returns a Return the tail of `list` starting from the first item for which (`pred` item) returns nil. -```cl +```el (-drop-while 'even? '(1 2 3 4)) ;; => '(1 2 3 4) (-drop-while 'even? '(2 4 5 6)) ;; => '(5 6) (--drop-while (< it 4) '(1 2 3 4 3 2 1)) ;; => '(4 3 2 1) @@ -484,7 +484,7 @@ Return the tail of `list` starting from the first item for which (`pred` item) r Return a list whose elements are elements from `list` selected as `(nth i list)` for all i from `indices`. -```cl +```el (-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p" "t" "o" "r")) ;; => '("c" "o" "l" "o" "r") (-select-by-indices '(2 1 0) '("a" "b" "c")) ;; => '("c" "b" "a") (-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) ;; => '("f" "a" "r" "f" "a" "l" "l" "a") @@ -500,7 +500,7 @@ Bag of various functions which modify input list. Return a new list of the non-nil results of applying `fn` to the items in `list`. -```cl +```el (-keep 'cdr '((1 2 3) (4 5) (6))) ;; => '((2 3) (5)) (-keep (lambda (num) (when (> num 3) (* 10 num))) '(1 2 3 4 5 6)) ;; => '(40 50 60) (--keep (when (> it 3) (* 10 it)) '(1 2 3 4 5 6)) ;; => '(40 50 60) @@ -510,7 +510,7 @@ Return a new list of the non-nil results of applying `fn` to the items in `list` Return a new list with the concatenation of the elements in the supplied `lists`. -```cl +```el (-concat '(1)) ;; => '(1) (-concat '(1) '(2)) ;; => '(1 2) (-concat '(1) '(2 3) '(4)) ;; => '(1 2 3 4) @@ -520,9 +520,9 @@ Return a new list with the concatenation of the elements in the supplied `lists` Take a nested list `l` and return its contents as a single, flat list. -See also: `-flatten-n` +See also: [`-flatten-n`](#-flatten-n-num-list) -```cl +```el (-flatten '((1))) ;; => '(1) (-flatten '((1 (2 3) (((4 (5))))))) ;; => '(1 2 3 4 5) (-flatten '(1 2 (3 . 4))) ;; => '(1 2 (3 . 4)) @@ -532,9 +532,9 @@ See also: `-flatten-n` Flatten `num` levels of a nested `list`. -See also: `-flatten` +See also: [`-flatten`](#-flatten-l) -```cl +```el (-flatten-n 1 '((1 2) ((3 4) ((5 6))))) ;; => '(1 2 (3 4) ((5 6))) (-flatten-n 2 '((1 2) ((3 4) ((5 6))))) ;; => '(1 2 3 4 (5 6)) (-flatten-n 3 '((1 2) ((3 4) ((5 6))))) ;; => '(1 2 3 4 5 6) @@ -546,9 +546,9 @@ Replace all `old` items in `list` with `new`. Elements are compared using `equal`. -See also: `-replace-at` +See also: [`-replace-at`](#-replace-at-n-x-list) -```cl +```el (-replace 1 "1" '(1 2 3 4 3 2 1)) ;; => '("1" 2 3 4 3 2 "1") (-replace "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo")) ;; => '("a" "nice" "bar" "sentence" "about" "bar") (-replace 1 2 nil) ;; => nil @@ -558,9 +558,9 @@ See also: `-replace-at` Return a list with `x` inserted into `list` at position `n`. -See also: `-splice`, `-splice-list` +See also: [`-splice`](#-splice-pred-fun-list), [`-splice-list`](#-splice-list-pred-new-list-list) -```cl +```el (-insert-at 1 'x '(a b c)) ;; => '(a x b c) (-insert-at 12 'x '(a b c)) ;; => '(a b c x) ``` @@ -569,9 +569,9 @@ See also: `-splice`, `-splice-list` Return a list with element at Nth position in `list` replaced with `x`. -See also: `-replace` +See also: [`-replace`](#-replace-old-new-list) -```cl +```el (-replace-at 0 9 '(0 1 2 3 4 5)) ;; => '(9 1 2 3 4 5) (-replace-at 1 9 '(0 1 2 3 4 5)) ;; => '(0 9 2 3 4 5) (-replace-at 4 9 '(0 1 2 3 4 5)) ;; => '(0 1 2 3 9 5) @@ -581,9 +581,9 @@ See also: `-replace` Return a list with element at Nth position in `list` replaced with `(func (nth n list))`. -See also: `-map-when` +See also: [`-map-when`](#-map-when-pred-rep-list) -```cl +```el (-update-at 0 (lambda (x) (+ x 9)) '(0 1 2 3 4 5)) ;; => '(9 1 2 3 4 5) (-update-at 1 (lambda (x) (+ x 8)) '(0 1 2 3 4 5)) ;; => '(0 9 2 3 4 5) (--update-at 2 (length it) '("foo" "bar" "baz" "quux")) ;; => '("foo" "bar" 3 "quux") @@ -593,9 +593,9 @@ See also: `-map-when` Return a list with element at Nth position in `list` removed. -See also: `-remove-at-indices`, `-remove` +See also: [`-remove-at-indices`](#-remove-at-indices-indices-list), [`-remove`](#-remove-pred-list) -```cl +```el (-remove-at 0 '("0" "1" "2" "3" "4" "5")) ;; => '("1" "2" "3" "4" "5") (-remove-at 1 '("0" "1" "2" "3" "4" "5")) ;; => '("0" "2" "3" "4" "5") (-remove-at 2 '("0" "1" "2" "3" "4" "5")) ;; => '("0" "1" "3" "4" "5") @@ -607,9 +607,9 @@ Return a list whose elements are elements from `list` without elements selected as `(nth i list)` for all i from `indices`. -See also: `-remove-at`, `-remove` +See also: [`-remove-at`](#-remove-at-n-list), [`-remove`](#-remove-pred-list) -```cl +```el (-remove-at-indices '(0) '("0" "1" "2" "3" "4" "5")) ;; => '("1" "2" "3" "4" "5") (-remove-at-indices '(0 2 4) '("0" "1" "2" "3" "4" "5")) ;; => '("1" "3" "5") (-remove-at-indices '(0 5) '("0" "1" "2" "3" "4" "5")) ;; => '("1" "2" "3" "4") @@ -631,7 +631,7 @@ item, etc. If `list` contains no items, return `initial-value` and In the anaphoric form `--reduce-from`, the accumulated value is exposed as `acc`. -```cl +```el (-reduce-from '- 10 '(1 2 3)) ;; => 4 (-reduce-from (lambda (memo item) (concat "(" memo " - " (int-to-string item) ")")) "10" '(1 2 3)) ;; => "(((10 - 1) - 2) - 3)" (--reduce-from (concat acc " " it) "START" '("a" "b" "c")) ;; => "START a b c" @@ -643,10 +643,10 @@ Replace conses with `fn`, nil with `initial-value` and evaluate the resulting expression. If `list` is empty, `initial-value` is returned and `fn` is not called. -Note: this function works the same as `-reduce-from` but the +Note: this function works the same as [`-reduce-from`](#-reduce-from-fn-initial-value-list) but the operation associates from right instead of from left. -```cl +```el (-reduce-r-from '- 10 '(1 2 3)) ;; => -8 (-reduce-r-from (lambda (item memo) (concat "(" (int-to-string item) " - " memo ")")) "10" '(1 2 3)) ;; => "(1 - (2 - (3 - 10)))" (--reduce-r-from (concat it " " acc) "END" '("a" "b" "c")) ;; => "a b c END" @@ -663,7 +663,7 @@ reduce return the result of calling `fn` with no arguments. If In the anaphoric form `--reduce`, the accumulated value is exposed as `acc`. -```cl +```el (-reduce '- '(1 2 3 4)) ;; => -8 (-reduce (lambda (memo item) (format "%s-%s" memo item)) '(1 2 3)) ;; => "1-2-3" (--reduce (format "%s-%s" acc it) '(1 2 3)) ;; => "1-2-3" @@ -680,10 +680,10 @@ returned and `fn` is not called. The first argument of `fn` is the new item, the second is the accumulated value. -Note: this function works the same as `-reduce` but the operation +Note: this function works the same as [`-reduce`](#-reduce-fn-list) but the operation associates from right instead of from left. -```cl +```el (-reduce-r '- '(1 2 3 4)) ;; => -2 (-reduce-r (lambda (item memo) (format "%s-%s" memo item)) '(1 2 3)) ;; => "3-2-1" (--reduce-r (format "%s-%s" acc it) '(1 2 3)) ;; => "3-2-1" @@ -693,7 +693,7 @@ associates from right instead of from left. Counts the number of items in `list` where (`pred` item) is non-nil. -```cl +```el (-count 'even? '(1 2 3 4 5)) ;; => 2 (--count (< it 4) '(1 2 3 4)) ;; => 3 ``` @@ -702,7 +702,7 @@ Counts the number of items in `list` where (`pred` item) is non-nil. Return the sum of `list`. -```cl +```el (-sum '()) ;; => 0 (-sum '(1)) ;; => 1 (-sum '(1 2 3 4)) ;; => 10 @@ -712,7 +712,7 @@ Return the sum of `list`. Return the product of `list`. -```cl +```el (-product '()) ;; => 1 (-product '(1)) ;; => 1 (-product '(1 2 3 4)) ;; => 24 @@ -722,7 +722,7 @@ Return the product of `list`. Return the smallest value from `list` of numbers or markers. -```cl +```el (-min '(0)) ;; => 0 (-min '(3 2 1)) ;; => 1 (-min '(1 2 3)) ;; => 1 @@ -733,10 +733,10 @@ Return the smallest value from `list` of numbers or markers. Take a comparison function `comparator` and a `list` and return the least element of the list by the comparison function. -See also combinator `-on` which can transform the values before +See also combinator [`-on`](#-on-operator-transformer) which can transform the values before comparing them. -```cl +```el (-min-by '> '(4 3 6 1)) ;; => 1 (--min-by (> (car it) (car other)) '((1 2 3) (2) (3 2))) ;; => '(1 2 3) (--min-by (> (length it) (length other)) '((1 2 3) (2) (3 2))) ;; => '(2) @@ -746,7 +746,7 @@ comparing them. Return the largest value from `list` of numbers or markers. -```cl +```el (-max '(0)) ;; => 0 (-max '(3 2 1)) ;; => 3 (-max '(1 2 3)) ;; => 3 @@ -757,10 +757,10 @@ Return the largest value from `list` of numbers or markers. Take a comparison function `comparator` and a `list` and return the greatest element of the list by the comparison function. -See also combinator `-on` which can transform the values before +See also combinator [`-on`](#-on-operator-transformer) which can transform the values before comparing them. -```cl +```el (-max-by '> '(4 3 6 1)) ;; => 6 (--max-by (> (car it) (car other)) '((1 2 3) (2) (3 2))) ;; => '(3 2) (--max-by (> (length it) (length other)) '((1 2 3) (2) (3 2))) ;; => '(1 2 3) @@ -782,7 +782,7 @@ This means a list of form: `n` is the length of the returned list. -```cl +```el (-iterate '1+ 1 10) ;; => '(1 2 3 4 5 6 7 8 9 10) (-iterate (lambda (x) (+ x x)) 2 5) ;; => '(2 4 8 16 32) (--iterate (* it it) 2 5) ;; => '(2 4 16 256 65536) @@ -792,15 +792,15 @@ This means a list of form: Build a list from `seed` using `fun`. -This is "dual" operation to `-reduce-r`: while -reduce-r -consumes a list to produce a single value, `-unfold` takes a +This is "dual" operation to [`-reduce-r`](#-reduce-r-fn-list): while -reduce-r +consumes a list to produce a single value, [`-unfold`](#-unfold-fun-seed) takes a seed value and builds a (potentially infinite!) list. `fun` should return `nil` to stop the generating process, or a cons (`a` . `b`), where `a` will be prepended to the result and `b` is the new seed. -```cl +```el (-unfold (lambda (x) (unless (= x 0) (cons x (1- x)))) 10) ;; => '(10 9 8 7 6 5 4 3 2 1) (--unfold (when it (cons it (cdr it))) '(1 2 3 4)) ;; => '((1 2 3 4) (2 3 4) (3 4) (4)) (--unfold (when it (cons it (butlast it))) '(1 2 3 4)) ;; => '((1 2 3 4) (1 2 3) (1 2) (1)) @@ -815,7 +815,7 @@ Return t if (`pred` x) is non-nil for any x in `list`, else nil. Alias: `-any-p`, `-some?`, `-some-p` -```cl +```el (-any? 'even? '(1 2 3)) ;; => t (-any? 'even? '(1 3 5)) ;; => nil (--any? (= 0 (% it 2)) '(1 2 3)) ;; => t @@ -827,7 +827,7 @@ Return t if (`pred` x) is non-nil for all x in `list`, else nil. Alias: `-all-p`, `-every?`, `-every-p` -```cl +```el (-all? 'even? '(1 2 3)) ;; => nil (-all? 'even? '(2 4 6)) ;; => t (--all? (= 0 (% it 2)) '(2 4 6)) ;; => t @@ -839,7 +839,7 @@ Return t if (`pred` x) is nil for all x in `list`, else nil. Alias: `-none-p` -```cl +```el (-none? 'even? '(1 2 3)) ;; => nil (-none? 'even? '(1 3 5)) ;; => t (--none? (= 0 (% it 2)) '(1 2 3)) ;; => nil @@ -852,7 +852,7 @@ Return `nil` both if all items match the predicate or if none of the items match Alias: `-only-some-p` -```cl +```el (-only-some? 'even? '(1 2 3)) ;; => t (-only-some? 'even? '(1 3 5)) ;; => nil (-only-some? 'even? '(2 4 6)) ;; => nil @@ -867,7 +867,7 @@ if that's non-nil. Alias: `-contains-p` -```cl +```el (-contains? '(1 2 3) 1) ;; => t (-contains? '(1 2 3) 2) ;; => t (-contains? '(1 2 3) 4) ;; => nil @@ -881,7 +881,7 @@ The order of the elements in the lists does not matter. Alias: `-same-items-p` -```cl +```el (-same-items? '(1 2 3) '(1 2 3)) ;; => t (-same-items? '(1 2 3) '(3 2 1)) ;; => t (-same-items? '(1 2 3) '(1 2 3 4)) ;; => nil @@ -893,7 +893,7 @@ Return non-nil if `prefix` is prefix of `list`. Alias: `-is-prefix-p` -```cl +```el (-is-prefix? '(1 2 3) '(1 2 3 4 5)) ;; => t (-is-prefix? '(1 2 3 4 5) '(1 2 3)) ;; => nil (-is-prefix? '(1 3) '(1 2 3 4 5)) ;; => nil @@ -905,7 +905,7 @@ Return non-nil if `suffix` is suffix of `list`. Alias: `-is-suffix-p` -```cl +```el (-is-suffix? '(3 4 5) '(1 2 3 4 5)) ;; => t (-is-suffix? '(1 2 3 4 5) '(3 4 5)) ;; => nil (-is-suffix? '(3 5) '(1 2 3 4 5)) ;; => nil @@ -919,7 +919,7 @@ This operation runs in `o`(n^2) time Alias: `-is-infix-p` -```cl +```el (-is-infix? '(1 2 3) '(1 2 3 4 5)) ;; => t (-is-infix? '(2 3 4) '(1 2 3 4 5)) ;; => t (-is-infix? '(3 4 5) '(1 2 3 4 5)) ;; => t @@ -935,7 +935,7 @@ Functions partitioning the input list into a list of lists. Return a list of ((-take `n` `list`) (-drop `n` `list`)), in no more than one pass through the list. -```cl +```el (-split-at 3 '(1 2 3 4 5)) ;; => '((1 2 3) (4 5)) (-split-at 17 '(1 2 3 4 5)) ;; => '((1 2 3 4 5) nil) ``` @@ -944,7 +944,7 @@ Return a list of ((-take `n` `list`) (-drop `n` `list`)), in no more than one pa Return a list of ((-take-while `pred` `list`) (-drop-while `pred` `list`)), in no more than one pass through the list. -```cl +```el (-split-with 'even? '(1 2 3 4)) ;; => '(nil (1 2 3 4)) (-split-with 'even? '(2 4 5 6)) ;; => '((2 4) (5 6)) (--split-with (< it 4) '(1 2 3 4 3 2 1)) ;; => '((1 2 3) (4 3 2 1)) @@ -954,14 +954,14 @@ Return a list of ((-take-while `pred` `list`) (-drop-while `pred` `list`)), in n Split the `list` each time `item` is found. -Unlike `-partition-by`, the `item` is discarded from the results. +Unlike [`-partition-by`](#-partition-by-fn-list), the `item` is discarded from the results. Empty lists are also removed from the result. Comparison is done by `equal`. -See also `-split-when` +See also [`-split-when`](#-split-when-fn-list) -```cl +```el (-split-on '| '(Nil | Leaf a | Node [Tree a])) ;; => '((Nil) (Leaf a) (Node [Tree a])) (-split-on ':endgroup '("a" "b" :endgroup "c" :endgroup "d" "e")) ;; => '(("a" "b") ("c") ("d" "e")) (-split-on ':endgroup '("a" "b" :endgroup :endgroup "d" "e")) ;; => '(("a" "b") ("d" "e")) @@ -971,13 +971,13 @@ See also `-split-when` Split the `list` on each element where `fn` returns non-nil. -Unlike `-partition-by`, the "matched" element is discarded from +Unlike [`-partition-by`](#-partition-by-fn-list), the "matched" element is discarded from the results. Empty lists are also removed from the result. This function can be thought of as a generalization of `split-string`. -```cl +```el (-split-when 'even? '(1 2 3 4 5 6)) ;; => '((1) (3) (5)) (-split-when 'even? '(1 2 3 4 6 8 9)) ;; => '((1) (3) (9)) (--split-when (memq it '(&optional &rest)) '(a b &optional c d &rest args)) ;; => '((a b) (c d) (args)) @@ -987,7 +987,7 @@ This function can be thought of as a generalization of Return a list of ((-filter `pred` `list`) (-remove `pred` `list`)), in one pass through the list. -```cl +```el (-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7)) ;; => '((2 4 6) (1 3 5 7)) (--separate (< it 5) '(3 7 5 9 3 2 1 4 6)) ;; => '((3 3 2 1 4) (7 5 9 6)) (-separate 'cdr '((1 2) (1) (1 2 3) (4))) ;; => '(((1 2) (1 2 3)) ((1) (4))) @@ -999,7 +999,7 @@ Return a new list with the items in `list` grouped into `n-`sized sublists. If there are not enough items to make the last group `n-`sized, those items are discarded. -```cl +```el (-partition 2 '(1 2 3 4 5 6)) ;; => '((1 2) (3 4) (5 6)) (-partition 2 '(1 2 3 4 5 6 7)) ;; => '((1 2) (3 4) (5 6)) (-partition 3 '(1 2 3 4 5 6 7)) ;; => '((1 2 3) (4 5 6)) @@ -1010,7 +1010,7 @@ those items are discarded. Return a new list with the items in `list` grouped into `n-`sized sublists. The last group may contain less than `n` items. -```cl +```el (-partition-all 2 '(1 2 3 4 5 6)) ;; => '((1 2) (3 4) (5 6)) (-partition-all 2 '(1 2 3 4 5 6 7)) ;; => '((1 2) (3 4) (5 6) (7)) (-partition-all 3 '(1 2 3 4 5 6 7)) ;; => '((1 2 3) (4 5 6) (7)) @@ -1022,7 +1022,7 @@ Return a new list with the items in `list` grouped into `n-`sized sublists at of If there are not enough items to make the last group `n-`sized, those items are discarded. -```cl +```el (-partition-in-steps 2 1 '(1 2 3 4)) ;; => '((1 2) (2 3) (3 4)) (-partition-in-steps 3 2 '(1 2 3 4)) ;; => '((1 2 3)) (-partition-in-steps 3 2 '(1 2 3 4 5)) ;; => '((1 2 3) (3 4 5)) @@ -1033,7 +1033,7 @@ those items are discarded. Return a new list with the items in `list` grouped into `n-`sized sublists at offsets `step` apart. The last groups may contain less than `n` items. -```cl +```el (-partition-all-in-steps 2 1 '(1 2 3 4)) ;; => '((1 2) (2 3) (3 4) (4)) (-partition-all-in-steps 3 2 '(1 2 3 4)) ;; => '((1 2 3) (3 4)) (-partition-all-in-steps 3 2 '(1 2 3 4 5)) ;; => '((1 2 3) (3 4 5) (5)) @@ -1043,7 +1043,7 @@ The last groups may contain less than `n` items. Apply `fn` to each item in `list`, splitting it each time `fn` returns a new value. -```cl +```el (-partition-by 'even? '()) ;; => '() (-partition-by 'even? '(1 1 2 2 2 3 4 6 8)) ;; => '((1 1) (2 2 2) (3) (4 6 8)) (--partition-by (< it 3) '(1 2 3 4 3 2 1)) ;; => '((1 2) (3 4 3) (2 1)) @@ -1056,7 +1056,7 @@ value. Apply `fn` to each item in `list`, splitting it each time `fn` returns the header value, but only after seeing at least one other value (the body). -```cl +```el (--partition-by-header (= it 1) '(1 2 3 1 2 1 2 3 4)) ;; => '((1 2 3) (1 2) (1 2 3 4)) (--partition-by-header (> it 0) '(1 2 0 1 0 1 2 3 0)) ;; => '((1 2 0) (1 0) (1 2 3 0)) (-partition-by-header 'even? '(2 1 1 1 4 1 3 5 6 6 1)) ;; => '((2 1 1 1) (4 1 3 5) (6 6 1)) @@ -1067,7 +1067,7 @@ other value (the body). Separate `list` into an alist whose keys are `fn` applied to the elements of `list`. Keys are compared by `equal`. -```cl +```el (-group-by 'even? '()) ;; => '() (-group-by 'even? '(1 1 2 2 2 3 4 6 8)) ;; => '((nil 1 1 3) (t 2 2 2 4 6 8)) (--group-by (car (split-string it "/")) '("a/b" "c/d" "a/e")) ;; => '(("a" "a/b" "a/e") ("c" "c/d")) @@ -1085,7 +1085,7 @@ Return the index of the first element in the given `list` which is equal to the query element `elem`, or nil if there is no such element. -```cl +```el (-elem-index 2 '(6 7 8 2 3 4)) ;; => 3 (-elem-index "bar" '("foo" "bar" "baz")) ;; => 1 (-elem-index '(1 2) '((3) (5 6) (1 2) nil)) ;; => 2 @@ -1096,7 +1096,7 @@ such element. Return the indices of all elements in `list` equal to the query element `elem`, in ascending order. -```cl +```el (-elem-indices 2 '(6 7 8 2 3 4 2 1)) ;; => '(3 6) (-elem-indices "bar" '("foo" "bar" "baz")) ;; => '(1) (-elem-indices '(1 2) '((3) (1 2) (5 6) (1 2) nil)) ;; => '(1 3) @@ -1108,7 +1108,7 @@ Take a predicate `pred` and a `list` and return the index of the first element in the list satisfying the predicate, or nil if there is no such element. -```cl +```el (-find-index 'even? '(2 4 1 6 3 3 5 8)) ;; => 0 (--find-index (< 5 it) '(2 4 1 6 3 3 5 8)) ;; => 3 (-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz")) ;; => 1 @@ -1120,7 +1120,7 @@ Take a predicate `pred` and a `list` and return the index of the last element in the list satisfying the predicate, or nil if there is no such element. -```cl +```el (-find-last-index 'even? '(2 4 1 6 3 3 5 8)) ;; => 7 (--find-last-index (< 5 it) '(2 7 1 6 3 8 5 2)) ;; => 5 (-find-last-index (-partial 'string-lessp "baz") '("q" "foo" "baz")) ;; => 1 @@ -1131,7 +1131,7 @@ there is no such element. Return the indices of all elements in `list` satisfying the predicate `pred`, in ascending order. -```cl +```el (-find-indices 'even? '(2 4 1 6 3 3 5 8)) ;; => '(0 1 3 7) (--find-indices (< 5 it) '(2 4 1 6 3 3 5 8)) ;; => '(3 7) (-find-indices (-partial 'string-lessp "baz") '("bar" "foo" "baz")) ;; => '(1) @@ -1143,7 +1143,7 @@ Grade elements of `list` using `comparator` relation, yielding a permutation vector such that applying this permutation to `list` sorts it in ascending order. -```cl +```el (-grade-up '< '(3 1 4 2 1 3 3)) ;; => '(1 4 3 0 5 6 2) (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up '< l) l)) ;; => '(1 1 2 3 3 3 4) ``` @@ -1154,7 +1154,7 @@ Grade elements of `list` using `comparator` relation, yielding a permutation vector such that applying this permutation to `list` sorts it in descending order. -```cl +```el (-grade-down '< '(3 1 4 2 1 3 3)) ;; => '(2 0 5 6 3 1 4) (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down '< l) l)) ;; => '(4 3 3 3 2 1 1) ``` @@ -1171,7 +1171,7 @@ Return a new list containing the elements of `list1` and elements of `list2` tha The test for equality is done with `equal`, or with `-compare-fn` if that's non-nil. -```cl +```el (-union '(1 2 3) '(3 4 5)) ;; => '(1 2 3 4 5) (-union '(1 2 3 4) '()) ;; => '(1 2 3 4) (-union '(1 1 2 2) '(3 2 1)) ;; => '(1 1 2 2 3) @@ -1183,7 +1183,7 @@ Return a new list with only the members of `list` that are not in `list2`. The test for equality is done with `equal`, or with `-compare-fn` if that's non-nil. -```cl +```el (-difference '() '()) ;; => '() (-difference '(1 2 3) '(4 5 6)) ;; => '(1 2 3) (-difference '(1 2 3 4) '(3 4 5 6)) ;; => '(1 2) @@ -1195,7 +1195,7 @@ Return a new list containing only the elements that are members of both `list` a The test for equality is done with `equal`, or with `-compare-fn` if that's non-nil. -```cl +```el (-intersection '() '()) ;; => '() (-intersection '(1 2 3) '(4 5 6)) ;; => '() (-intersection '(1 2 3 4) '(3 4 5 6)) ;; => '(3 4) @@ -1209,7 +1209,7 @@ or with `-compare-fn` if that's non-nil. Alias: `-uniq` -```cl +```el (-distinct '()) ;; => '() (-distinct '(1 2 2 4)) ;; => '(1 2 4) ``` @@ -1225,7 +1225,7 @@ Other list functions not fit to be classified elsewhere. Rotate `list` `n` places to the right. With `n` negative, rotate to the left. The time complexity is `o`(n). -```cl +```el (-rotate 3 '(1 2 3 4 5 6 7)) ;; => '(5 6 7 1 2 3 4) (-rotate -3 '(1 2 3 4 5 6 7)) ;; => '(4 5 6 7 1 2 3) ``` @@ -1235,7 +1235,7 @@ The time complexity is `o`(n). Return a list with `x` repeated `n` times. Return nil if `n` is less than 1. -```cl +```el (-repeat 3 :a) ;; => '(:a :a :a) (-repeat 1 :a) ;; => '(:a) (-repeat 0 :a) ;; => nil @@ -1249,7 +1249,7 @@ The last 2 members of `args` are used as the final cons of the result so if the final member of `args` is not a list the result is a dotted list. -```cl +```el (-cons* 1 2) ;; => '(1 . 2) (-cons* 1 2 3) ;; => '(1 2 . 3) (-cons* 1) ;; => 1 @@ -1263,7 +1263,7 @@ This is like `cons`, but operates on the end of list. If `elements` is non nil, append these to the list as well. -```cl +```el (-snoc '(1 2 3) 4) ;; => '(1 2 3 4) (-snoc '(1 2 3) 4 5 6) ;; => '(1 2 3 4 5 6) (-snoc '(1 2 3) '(4 5 6)) ;; => '(1 2 3 (4 5 6)) @@ -1273,7 +1273,7 @@ If `elements` is non nil, append these to the list as well. Return a new list of all elements in `list` separated by `sep`. -```cl +```el (-interpose "-" '()) ;; => '() (-interpose "-" '("a")) ;; => '("a") (-interpose "-" '("a" "b" "c")) ;; => '("a" "-" "b" "-" "c") @@ -1283,7 +1283,7 @@ Return a new list of all elements in `list` separated by `sep`. Return a new list of the first item in each list, then the second etc. -```cl +```el (-interleave '(1 2) '("a" "b")) ;; => '(1 "a" 2 "b") (-interleave '(1 2) '("a" "b") '("A" "B")) ;; => '(1 "a" "A" 2 "b" "B") (-interleave '(1 2 3) '("a" "b")) ;; => '(1 "a" 2 "b") @@ -1299,7 +1299,7 @@ position. The anaphoric form `--zip-with` binds the elements from `list1` as `it`, and the elements from `list2` as `other`. -```cl +```el (-zip-with '+ '(1 2 3) '(4 5 6)) ;; => '(5 7 9) (-zip-with 'cons '(1 2 3) '(4 5 6)) ;; => '((1 . 4) (2 . 5) (3 . 6)) (--zip-with (concat it " and " other) '("Batman" "Jekyll") '("Robin" "Hyde")) ;; => '("Batman and Robin" "Jekyll and Hyde") @@ -1314,7 +1314,7 @@ groupings are equal to the length of the shortest input list. If two lists are provided as arguments, return the groupings as a list of cons cells. Otherwise, return the groupings as a list of lists. -```cl +```el (-zip '(1 2 3) '(4 5 6)) ;; => '((1 . 4) (2 . 5) (3 . 6)) (-zip '(1 2 3) '(4 5 6 7)) ;; => '((1 . 4) (2 . 5) (3 . 6)) (-zip '(1 2 3 4) '(4 5 6)) ;; => '((1 . 4) (2 . 5) (3 . 6)) @@ -1326,7 +1326,7 @@ Zip `lists`, with `fill-value` padded onto the shorter lists. The lengths of the returned groupings are equal to the length of the longest input list. -```cl +```el (-zip-fill 0 '(1 2 3 4 5) '(6 7 8 9)) ;; => '((1 . 6) (2 . 7) (3 . 8) (4 . 9) (5 . 0)) ``` @@ -1335,7 +1335,7 @@ longest input list. Return an infinite copy of `list` that will cycle through the elements and repeat from the beginning. -```cl +```el (-take 5 (-cycle '(1 2 3))) ;; => '(1 2 3 1 2) (-take 7 (-cycle '(1 "and" 3))) ;; => '(1 "and" 3 1 "and" 3 1) (-zip (-cycle '(1 2 3)) '(1 2)) ;; => '((1 . 1) (2 . 2)) @@ -1346,7 +1346,7 @@ elements and repeat from the beginning. Appends `fill-value` to the end of each list in `lists` such that they will all have the same length. -```cl +```el (-pad 0 '()) ;; => '(nil) (-pad 0 '(1)) ;; => '((1)) (-pad 0 '(1 2 3) '(4 5)) ;; => '((1 2 3) (4 5 0)) @@ -1363,9 +1363,9 @@ The outer product is computed by applying fn to all possible combinations created by taking one element from each list in order. The dimension of the result is (length lists). -See also: `-table-flat` +See also: [`-table-flat`](#-table-flat-fn-rest-lists) -```cl +```el (-table '* '(1 2 3) '(1 2 3)) ;; => '((1 2 3) (2 4 6) (3 6 9)) (-table (lambda (a b) (-sum (-zip-with '* a b))) '((1 2) (3 4)) '((1 3) (2 4))) ;; => '((7 15) (10 22)) (apply '-table 'list (-repeat 3 '(1 2))) ;; => '((((1 1 1) (2 1 1)) ((1 2 1) (2 2 1))) (((1 1 2) (2 1 2)) ((1 2 2) (2 2 2)))) @@ -1387,9 +1387,9 @@ of the result. This is equivalent to calling: but the implementation here is much more efficient. -See also: `-flatten-n`, `-table` +See also: [`-flatten-n`](#-flatten-n-num-list), [`-table`](#-table-fn-rest-lists) -```cl +```el (-table-flat 'list '(1 2 3) '(a b c)) ;; => '((1 a) (2 a) (3 a) (1 b) (2 b) (3 b) (1 c) (2 c) (3 c)) (-table-flat '* '(1 2 3) '(1 2 3)) ;; => '(1 2 3 2 4 6 3 6 9) (apply '-table-flat 'list (-repeat 3 '(1 2))) ;; => '((1 1 1) (2 1 1) (1 2 1) (2 2 1) (1 1 2) (2 1 2) (1 2 2) (2 2 2)) @@ -1403,7 +1403,7 @@ To get the first item in the list no questions asked, use `car`. Alias: `-find` -```cl +```el (-first 'even? '(1 2 3)) ;; => 2 (-first 'even? '(1 3 5)) ;; => nil (--first (> it 2) '(1 2 3)) ;; => 3 @@ -1413,7 +1413,7 @@ Alias: `-find` Return the last x in `list` where (`pred` x) is non-nil, else nil. -```cl +```el (-last 'even? '(1 2 3 4 5 6 3 3 3)) ;; => 6 (-last 'even? '(1 3 7 5 9)) ;; => nil (--last (> (length it) 3) '("a" "looong" "word" "and" "short" "one")) ;; => "short" @@ -1423,7 +1423,7 @@ Return the last x in `list` where (`pred` x) is non-nil, else nil. Return the first item of `list`, or nil on an empty list. -```cl +```el (-first-item '(1 2 3)) ;; => 1 (-first-item nil) ;; => nil ``` @@ -1432,7 +1432,7 @@ Return the first item of `list`, or nil on an empty list. Return the last item of `list`, or nil on an empty list. -```cl +```el (-last-item '(1 2 3)) ;; => 3 (-last-item nil) ;; => nil ``` @@ -1441,7 +1441,7 @@ Return the last item of `list`, or nil on an empty list. Return a list of all items in list except for the last. -```cl +```el (-butlast '(1 2 3)) ;; => '(1 2) (-butlast '(1 2)) ;; => '(1) (-butlast '(1)) ;; => nil @@ -1454,7 +1454,7 @@ Return the sorted list. `list` is `not` modified by side effects. `comparator` is called with two elements of `list`, and should return non-nil if the first element should sort before the second. -```cl +```el (-sort '< '(3 1 2)) ;; => '(1 2 3) (-sort '> '(3 1 2)) ;; => '(3 2 1) (--sort (< it other) '(3 1 2)) ;; => '(1 2 3) @@ -1467,7 +1467,7 @@ Return a list with `args`. If first item of `args` is already a list, simply return `args`. If not, return a list with `args` as elements. -```cl +```el (-list 1) ;; => '(1) (-list 1 2 3) ;; => '(1 2 3) ``` @@ -1478,7 +1478,7 @@ Compute the (least) fixpoint of `fn` with initial input `list`. `fn` is called at least once, results are compared with `equal`. -```cl +```el (-fix (lambda (l) (-non-nil (--mapcat (-split-at (/ (length it) 2) it) l))) '((1 2 3 4 5 6))) ;; => '((1) (2) (3) (4) (5) (6)) (let ((data '(("starwars" "scifi") ("jedi" "starwars" "warrior")))) (--fix (-uniq (--mapcat (cons it (cdr (assoc it data))) it)) '("jedi" "book"))) ;; => '("jedi" "starwars" "warrior" "scifi" "book") ``` @@ -1501,7 +1501,7 @@ of the passed branch node. Non-branch nodes are simply copied. -```cl +```el (-tree-seq 'listp 'identity '(1 (2 3) 4 (5 (6 7)))) ;; => '((1 (2 3) 4 (5 (6 7))) 1 (2 3) 2 3 4 (5 (6 7)) 5 (6 7) 6 7) (-tree-seq 'listp 'reverse '(1 (2 3) 4 (5 (6 7)))) ;; => '((1 (2 3) 4 (5 (6 7))) (5 (6 7)) (6 7) 7 6 5 4 (2 3) 3 2 1) (--tree-seq (vectorp it) (append it nil) [1 [2 3] 4 [5 [6 7]]]) ;; => '([1 [2 3] 4 [5 [6 7]]] 1 [2 3] 2 3 4 [5 [6 7]] 5 [6 7] 6 7) @@ -1511,7 +1511,7 @@ Non-branch nodes are simply copied. Apply `fn` to each element of `tree` while preserving the tree structure. -```cl +```el (-tree-map '1+ '(1 (2 3) (4 (5 6) 7))) ;; => '(2 (3 4) (5 (6 7) 8)) (-tree-map '(lambda (x) (cons x (expt 2 x))) '(1 (2 3) 4)) ;; => '((1 . 2) ((2 . 4) (3 . 8)) (4 . 16)) (--tree-map (length it) '("
" ("" "text" "
") "")) ;; => '(6 (3 4 4) 7) @@ -1525,7 +1525,7 @@ If `pred` returns nil, continue descending down this node. If `pred` returns non-nil, apply `fun` to this node and do not descend further. -```cl +```el (-tree-map-nodes 'vectorp (lambda (x) (-sum (append x nil))) '(1 [2 3] 4 (5 [6 7] 8))) ;; => '(1 5 4 (5 13 8)) (-tree-map-nodes 'keywordp (lambda (x) (symbol-name x)) '(1 :foo 4 ((5 6 :bar) :baz 8))) ;; => '(1 ":foo" 4 ((5 6 ":bar") ":baz" 8)) (--tree-map-nodes (eq (car-safe it) 'add-mode) (-concat it (list :mode 'emacs-lisp-mode)) '(with-mode emacs-lisp-mode (foo bar) (add-mode a b) (baz (add-mode c d)))) ;; => '(with-mode emacs-lisp-mode (foo bar) (add-mode a b :mode emacs-lisp-mode) (baz (add-mode c d :mode emacs-lisp-mode))) @@ -1539,9 +1539,9 @@ If elements of `tree` are lists themselves, apply the reduction recursively. `fn` is first applied to first element of the list and second element, then on this result and third element from the list etc. -See `-reduce-r` for how exactly are lists of zero or one element handled. +See [`-reduce-r`](#-reduce-r-fn-list) for how exactly are lists of zero or one element handled. -```cl +```el (-tree-reduce '+ '(1 (2 3) (4 5))) ;; => 15 (-tree-reduce 'concat '("strings" (" on" " various") ((" levels")))) ;; => "strings on various levels" (--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"))) ;; => "some words