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.
> 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.
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).
On Sun, 21 May 2006, Jeff Pritchard 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?
> thanks, > 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 :-)
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" :-)
Jeff Pritchard 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.
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?
> 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.
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:
On May 21, 2006, at 1:05 AM, Jeff Pritchard 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.
On May 21, 2006, at 1:17 AM, Jeff Pritchard wrote:
> 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:
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.
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..
Jeff Pritchard 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.
-- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
> 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.
On Sun, 21 May 2006, Jeff Pritchard 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.
Jeff Pritchard 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.
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 Pritchard 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.
> 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
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.
> Jeff Pritchard 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.
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:
> On May 21, 2006, at 10:42 AM, Jeff Pritchard wrote:
>> 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
> 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.
Ruby's inject() lets you do amazing things very expressively. I first got into inject() when realized how easy it makes getting a class by its fully qualified name:
klass = fully_qualified_name.split('::').inject(Module) { |_module, _symbol| _module.const_get(_symbol)
}
instance = klass.new
In this example name "inject" totally corresponds to what it is doing here. To me at least.
> -----Original Message----- > From: Robert Klemme [mailto:shortcut...@googlemail.com] > Sent: Sunday, May 21, 2006 8:59 AM > To: ruby-talk ML > Subject: Re: rubynuby - confused by method name "inject"
> 2006/5/21, ReggW <m...@yourhome.com>: > > Jeff Pritchard 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.
> 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:
Jim Weirich wrote: > How I remember: Inject takes a binary operation (e.g. +) and injects it > between each element of a list.
> [1,2,3].inject { |a,b| a+b } => 1+2+3
> -- Jim Weirich
I think about this in a slightly different way, which helps me rememeber. Something like, "#inject itorates over thie items in order but also _injects_ the result from the last block call into the current one."
On 5/21/06, Jim Weirich <j...@weirichhouse.org> wrote:
> Jeff Pritchard wrote: > > As for Inject, nobody has come up with a wording that makes any sense to > > me yet. Does Inject have any aliases/synonyms?
> How I remember: Inject takes a binary operation (e.g. +) and injects it > between each element of a list.
Leslie Viljoen wrote: > On 5/21/06, Jim Weirich <j...@weirichhouse.org> wrote: >> Jeff Pritchard wrote: >> > As for Inject, nobody has come up with a wording that makes any sense to >> > me yet. Does Inject have any aliases/synonyms?
>> How I remember: Inject takes a binary operation (e.g. +) and injects it >> between each element of a list.