Function: cl-reduce

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

Signature

(cl-reduce FUNCTION SEQ [KEYWORD VALUE]...)

Documentation

Reduce two-argument FUNCTION across SEQ.

Keywords supported: :start :end :from-end :initial-value :key

Return the result of calling FUNCTION with the first and the second element of SEQ, then calling FUNCTION with that result and the third element of SEQ, then with that result and the fourth element of SEQ, etc.

If :INITIAL-VALUE is specified, it is logically added to the front of SEQ (or the back if :FROM-END is non-nil). If SEQ is empty, return :INITIAL-VALUE and FUNCTION is not called.

If SEQ is empty and no :INITIAL-VALUE is specified, then return the result of calling FUNCTION with zero arguments. This is the only case where FUNCTION is called with fewer than two arguments.

If SEQ contains exactly one element and no :INITIAL-VALUE is specified, then just return that element without calling FUNCTION.

If :FROM-END is non-nil, the reduction occurs from the back of the SEQ moving forward, and the order of arguments to the FUNCTION is also reversed.

View in manual

Aliases

reduce (obsolete since 27.1) org-reduce (obsolete since 9.0)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-seq.el.gz
;;;###autoload
(defun cl-reduce (func seq &rest cl-keys)
  "Reduce two-argument FUNCTION across SEQ.
\nKeywords supported:  :start :end :from-end :initial-value :key

Return the result of calling FUNCTION with the first and the
second element of SEQ, then calling FUNCTION with that result and
the third element of SEQ, then with that result and the fourth
element of SEQ, etc.

If :INITIAL-VALUE is specified, it is logically added to the
front of SEQ (or the back if :FROM-END is non-nil).  If SEQ is
empty, return :INITIAL-VALUE and FUNCTION is not called.

If SEQ is empty and no :INITIAL-VALUE is specified, then return
the result of calling FUNCTION with zero arguments.  This is the
only case where FUNCTION is called with fewer than two arguments.

If SEQ contains exactly one element and no :INITIAL-VALUE is
specified, then just return that element without calling FUNCTION.

If :FROM-END is non-nil, the reduction occurs from the back of
the SEQ moving forward, and the order of arguments to the
FUNCTION is also reversed.

\n(fn FUNCTION SEQ [KEYWORD VALUE]...)"
  (declare (important-return-value t))
  (cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
    (or (listp seq) (setq seq (append seq nil)))
    (setq seq (cl-subseq seq cl-start cl-end))
    (if cl-from-end (setq seq (nreverse seq)))
    (let ((accum (cond ((memq :initial-value cl-keys) cl-initial-value)
                       (seq (cl--check-key (pop seq)))
                       (t (funcall func)))))
      (if cl-from-end
          (while seq
            (setq accum (funcall func (cl--check-key (pop seq))
                                 accum)))
        (while seq
          (setq accum (funcall func accum
                               (cl--check-key (pop seq))))))
      accum)))