Actually, #inject:into: is what is normally called fold or foldl,
and #reduce: is commonly called fold1.  It is very confusing to
call foldl1 #fold:.

To the best of my knowledge, the name 'reduce' comes from APL.
"Z <- LO/R
 has the effect of placing the function LO between adjacent pairs
 of items along the last axis of R and evaluating the resulting
 expression for each subarray.
 If R is the vector A B C the LO-reduction is defined as follows:
 LO/R <-> A LO B LO C"
which for APL means A LO (B LO C), so it's technically foldr1.
-- IBM APL2 Language Reference, 1994, page 209.

The name given to #inject:into: when it was first invented was
LIT, short for List ITeration.

Here's a reference: Functional Programming, Application and
Implementation, Peter Henderson, 1980, page 41:
<quote>
As another example of a higher-order function consider the following
operation of reduction (the idea and the name are in fact taken from the
programming language APL). We define reduce(x,g,a) where x is a list,
g is a binary function, and a is a constant, so that the list
x = (Xl. . . Xk) is reduced to the value g(X1, g(X2, ... g(Xk,a) ...))
<snip>
reduce(x,g,a) = if x = NIL then a else
                g(car(x),reduce(cdr(x),g,a))
</quote>

So APL used "reduce" for foldr1, and Henderson, because
most functions don't carry their identity with them,
used it for foldr.

As I recall it, the name "reduce" was dropped because
people wanted to support both foldl and foldr (and did
NOT want to apply it to higher-ranked arrays, so that
'rank reduced by 1' wasn't an aid to memory).  But it
IS a name for the operation that predates Smalltalk
and is still in use (as APL itself is).

If it comes to that, this higher-order function is
called 'reduce' in Swift:
https://developer.apple.com/documentation/swift/array/reduce(_:_:)
and of course the Map-Reduce paradigm is famous, and
guess what 'Reduce' stands for?

An intention-revealing name in Smalltalk would have been
  aCollection injectInto: aBinaryValuable [ifNone: emptyBlock]





On Thu, 13 Apr 2023 at 07:03, Steffen Märcker <merk...@web.de> wrote:

> Hi!
>
> I wonder whether there was a specific reason to name this method #reduce:?
> I would have expected #fold: as this is the more common term for what it
> does. And in fact, even the comment reads "Fold the result of the receiver
> into aBlock." Whereas #reduce: is the common term for what we call with
> #inject:into: .
>
> I am asking not to annoy anyone but out of curiosity. It figured this out
> only by some weird behaviour after porting some code that (re)defines
> #reduce .
>
> Ciao!
> Steffen
>

Reply via email to