Forum: Ruby Choice of the word "inject" vs what the method actually does

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9b5ff40232c1dfd61238c2a90467f84?d=identicon&s=25 Wayne E. Seguin (Guest)
on 2007-07-04 14:37
(Received via mailing list)
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
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2007-07-04 15:28
(Received via mailing list)
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"
<wayneeseguin@gmail.com> 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. ;-)

              matz.
F2d901ea9430646c6dd35a629bb3f119?d=identicon&s=25 Simen Edvardsen (Guest)
on 2007-07-04 15:58
(Received via mailing list)
On 7/4/07, Wayne E. Seguin <wayneeseguin@gmail.com> 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
21053fc472ce35063e175e9cfbb78e6b?d=identicon&s=25 Gareth Adams (Guest)
on 2007-07-04 16:27
(Received via mailing list)
Wayne E. Seguin <wayneeseguin <at> 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
B9b5ff40232c1dfd61238c2a90467f84?d=identicon&s=25 Wayne E. Seguin (Guest)
on 2007-07-06 00:02
(Received via mailing list)
On Jul 4, 9:26 am, Yukihiro Matsumoto <m...@ruby-lang.org> 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. ;-)
>
>                                                         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 ;)
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2007-07-09 20:55
(Received via mailing list)
On Jul 4, 2007, at 10:26 AM, Gareth Adams 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 Wright
B9b5ff40232c1dfd61238c2a90467f84?d=identicon&s=25 Wayne E. Seguin (Guest)
on 2007-07-09 21:03
(Received via mailing list)
On Jul 09, 2007, at 14:53 , Gary Wright wrote:
>
>
> Gary Wright

Now that makes perfect sense. Thank you Gary.
E2a9d30a2487a924165aaaa016b54f87?d=identicon&s=25 C Erler (Guest)
on 2007-07-11 10:34
(Received via mailing list)
On Jul 4, 7:32 am, "Wayne E. Seguin" <wayneeseg...@gmail.com> 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.
B9b5ff40232c1dfd61238c2a90467f84?d=identicon&s=25 Wayne E. Seguin (Guest)
on 2007-07-12 02:47
(Received via mailing list)
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.
This topic is locked and can not be replied to.