Choice of the word "inject" vs what the method actually does

The word Inject essentially means to introduce something into a
system and yet the inject method iterates over a series elements
carrying forward some sort of result at each next step.

Hence would it not make more sense to name it " accumulate" which
seems to me more explicit and intuitive?

Yes I realize that an alias may be used for this, however, I am
looking for insight as to the reason “inject” was chosen over
something better suited like accumulate.

(Eg. I am not looking for “just use alias” or “it’s tradition” or
“that’s just how it is” as answers.)

Thank you very much for any insight and/or good discussion on this.

~Wayne

Hi,

In message “Re: Choice of the word “inject” vs what the method actually
does (method naming)”
on Wed, 4 Jul 2007 21:32:06 +0900, “Wayne E. Seguin”
[email protected] writes:

|Yes I realize that an alias may be used for this, however, I am
|looking for insight as to the reason “inject” was chosen over
|something better suited like accumulate.

It a method name used in Smalltalk. Probably the primary intention
was to pun with other -ect methods such as select, correct, or detect.
In short, it’s tradition.

|(Eg. I am not looking for “just use alias” or “it’s tradition” or
|“that’s just how it is” as answers.)

Oops, it might be the answer you were looking for. :wink:

          matz.

On 7/4/07, Wayne E. Seguin [email protected] wrote:

Hence would it not make more sense to name it " accumulate" which
seems to me more explicit and intuitive?

You’ve already been given an answer as to why it’s named as it is, but
if hypotethically it should be renamed, wouldn’t “foldl” be more
suitable? As far as I can gather, inject is what functional
programming languages call a left fold, equivalent to the Haskell
function

foldl :: (a → b → a) → a → [b] → a

On Jul 4, 9:26 am, Yukihiro M. [email protected] wrote:

was to pun with other -ect methods such as select, correct, or detect.
In short, it’s tradition.

|(Eg. I am not looking for “just use alias” or “it’s tradition” or
|“that’s just how it is” as answers.)

Oops, it might be the answer you were looking for. :wink:

                                                    matz.

Thank you very much! You did answer my question of where it comes
from, the reasons.

I was not looking for “that’s just the way it’s done” which is a bit
different than what I said :wink:

Wayne E. Seguin <wayneeseguin gmail.com> writes:

The word Inject essentially means to introduce something into a
system and yet the inject method iterates over a series elements
carrying forward some sort of result at each next step.

Hence would it not make more sense to name it " accumulate" which
seems to me more explicit and intuitive?

The best I could ever come up with is that the function is injected
between
the elements of the object

On Jul 09, 2007, at 14:53 , Gary W. wrote:

Gary W.

Now that makes perfect sense. Thank you Gary.

On Jul 4, 2007, at 10:26 AM, Gareth A. wrote:

injected between
the elements of the object

Yes. For me, this rationale becomes clear when you see that

[1,2,3,4].inject { |a,i| a + i }

is the same as

(1 + 2 + 3 + 4)

The binary operator ‘plus’ has been injected between all the elements
of the collection.

Gary W.

On Jul 11, 2007, at 04:33 , C Erler wrote:

The general idea is that you have an unfinished result. You inject
each item one at a time into the result.

Thank you. This also makes a lot of sense.

On Jul 4, 7:32 am, “Wayne E. Seguin” [email protected] wrote:

Yes I realize that an alias may be used for this, however, I am
looking for insight as to the reason “inject” was chosen over
something better suited like accumulate.

The general idea is that you have an unfinished result. You inject
each item one at a time into the result.

[1, 2, 3].inject(0) { |sum, item| sum + item } injects each item into
the sum one at a time.
[String, Array, Hash].inject({}) { |class_lookup, klass|
class_lookup[klass.name] = klass } injects an entry into the lookup
table one at a time.
[egg, sugar, salt].inject { |mixture, ingredient| mixture.mix_in
ingredient } injects an ingredient into the mixture one at a time.