Function: seq-reduce

seq-reduce is a byte-compiled function defined in seq.el.gz.

Signature

(seq-reduce FUNCTION SEQUENCE INITIAL-VALUE)

Documentation

Reduce the function FUNCTION across SEQUENCE, starting with INITIAL-VALUE.

Return the result of calling FUNCTION with INITIAL-VALUE and the first element of SEQUENCE, then calling FUNCTION with that result and the second element of SEQUENCE, then with that result and the third element of SEQUENCE, etc. FUNCTION will be called with INITIAL-VALUE (and then the accumulated value) as the first argument, and the elements from SEQUENCE as the second argument.

If SEQUENCE is empty, return INITIAL-VALUE without calling FUNCTION. This does not modify SEQUENCE.

Other relevant functions are documented in the sequence, vector and list groups.

View in manual

Shortdoc

;; list
(seq-reduce #'+ '(1 2 3) 0)
    => 6
;; vector
(seq-reduce #'+ [1 2 3] 0)
    => 6
;; sequence
(seq-reduce #'* [1 2 3] 2)
    => 12

Implementations

(seq-reduce FUNCTION SEQUENCE INITIAL-VALUE) in `seq.el'.

Undocumented

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/seq.el.gz
;;;###autoload
(cl-defgeneric seq-reduce (function sequence initial-value)
  "Reduce the function FUNCTION across SEQUENCE, starting with INITIAL-VALUE.

Return the result of calling FUNCTION with INITIAL-VALUE and the
first element of SEQUENCE, then calling FUNCTION with that result
and the second element of SEQUENCE, then with that result and the
third element of SEQUENCE, etc.  FUNCTION will be called with
INITIAL-VALUE (and then the accumulated value) as the first
argument, and the elements from SEQUENCE as the second argument.

If SEQUENCE is empty, return INITIAL-VALUE without calling FUNCTION.
This does not modify SEQUENCE."
  (if (seq-empty-p sequence)
      initial-value
    (let ((acc initial-value))
      (seq-doseq (elt sequence)
        (setq acc (funcall function acc elt)))
      acc)))