Subject: bug#78656: [PATCH] Add new reduction primitives `fold-left' and `fold-right'

From: Michael Heerdegen <[email-masked]>
Date: Tue, 03 Jun 2025 01:09:32 +0200

Eli Zaretskii <[email-masked]> writes:

> On balance, I'd prefer to have a new function in seq.el rather than
> another primitive in C.  Since we didn't need this function until now,
> it is reasonable to assume it is not going to be a hot spot in any
> program, so an implementation in C is not really required.

Since this would just add an implicit `seq-reverse' call, this doesn't
sound convincing either.  `cl-reduce' is able to reduce :from-end.  If
at all, then adding a new argument to `seq-reduce' that causes reducing
from the end sounds more sensible to me.

@Zach: I really can feel your desire to have a primitive but part of the
story is also that Emacs Lisp is not that purely functional language
that some of us wish, since it's a lot more iteration oriented than
other Lisps - recursion depth is horribly limited, for example.  Trying
to use a purely functional style in Elisp doesn't work well.

So, in Elisp we are having most of these things in seq.el instead of the
core, and I think it's appropriate.  Especially folding is an operation
that is not used that often in Elisp.

Michael.