Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
rubynuby - confused by method name "inject"
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 45 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeff Pritchard  
View profile  
 More options May 20 2006, 10:42 pm
Newsgroups: comp.lang.ruby
From: Jeff Pritchard <j...@jeffpritchard.com>
Date: Sun, 21 May 2006 11:42:31 +0900
Local: Sat, May 20 2006 10:42 pm
Subject: rubynuby - confused by method name "inject"
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.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options May 20 2006, 10:52 pm
From: Logan Capaldo <logancapa...@gmail.com>
Date: Sun, 21 May 2006 11:52:10 +0900
Local: Sat, May 20 2006 10:52 pm
Subject: Re: rubynuby - confused by method name "inject"

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

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]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@wobblini.net  
View profile  
 More options May 20 2006, 10:56 pm
From: dbl...@wobblini.net
Date: Sun, 21 May 2006 11:56:46 +0900
Local: Sat, May 20 2006 10:56 pm
Subject: Re: rubynuby - confused by method name "inject"
Hi --

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" :-)

David

--
David A. Black (dbl...@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > http://www.manning.com/black


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Burt  
View profile  
 More options May 20 2006, 11:06 pm
From: Dave Burt <d...@burt.id.au>
Date: Sun, 21 May 2006 12:06:32 +0900
Local: Sat, May 20 2006 11:06 pm
Subject: Re: rubynuby - confused by method name "inject"

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.

Cheers,
Dave


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Pritchard  
View profile  
 More options May 21 2006, 1:05 am
Newsgroups: comp.lang.ruby
From: Jeff Pritchard <j...@jeffpritchard.com>
Date: Sun, 21 May 2006 14:05:29 +0900
Local: Sun, May 21 2006 1:05 am
Subject: Re: rubynuby - confused by method name "inject"
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?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francis Cianfrocca  
View profile  
 More options May 21 2006, 1:14 am
From: "Francis Cianfrocca" <garbageca...@gmail.com>
Date: Sun, 21 May 2006 14:14:38 +0900
Local: Sun, May 21 2006 1:14 am
Subject: Re: rubynuby - confused by method name "inject"

I think the name originally came from Smalltalk.

On 5/20/06, Jeff Pritchard <j...@jeffpritchard.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Pritchard  
View profile  
 More options May 21 2006, 1:17 am
Newsgroups: comp.lang.ruby
From: Jeff Pritchard <j...@jeffpritchard.com>
Date: Sun, 21 May 2006 14:17:27 +0900
Local: Sun, May 21 2006 1:17 am
Subject: Re: rubynuby - confused by method name "inject"
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

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options May 21 2006, 1:25 am
From: Logan Capaldo <logancapa...@gmail.com>
Date: Sun, 21 May 2006 14:25:10 +0900
Local: Sun, May 21 2006 1:25 am
Subject: Re: rubynuby - confused by method name "inject"

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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Logan Capaldo  
View profile  
 More options May 21 2006, 1:34 am
From: Logan Capaldo <logancapa...@gmail.com>
Date: Sun, 21 May 2006 14:34:09 +0900
Local: Sun, May 21 2006 1:34 am
Subject: Re: rubynuby - confused by method name "inject"

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:

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

> 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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joseph Abrahamson  
View profile  
 More options May 21 2006, 2:03 am
Newsgroups: comp.lang.ruby
From: Joseph Abrahamson <abrahamso...@gmail.com>
Date: Sun, 21 May 2006 15:03:22 +0900
Local: Sun, May 21 2006 2:03 am
Subject: Re: rubynuby - confused by method name "inject"
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.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
brez! !!  
View profile  
 More options May 21 2006, 2:28 am
Newsgroups: comp.lang.ruby
From: "brez! !!" <jbres...@gmail.com>
Date: Sun, 21 May 2006 15:28:23 +0900
Local: Sun, May 21 2006 2:28 am
Subject: Re: rubynuby - confused by method name "inject"
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..

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel VanderWerf  
View profile  
 More options May 21 2006, 3:28 am
From: Joel VanderWerf <vj...@path.berkeley.edu>
Date: Sun, 21 May 2006 16:28:04 +0900
Local: Sun, May 21 2006 3:28 am
Subject: Re: rubynuby - confused by method name "inject"

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joey  
View profile  
 More options May 21 2006, 3:32 am
From: Joey <rubyt...@eachmapinject.com>
Date: Sun, 21 May 2006 16:32:07 +0900
Local: Sun, May 21 2006 3:32 am
Subject: Re: rubynuby - confused by method name "inject"

[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

On 5/21/06, Jeff Pritchard <j...@jeffpritchard.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Schierbeck  
View profile  
 More options May 21 2006, 5:46 am
From: Daniel Schierbeck <daniel.schierb...@gmail.com>
Date: Sun, 21 May 2006 18:46:57 +0900
Local: Sun, May 21 2006 5:46 am
Subject: Re: rubynuby - confused by method name "inject"

Jeff Pritchard 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@wobblini.net  
View profile  
 More options May 21 2006, 7:16 am
From: dbl...@wobblini.net
Date: Sun, 21 May 2006 20:16:38 +0900
Local: Sun, May 21 2006 7:16 am
Subject: Re: rubynuby - confused by method name "inject"
Hi --

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.

David

--
David A. Black (dbl...@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > http://www.manning.com/black


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ReggW  
View profile  
 More options May 21 2006, 7:30 am
Newsgroups: comp.lang.ruby
From: ReggW <m...@yourhome.com>
Date: Sun, 21 May 2006 20:30:54 +0900
Local: Sun, May 21 2006 7:30 am
Subject: Re: rubynuby - confused by method name "inject"

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.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Pritchard  
View profile  
 More options May 21 2006, 10:42 am
Newsgroups: comp.lang.ruby
From: Jeff Pritchard <j...@jeffpritchard.com>
Date: Sun, 21 May 2006 23:42:09 +0900
Local: Sun, May 21 2006 10:42 am
Subject: Re: rubynuby - confused by method name "inject"
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.

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Timothy Hunter  
View profile  
 More options May 21 2006, 11:21 am
From: Timothy Hunter <cycli...@nc.rr.com>
Date: Mon, 22 May 2006 00:21:08 +0900
Local: Sun, May 21 2006 11:21 am
Subject: Re: rubynuby - confused by method name "inject"

On May 21, 2006, at 10:42 AM, Jeff Pritchard 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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options May 21 2006, 11:58 am
From: "Robert Klemme" <shortcut...@googlemail.com>
Date: Mon, 22 May 2006 00:58:49 +0900
Local: Sun, May 21 2006 11:58 am
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:

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

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

Kind regards

robert

--
Have a look: http://www.flickr.com/photos/fussel-foto/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@wobblini.net  
View profile  
 More options May 21 2006, 12:07 pm
From: dbl...@wobblini.net
Date: Mon, 22 May 2006 01:07:57 +0900
Local: Sun, May 21 2006 12:07 pm
Subject: Re: rubynuby - confused by method name "inject"
Hi --

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.

:-)

David

--
David A. Black (dbl...@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > http://www.manning.com/black


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gennady Bystritsky  
View profile  
 More options May 21 2006, 12:43 pm
From: "Gennady Bystritsky" <Gennady.Bystrit...@quest.com>
Date: Mon, 22 May 2006 01:43:01 +0900
Local: Sun, May 21 2006 12:43 pm
Subject: Re: rubynuby - confused by method name "inject"
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.

Sincerely,
Gennady.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Weirich  
View profile  
 More options May 21 2006, 12:55 pm
Newsgroups: comp.lang.ruby
From: Jim Weirich <j...@weirichhouse.org>
Date: Mon, 22 May 2006 01:55:32 +0900
Local: Sun, May 21 2006 12:55 pm
Subject: Re: rubynuby - confused by method name "inject"

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.

   [1,2,3].inject { |a,b| a+b }  => 1+2+3

-- Jim Weirich

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Nelson  
View profile  
 More options May 21 2006, 5:54 pm
Newsgroups: comp.lang.ruby
From: Mike Nelson <miken...@yahoo.com>
Date: Mon, 22 May 2006 06:54:15 +0900
Local: Sun, May 21 2006 5:54 pm
Subject: Re: rubynuby - confused by method name "inject"

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."

  (1..5).inject { |injected_value, this_item| injected_value + this_item

} => 15

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leslie Viljoen  
View profile  
 More options May 22 2006, 5:27 am
From: "Leslie Viljoen" <leslievilj...@gmail.com>
Date: Mon, 22 May 2006 18:27:03 +0900
Local: Mon, May 22 2006 5:27 am
Subject: Re: rubynuby - confused by method name "inject"
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.

>    [1,2,3].inject { |a,b| a+b }  => 1+2+3

Awesome! That really helps me!

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Regg Mr  
View profile  
 More options May 22 2006, 6:04 am
Newsgroups: comp.lang.ruby
From: Regg Mr <spamwh...@cox.net>
Date: Mon, 22 May 2006 19:04:17 +0900
Local: Mon, May 22 2006 6:04 am
Subject: Re: rubynuby - confused by method name "inject"

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.

>>    [1,2,3].inject { |a,b| a+b }  => 1+2+3

> Awesome! That really helps me!

Seems like "sum" would have been a better name.

Also

[1,2,3].each {|a| b += a}

is easier to read ...for me.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 45   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google