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 return that element and FUNCTION is not called.
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.
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 (cl-func cl-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 return that element and FUNCTION is not called.
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]...)"
(cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
(or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
(setq cl-seq (cl-subseq cl-seq cl-start cl-end))
(if cl-from-end (setq cl-seq (nreverse cl-seq)))
(let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
(cl-seq (cl--check-key (pop cl-seq)))
(t (funcall cl-func)))))
(if cl-from-end
(while cl-seq
(setq cl-accum (funcall cl-func (cl--check-key (pop cl-seq))
cl-accum)))
(while cl-seq
(setq cl-accum (funcall cl-func cl-accum
(cl--check-key (pop cl-seq))))))
cl-accum)))