Rubynuby - confused by method name "inject"


#1

Can anybody explain to me how the Enumberable#inject method is
“injecting” something into something? I find it very difficult remember
method names when I don’t “get” them. So far in Ruby, “inject” takes
the cake for least understandable method name (with my own particular
convoluted gray matter).

Can somebody give some examples of its use and state in words how it is
“injecting” something into something?

thanks,
jp

P.S.
If anybody wants to take a stab at “collect”, that would be welcome
also.


#2

On May 20, 2006, at 10:42 PM, Jeff P. wrote:

thanks,
jp

P.S.
If anybody wants to take a stab at “collect”, that would be welcome
also.


Posted via http://www.ruby-forum.com/.

Would you prefer foldl? :-p

Well one possible explanation is that it’s injecting the contents of
the array and the result of the last call to the block back into the
block, sort of an auto-transfusion.

The canonical inject example is the sum of the elements of an array:
irb(main):001:0> [1, 7, 2].inject(0) { |sum_so_far, current_item|
sum_so_far + current_item }
=> 10

As far as collect goes, you’re collecting the results of the block
applied to each element in the enumerable. (I personally prefer map
to collect as far as terminology goes).

irb(main):002:0> [1, 7, 2].collect { |item| item * 2 }
=> [2, 14, 4]


#3

Hi –

On Sun, 21 May 2006, Jeff P. wrote:

jp

P.S.
If anybody wants to take a stab at “collect”, that would be welcome
also.

I have just about the mental energy for collect right now, so I’ll
give that a try :slight_smile:

The basic usage (just to have an example):

You have an array: [1,2,3,4,5]
You call: new_array = array.collect {|element| element * 10 }
The result (new_array) is: [10,20,30,40,50]

Now, in terms of the word “collect” itself:

Imagine that the elements of the first array are sitting on a bus.
The conductor comes through the bus and collects the fare from each
element. The fare is: yourself times 10. The cumulative result is a
new array, consisting of the old array’s elements but each multiplied
by 10.

