Skip to content

Reductions

Functions reducing lists to a single value (which may also be a list).

Function: -reduce-from (fn init list)

Reduce the function fn across list, starting with init. Return the result of applying fn to init and the first element of list, then applying fn to that result and the second element, etc. If list is empty, return init without calling fn.

This function’s anaphoric counterpart is --reduce-from.

For other folds, see also -reduce (see -reduce) and -reduce-r (see -reduce-r).

emacs-lisp
(-reduce-from #'- 10 '(1 2 3))
4
emacs-lisp
(-reduce-from #'list 10 '(1 2 3))
    ⇒ (((10 1) 2) 3)
emacs-lisp
(--reduce-from (concat acc " " it) "START" '("a" "b" "c"))
"START a b c"

Function: -reduce-r-from (fn init list)

Reduce the function fn across list in reverse, starting with init. Return the result of applying fn to the last element of list and init, then applying fn to the second-to-last element and the previous result of fn, etc. That is, the first argument of fn is the current element, and its second argument the accumulated value. If list is empty, return init without calling fn.

This function is like -reduce-from (see -reduce-from) but the operation associates from the right rather than left. In other words, it starts from the end of list and flips the arguments to fn. Conceptually, it is like replacing the conses in list with applications of fn, and its last link with init, and evaluating the resulting expression.

This function’s anaphoric counterpart is --reduce-r-from.

For other folds, see also -reduce-r (see -reduce-r) and -reduce (see -reduce).

emacs-lisp
(-reduce-r-from #'- 10 '(1 2 3))
-8
emacs-lisp
(-reduce-r-from #'list 10 '(1 2 3))
    ⇒ (1 (2 (3 10)))
emacs-lisp
(--reduce-r-from (concat it " " acc) "END" '("a" "b" "c"))
"a b c END"

Function: -reduce (fn list)

Reduce the function fn across list. Return the result of applying fn to the first two elements of list, then applying fn to that result and the third element, etc. If list contains a single element, return it without calling fn. If list is empty, return the result of calling fn with no arguments.

This function’s anaphoric counterpart is --reduce.

For other folds, see also -reduce-from (see -reduce-from) and -reduce-r (see -reduce-r).

emacs-lisp
(-reduce #'- '(1 2 3 4))
-8
emacs-lisp
(-reduce #'list '(1 2 3 4))
    ⇒ (((1 2) 3) 4)
emacs-lisp
(--reduce (format "%s-%d" acc it) '(1 2 3))
"1-2-3"

Function: -reduce-r (fn list)

Reduce the function fn across list in reverse. Return the result of applying fn to the last two elements of list, then applying fn to the third-to-last element and the previous result of fn, etc. That is, the first argument of fn is the current element, and its second argument the accumulated value. If list contains a single element, return it without calling fn. If list is empty, return the result of calling fn with no arguments.

This function is like -reduce (see -reduce) but the operation associates from the right rather than left. In other words, it starts from the end of list and flips the arguments to fn. Conceptually, it is like replacing the conses in list with applications of fn, ignoring its last link, and evaluating the resulting expression.

This function’s anaphoric counterpart is --reduce-r.

For other folds, see also -reduce-r-from (see -reduce-r-from) and -reduce (see -reduce).

emacs-lisp
(-reduce-r #'- '(1 2 3 4))
-2
emacs-lisp
(-reduce-r #'list '(1 2 3 4))
    ⇒ (1 (2 (3 4)))
emacs-lisp
(--reduce-r (format "%s-%d" acc it) '(1 2 3))
"3-2-1"

Function: -reductions-from (fn init list)

Return a list of fn’s intermediate reductions across list. That is, a list of the intermediate values of the accumulator when -reduce-from (see -reduce-from) (which see) is called with the same arguments.

This function’s anaphoric counterpart is --reductions-from.

For other folds, see also -reductions (see -reductions) and -reductions-r (see -reductions-r).

emacs-lisp
(-reductions-from #'max 0 '(2 1 4 3))
    ⇒ (0 2 2 4 4)
emacs-lisp
(-reductions-from #'* 1 '(1 2 3 4))
    ⇒ (1 1 2 6 24)
emacs-lisp
(--reductions-from (format "(FN %s %d)" acc it) "INIT" '(1 2 3))
    ⇒ ("INIT" "(FN INIT 1)" "(FN (FN INIT 1) 2)" "(FN (FN (FN INIT 1) 2) 3)")

Function: -reductions-r-from (fn init list)

Return a list of fn’s intermediate reductions across reversed list. That is, a list of the intermediate values of the accumulator when -reduce-r-from (see -reduce-r-from) (which see) is called with the same arguments.

This function’s anaphoric counterpart is --reductions-r-from.

For other folds, see also -reductions (see -reductions) and -reductions-r (see -reductions-r).

emacs-lisp
(-reductions-r-from #'max 0 '(2 1 4 3))
    ⇒ (4 4 4 3 0)
emacs-lisp
(-reductions-r-from #'* 1 '(1 2 3 4))
    ⇒ (24 24 12 4 1)
emacs-lisp
(--reductions-r-from (format "(FN %d %s)" it acc) "INIT" '(1 2 3))
    ⇒ ("(FN 1 (FN 2 (FN 3 INIT)))" "(FN 2 (FN 3 INIT))" "(FN 3 INIT)" "INIT")

Function: -reductions (fn list)

Return a list of fn’s intermediate reductions across list. That is, a list of the intermediate values of the accumulator when -reduce (see -reduce) (which see) is called with the same arguments.

This function’s anaphoric counterpart is --reductions.

For other folds, see also -reductions (see -reductions) and -reductions-r (see -reductions-r).

emacs-lisp
(-reductions #'+ '(1 2 3 4))
    ⇒ (1 3 6 10)
emacs-lisp
(-reductions #'* '(1 2 3 4))
    ⇒ (1 2 6 24)
emacs-lisp
(--reductions (format "(FN %s %d)" acc it) '(1 2 3))
    ⇒ (1 "(FN 1 2)" "(FN (FN 1 2) 3)")

Function: -reductions-r (fn list)

Return a list of fn’s intermediate reductions across reversed list. That is, a list of the intermediate values of the accumulator when -reduce-r (see -reduce-r) (which see) is called with the same arguments.

This function’s anaphoric counterpart is --reductions-r.

For other folds, see also -reductions-r-from (see -reductions-r-from) and -reductions (see -reductions).

emacs-lisp
(-reductions-r #'+ '(1 2 3 4))
    ⇒ (10 9 7 4)
emacs-lisp
(-reductions-r #'* '(1 2 3 4))
    ⇒ (24 24 12 4)
emacs-lisp
(--reductions-r (format "(FN %d %s)" it acc) '(1 2 3))
    ⇒ ("(FN 1 (FN 2 3))" "(FN 2 3)" 3)

Function: -count (pred list)

Counts the number of items in list where (pred item) is non-nil.

emacs-lisp
(-count 'even? '(1 2 3 4 5))
2
emacs-lisp
(--count (< it 4) '(1 2 3 4))
3

Function: -sum (list)

Return the sum of list.

emacs-lisp
(-sum ())
0
emacs-lisp
(-sum '(1))
1
emacs-lisp
(-sum '(1 2 3 4))
10

Function: -running-sum (list)

Return a list with running sums of items in list. list must be non-empty.

emacs-lisp
(-running-sum '(1 2 3 4))
    ⇒ (1 3 6 10)
emacs-lisp
(-running-sum '(1))
    ⇒ (1)
emacs-lisp
(-running-sum ())
    error→ Wrong type argument: consp, nil

Function: -product (list)

Return the product of list.

emacs-lisp
(-product ())
1
emacs-lisp
(-product '(1))
1
emacs-lisp
(-product '(1 2 3 4))
24

Function: -running-product (list)

Return a list with running products of items in list. list must be non-empty.

emacs-lisp
(-running-product '(1 2 3 4))
    ⇒ (1 2 6 24)
emacs-lisp
(-running-product '(1))
    ⇒ (1)
emacs-lisp
(-running-product ())
    error→ Wrong type argument: consp, nil

Function: -inits (list)

Return all prefixes of list.

emacs-lisp
(-inits '(1 2 3 4))
    ⇒ (nil (1) (1 2) (1 2 3) (1 2 3 4))
emacs-lisp
(-inits nil)
    ⇒ (nil)
emacs-lisp
(-inits '(1))
    ⇒ (nil (1))

Function: -tails (list)

Return all suffixes of list.

emacs-lisp
(-tails '(1 2 3 4))
    ⇒ ((1 2 3 4) (2 3 4) (3 4) (4) nil)
emacs-lisp
(-tails nil)
    ⇒ (nil)
emacs-lisp
(-tails '(1))
    ⇒ ((1) nil)

Function: -common-prefix (&rest lists)

Return the longest common prefix of lists.

emacs-lisp
(-common-prefix '(1))
    ⇒ (1)
emacs-lisp
(-common-prefix '(1 2) '(3 4) '(1 2))
    ⇒ ()
emacs-lisp
(-common-prefix '(1 2) '(1 2 3) '(1 2 3 4))
    ⇒ (1 2)

Function: -common-suffix (&rest lists)

Return the longest common suffix of lists.

emacs-lisp
(-common-suffix '(1))
    ⇒ (1)
emacs-lisp
(-common-suffix '(1 2) '(3 4) '(1 2))
    ⇒ ()
emacs-lisp
(-common-suffix '(1 2 3 4) '(2 3 4) '(3 4))
    ⇒ (3 4)

Function: -min (list)

Return the smallest value from list of numbers or markers.

emacs-lisp
(-min '(0))
0
emacs-lisp
(-min '(3 2 1))
1
emacs-lisp
(-min '(1 2 3))
1

Function: -min-by (comparator list)

Take a comparison function comparator and a list and return the least element of the list by the comparison function.

See also combinator -on (see -on) which can transform the values before comparing them.

emacs-lisp
(-min-by '> '(4 3 6 1))
1
emacs-lisp
(--min-by (> (car it) (car other)) '((1 2 3) (2) (3 2)))
    ⇒ (1 2 3)
emacs-lisp
(--min-by (> (length it) (length other)) '((1 2 3) (2) (3 2)))
    ⇒ (2)

Function: -max (list)

Return the largest value from list of numbers or markers.

emacs-lisp
(-max '(0))
0
emacs-lisp
(-max '(3 2 1))
3
emacs-lisp
(-max '(1 2 3))
3

Function: -max-by (comparator list)

Take a comparison function comparator and a list and return the greatest element of the list by the comparison function.

See also combinator -on (see -on) which can transform the values before comparing them.

emacs-lisp
(-max-by '> '(4 3 6 1))
6
emacs-lisp
(--max-by (> (car it) (car other)) '((1 2 3) (2) (3 2)))
    ⇒ (3 2)
emacs-lisp
(--max-by (> (length it) (length other)) '((1 2 3) (2) (3 2)))
    ⇒ (1 2 3)

Function: -frequencies (list)

Count the occurrences of each distinct element of list.

Return an alist of (element . n), where each element occurs n times in list.

The test for equality is done with equal, or with -compare-fn if that is non-nil.

See also -count (see -count) and -group-by (see -group-by).

emacs-lisp
(-frequencies ())
    ⇒ ()
emacs-lisp
(-frequencies '(1 2 3 1 2 1))
    ⇒ ((1 . 3) (2 . 2) (3 . 1))
emacs-lisp
(let ((-compare-fn #'string=)) (-frequencies '(a "a")))
    ⇒ ((a . 2))