Function: seq-reduce

seq-reduce is an autoloaded and byte-compiled function defined in seq-25.el.

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 and FUNCTION is not called.

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

Shortdoc

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

Implementations

(function sequence initial-value) in `seq-25.el'.

Undocumented

Source Code

;; Defined in ~/.emacs.d/elpa/seq-2.24/seq-25.el
;;;###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 and FUNCTION is not called."
  (if (seq-empty-p sequence)
      initial-value
    (let ((acc initial-value))
      (seq-doseq (elt sequence)
        (setq acc (funcall function acc elt)))
      acc)))