So collect is an iterative process, where a new value is “collected”
from each old value, courtesy of being passed to the code block. The
new array is thus a one-to-one mapping of the old array, with the
values transformed. (So collect is also called “map” :slight_smile:

David


#4

Jeff P. wrote:

Can anybody explain to me how the Enumberable#inject method is
“injecting” something into something? I find it very difficult remember
method names when I don’t “get” them. So far in Ruby, “inject” takes
the cake for least understandable method name (with my own particular
convoluted gray matter).

Can somebody give some examples of its use and state in words how it is
“injecting” something into something?

[“foo”, “bar”].inject(5) {|total, word| total + word.length } #=> 11

This example injects the result of the block applied to each element of
the collection into a single value.

I actually prefer the Haskell term, “fold” – it repeatedly folds the
function of the value and an element of the collection back into the
value.

If anybody wants to take a stab at “collect”, that would be welcome
also.

I use “map”, which is an alias of “collect”.

ys = xs.map {|x| x * x }

It takes a block (a mapping from X to Y) and applies it to a collection
of Xs to map them to Ys.

Cheers,
Dave


#5

I think the name originally came from Smalltalk.


#6

BTW, I think “combine” makes the most sense to me for a synonym of
“inject”. The block describes how to combine all of the elements into a
single answer. vis:

total_cost = [[pie,2.00],[coffee,1.00]].combine { |cost, [food, price]|
cost += price }

jp


#7

I agree, “map” makes a lot more sense than “collect”. I’m going to just
forget about collect and consider it “personally deprecated due to poor
naming”.

As for Inject, nobody has come up with a wording that makes any sense to
me yet. Does Inject have any aliases/synonyms?

thanks,
jp

P.S.
If I were to choose a synonym for the “inject” method, I might come up
with “collect”. vis. “iterate over these items and collect the results
in one answer.” It seems to me that this method does the same thing
that the big Latin “E” symbol (Epsilon) does in Mathematics. Is there a
name for that symbol within the math community? (I mean - besides
Epsilon). Calculus and Physics are “twenty some odd years ago” for me.

P.P.S.
Is it considered bad form to create your own aliases for things like
this? For instance, if I were to create an alias for Enumerated#inject
like “gather”, and use that all over my programs, would this be
considered bad form, or just hunky-dory use of the language?


#8

On May 21, 2006, at 1:05 AM, Jeff P. wrote:

If I were to choose a synonym for the “inject” method, I might come up
with “collect”. vis. “iterate over these items and collect the
results
in one answer.” It seems to me that this method does the same thing
that the big Latin “E” symbol (Epsilon) does in Mathematics. Is
there a
name for that symbol within the math community? (I mean - besides
Epsilon). Calculus and Physics are “twenty some odd years ago” for
me.

I think you mean Sigma. And I believe when you don’t refer to it as
sigma you say “Summation”.

But I am not a mathematician, so take what I said with a grain of salt.


#9

In Common Lisp there is the reduce function. I think that name
appropriately qualifies the action involved.

(reduce (function +) (list 1 2 3 4 5 6))

(1…6).inject(0) {|c,v| c + v}

Also: yes, the “E” in math is a Sigma or summation. I wouldn’t say
they’re quite the same since summnation always involves summing whereas
inject/reduce are more abstract and multipurpose.

Ex:
(reduce (function *) (list 1 2 3 4 5 6))
(1…6).ineject(1) {|c,v| c * v}
These both act more like a factorial than a sum.


#10

On May 21, 2006, at 1:17 AM, Jeff P. wrote:

jp


Posted via http://www.ruby-forum.com/.

well here’s another suggestion for an alternate name:

each_with_state

We already have each_with_index. Of course this implies an alternate
ordering of the arguments.


#11

yea look into the difference between functional / imperative
programming languages / most languages [Java, C, C++, etc] are
imperative… inject, filter, map, et al are very common place in
functional languages and require a different mindset than their
imperative cousins. obviously ruby [as well as python] have
functional elements. it’s worth taking the time to understand
what they do / they’re very useful/powerful when dealing with
collections…


#12

[1,2,3,4,5].inject(0){|sum,num|sum+num} => 15
Which is ((((0+1)+2)+3)+4)+5

j`ey
http://www.eachmapinject.com


#13

Jeff P. wrote:

As for Inject, nobody has come up with a wording that makes any sense to
me yet. Does Inject have any aliases/synonyms?

You can think of it as “accumulate”, since there is an “accumulator”,
whose value is initially the argument to inject (or the first value in
the collection), and subsequently the block value.


#14

Hi –

On Sun, 21 May 2006, Jeff P. wrote:

P.P.S.
Is it considered bad form to create your own aliases for things like
this? For instance, if I were to create an alias for Enumerated#inject
like “gather”, and use that all over my programs, would this be
considered bad form, or just hunky-dory use of the language?

It would be bad form, in my opinion. It’s good to discuss what you
feel are the shortcomings of the language in a forum like this, but
don’t turn your programs into a position statement at the cost of
making them harder to understand.

David


#15

Jeff P. wrote:

Can anybody explain to me how the Enumberable#inject method is
“injecting” something into something? I find it very difficult remember
method names when I don’t “get” them. So far in Ruby, “inject” takes
the cake for least understandable method name (with my own particular
convoluted gray matter).

I agree!!!

I have it marked in my “Ruby notes”, as “Don’t use”.
I do this so that when I come across it again, I won’t spend 2 hours
trying to make sense out of it’s purpose.


#16

Jeff P. wrote:

If anybody wants to take a stab at “collect”, that would be welcome

I think #collect is fairly intuitive:

addresses = contacts.collect{|contact| contact.address}

Here we collect the addresses of the contacts.

Daniel


#17

On May 21, 2006, at 10:42 AM, Jeff P. wrote:

answer this. If I understand it correctly, Matz basically “invented”
ruby. Matz is, as far as I know, Japanese. Was Ruby first written
with
Japanese class and method names and later translated to English?
Or did
it start out in English?

thanks,
jp

No, Matz has said that he wanted to create a language that could be
used by everybody and he realized that it would have to use English.

Ruby honors its influences by reusing their names. Thus we have the
Smalltalk’s “inject”, Lisp’s “mixin”, C’s “sprintf” and “puts”,
Perl’s $ variables, etc.


#18

Following David’s suggestion, I think I’ll go ahead and use it, but
always precede it with a comment so I can remember what it does when I
come back to it later. Something like:

this “inject” method is “combining” the results from the block

operation into a single answer
[1,2,3,4,5].inject(0){|sum,num|sum+num}

This topic makes me wonder, and perhaps one of the old-timers here can
answer this. If I understand it correctly, Matz basically “invented”
ruby. Matz is, as far as I know, Japanese. Was Ruby first written with
Japanese class and method names and later translated to English? Or did
it start out in English?

thanks,
jp

ReggW wrote:

Jeff P. wrote:

Can anybody explain to me how the Enumberable#inject method is
“injecting” something into something? I find it very difficult remember
method names when I don’t “get” them. So far in Ruby, “inject” takes
the cake for least understandable method name (with my own particular
convoluted gray matter).

I agree!!!

I have it marked in my “Ruby notes”, as “Don’t use”.
I do this so that when I come across it again, I won’t spend 2 hours
trying to make sense out of it’s purpose.


#19

2006/5/21, ReggW removed_email_address@domain.invalid:

I have it marked in my “Ruby notes”, as “Don’t use”.
I do this so that when I come across it again, I won’t spend 2 hours
trying to make sense out of it’s purpose.

Granted that it may take some time to understand it and to recognize
its power (took me a while, too), but - once you grokked it you’ll be
amazed how much you can do with it. A lot - if not all - methods in
Enumerable can be elegantly implemented with inject. Try it out!
Also, when searching the archives you’ll find quite a few postings
that show how something can be done with inject and often it’s more
elegant than other methods. Just compare the example that has been
shown in this thread with the version using each:

sum = 0
enum.each {|x| sum += x}
sum

enum.inject(0) {|sum, x| sum + x}

Kind regards

robert


#20

Hi –

On Mon, 22 May 2006, Timothy H. wrote:

No, Matz has said that he wanted to create a language that could be used by
everybody and he realized that it would have to use English.

Ruby honors its influences by reusing their names. Thus we have the
Smalltalk’s “inject”, Lisp’s “mixin”, C’s “sprintf” and “puts”, Perl’s $
variables, etc.

A dubious honor, in the latter case; quoting the ToDo:

  • discourage use of symbol variables (e.g. $/, etc.) in manual
  • discourage use of Perlish features by giving warnings.

:slight_smile:

David