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

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

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

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 (dblack@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!
   > Ruby for Rails

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.

···

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

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.

···

-----Original Message-----
From: Robert Klemme [mailto:shortcutter@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 <me@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/

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

It sort of does, if you think of it as *injecting* the method or
block, between the values in the array. (And between the initial
value, if given, and the array.) So for instance:

arr = [1, 2, 3]
arr.inject(:slight_smile:

is like injecting "-" into the spaces between the values 1, 2, 3, so
you get 1, - 2, - 3. Speaking it with the usual pauses for the commas
also helps reinforce the order of operation (important with - or /
more so than + or *).

-Dave

···

On Fri, Jan 3, 2014 at 12:49 PM, Kal Starkis <lists@ruby-forum.com> wrote:

The thread is about the native #inject method in Ruby, and how that
name doesn't help one intuitively understand or remember what it does.

--
Dave Aronson, the T. Rex of Codosaurus LLC (codosaur.us),
freelance software developer, and creator of these sites:
PullRequestRoulette.com, blog.codosaur.us, & Dare2XL.com.

What I've learned after many conversations about #inject and its aliases is
that there is no one metaphor that works for this particular concept. For
everyone who thinks "fold" makes perfect sense, there's someone else who
thinks it's nonsense and that "accumulate", "aggregate", "reduce", or
"inject" make way more sense.

···

On Fri, Jan 3, 2014 at 1:54 PM, Dave Aronson <rubytalk2dave@davearonson.com>wrote:

On Fri, Jan 3, 2014 at 12:49 PM, Kal Starkis <lists@ruby-forum.com> wrote:

> The thread is about the native #inject method in Ruby, and how that
> name doesn't help one intuitively understand or remember what it does.

It sort of does, if you think of it as *injecting* the method or
block, between the values in the array. (And between the initial
value, if given, and the array.) So for instance:

arr = [1, 2, 3]
arr.inject(:slight_smile:

is like injecting "-" into the spaces between the values 1, 2, 3, so
you get 1, - 2, - 3. Speaking it with the usual pauses for the commas
also helps reinforce the order of operation (important with - or /
more so than + or *).

-Dave

--
Dave Aronson, the T. Rex of Codosaurus LLC (codosaur.us),
freelance software developer, and creator of these sites:
PullRequestRoulette.com, blog.codosaur.us, & Dare2XL.com.

Agreed. I didn't grok why other languages called it "fold" until
someone told me the map metaphor. (Admittedly, Ruby is the only one
I've used a *lot* with such a concept.) The "inject into the spaces"
metaphor is one I came up with when trying to explain to a Ruby-newbie
what inject did. (Presumably others came up with it too; that may
even be how the name came about. Anybody know?) Reduce made sense
because I had already heard of map-reduce, but even then, as someone
else pointed out, "reduce" doesn't necessarily mean "to one".

If I were coming up with a name for it, for a new language, I think
"accumulate" would be my choice, by reference to the concept of an
accumulator. Even that, though, would be unclear to someone without a
solid CS/coding background already.

Maybe we should just make up a new word and confuse everybody equally.
Where'd I put that random D&D character name generator.... :wink:

-Dave

···

On Fri, Jan 3, 2014 at 2:06 PM, Avdi Grimm <groups@inbox.avdi.org> wrote:

What I've learned after many conversations about #inject and its aliases
is that there is no one metaphor that works for this particular concept.

--
Dave Aronson, the T. Rex of Codosaurus LLC (codosaur.us),
freelance software developer, and creator of these sites:
PullRequestRoulette.com, blog.codosaur.us, & Dare2XL.com.

Dave Aronson wrote in post #1132148:

The "inject into the spaces"
metaphor is one I came up with when trying to explain to a Ruby-newbie
what inject did. (Presumably others came up with it too;

Look no further than this very thread! See Jim Weirich's first comment.

that may
even be how the name came about. Anybody know?)

It seems to take its name from Smalltalk's inject:into: method. I'm not
familiar with Smalltalk, but here's an example of that method taken from
Wikipedia:

#(1 3 5) inject: 10 into: [ :sum :element | sum + element ] "=> 19"

Based on how it reads 'inject 10 into [block]' (10 being the
first value for :sum), the idea seems to be of injecting the new result
back into the block upon each iteration, rather than injecting the block
between elements of the original object.

Reduce made sense
because I had already heard of map-reduce…

Yes, but wouldn't map-fold be so much cooler? :slight_smile:

···

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

···

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

Hi --

···

On Mon, 22 May 2006, Timothy Hunter wrote:

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.

:slight_smile:

David

--
David A. Black (dblack@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!
   > Ruby for Rails

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

Awesome! That really helps me!

···

On 5/21/06, Jim Weirich <jim@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

The big Sigma is often called the n-ary sum operator, and there's a
corresponding big Pi called the n-ary product (it can be implemented
as inject where the operation in the block is *).

So, in math-speak, I think one could say:

inject applies a block pairwise as an n-ary operation over an
Enumerable, with an optional initial value.

This may or may not help anyone understand it. :slight_smile:

-A

···

On 5/21/06, Logan Capaldo <logancapaldo@gmail.com> wrote:

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.

@Kal maybe this will help:

Here is the concept in done fizzbuzz style list :
The problem: Write a function foo that takes a number n and returns a
function that takes a number i, and returns n incremented by i.

http://www.paulgraham.com/accgen.html

which is an extension to this inglorious rant on managerial driven
development (MDD)
http://www.paulgraham.com/icad.html

~Stu

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

Leslie Viljoen wrote:

···

On 5/21/06, Jim Weirich <jim@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/\.

A LeDonne wrote:

So, in math-speak, I think one could say:

inject applies a block pairwise as an n-ary operation over an
Enumerable, with an optional initial value.

This may or may not help anyone understand it. :slight_smile:

Shockingly [to myself], I actually understand that, but I think it
doesn't cover all the possibile uses of inject, some of which are not
"operator in-betweenings".

But thanks for sharing. Mentally relating product and summation to
inject helps me tolerate what I otherwise think is feasibly the most
unintuitively-named standard method I've ever encountered.

Pistos

···

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