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

Or an alias inject_operator perhaps...
This thread has discussed "sum" already and it's no good because you
can use any operator. I also like "accumulate" as mentioned, but
inject_operator is sooo clear to me.

···

On 5/22/06, Regg Mr <spamwhite@cox.net> wrote:

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.

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

Definitively not because you can do far more than calculating sums with inject.

Also

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

is easier to read ...for me.

Yes, but it also needs more LOC.

robert

···

2006/5/22, Regg Mr <spamwhite@cox.net>:

--
Have a look: Robert K. | Flickr

Hi --

···

On Mon, 22 May 2006, Regg Mr wrote:

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.

Only in the case where you're calculating a sum :slight_smile: Inject does the
sum-like thing but in a generalized way.

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

Regg Mr wrote:

Two points:

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

Unless the operator isn't + ...

    [1,2,3].inject { |left, right| left * right }
      # => 1*2*3

    "A::B".split("::").inject(Object) { |left, right|
left.const_get(right) }
      # => Object.const_get("A").const_get("B")

    (2..n).inject(1) { |left, right| left * right }
      # => n! => 2*3* ... * n

Also

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

is easier to read ...for me.

Perhaps, but there is a fundamental difference between the inject and
each versions. The "each" verison is procedural. It defines state (the
value of b) and how that state changes in each iteration (b += a).

The "inject" version is functional. There are no extra state variables
in our code that need initialization. And the expression itself is the
value, rather than having a side effect on a local variable.

Not that one way is better than the other, just noting differences.

-- Jim Weirich

···

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

..though the other uses of inject defy "inject_operator"...

data = [[1, 7, 3], [1, 2], [1, 5, 4, 1], [1, 8]]

longest_sample = nil
longest_length = data.inject(0) do |accum, sample|
  if accum > sample.length
    accum
  else
    longest_sample = sample
    sample.length
  end
end

puts "Longest sample: #{longest_sample.inspect}, length #{longest_length}"

..then again, using inject to find longest seems a bit silly...

longest_sample = data[0]
data.each do |sample|
  longest_sample = sample if sample.length > longest_sample.length
end
puts "Longest sample: #{longest_sample.inspect}, length #{longest_length}"

···

On 5/22/06, Leslie Viljoen <leslieviljoen@gmail.com> wrote:

On 5/22/06, Regg Mr <spamwhite@cox.net> wrote:
> 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.

Or an alias inject_operator perhaps...
This thread has discussed "sum" already and it's no good because you
can use any operator. I also like "accumulate" as mentioned, but
inject_operator is sooo clear to me.

..then again, using inject to find longest seems a bit silly...

longest_sample = data[0]
data.each do |sample|
        longest_sample = sample if sample.length > longest_sample.length
end
puts "Longest sample: #{longest_sample.inspect}, length #{longest_length}"

longest_sample = data.max{|a,b| a.length <=> b.length}

This is a question that I have often wondered about and I think this thread
has gone a fair way to explaining it.

Just to make sure, I want to step through a call to inject (The sum example)
to see that I have it. Any comments would be great.

[1,2,3].inject(0) { |cur_sum, elem| cur_sum + elem }

Iteration 1
cur_sum = 0, elem = 1
cur_sum = cur_sum + elem #=> cur_sum = 0 + 1 (Replace cur_sum with the
results of the block => 1 )

Iteration 2
cur_sum = 1, elem = 2
cur_sum = cur_sum + elem #=> cur_sum = 1 + 2 (Replace cur_sum with the
results of the block => 3 )

Iteration 3
cur_sum = 3, elem = 3
cur_sum = cur_sum + elem #=> cur_sum = 3 + 3 (Replace cur_sum with the
results of the block => 6 )

Return cur_sum #=> 6

I think that part so far is ok. I get this part. but how would I step
through this?

klass = fully_qualified_name.split('::').inject(Module) { |_module,
_symbol|
_module.const_get(_symbol)
}

instance = klass.new

···

On 5/22/06, Leslie Viljoen <leslieviljoen@gmail.com> wrote:

On 5/22/06, Leslie Viljoen <leslieviljoen@gmail.com> wrote:
> On 5/22/06, Regg Mr <spamwhite@cox.net> wrote:
> > 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.
>
> Or an alias inject_operator perhaps...
> This thread has discussed "sum" already and it's no good because you
> can use any operator. I also like "accumulate" as mentioned, but
> inject_operator is sooo clear to me.

..though the other uses of inject defy "inject_operator"...

data = [[1, 7, 3], [1, 2], [1, 5, 4, 1], [1, 8]]

longest_sample = nil
longest_length = data.inject(0) do |accum, sample|
        if accum > sample.length
                accum
        else
                longest_sample = sample
                sample.length
        end
end

puts "Longest sample: #{longest_sample.inspect}, length #{longest_length}"

..then again, using inject to find longest seems a bit silly...

longest_sample = data[0]
data.each do |sample|
        longest_sample = sample if sample.length > longest_sample.length
end
puts "Longest sample: #{longest_sample.inspect}, length #{longest_length}"

..though the other uses of inject defy "inject_operator"...

data = [[1, 7, 3], [1, 2], [1, 5, 4, 1], [1, 8]]

longest_sample = nil
longest_length = data.inject(0) do |accum, sample|
  if accum > sample.length
    accum
  else
    longest_sample = sample
    sample.length
  end
end

puts "Longest sample: #{longest_sample.inspect}, length
#{longest_length}"

..then again, using inject to find longest seems a bit silly...

Well, Enumerable#max is the way to go here, but inject isn't
that clumsy too:

longest = data.inject{|max, this| this.length > max.length ? this : max}
puts "Longest sample: #{longest.inspect}, length #{longest.length}"

I love it, it's the swiss army knife of Enumerable.

cheers

Simon

Yes precisely! That's what I like about Ruby, everyone's a smartypants!
(one day I will be too!)

···

On 5/22/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:

> ..then again, using inject to find longest seems a bit silly...
>
> longest_sample = data[0]
> data.each do |sample|
> longest_sample = sample if sample.length > longest_sample.length
> end
> puts "Longest sample: #{longest_sample.inspect}, length #{longest_length}"

longest_sample = data.max{|a,b| a.length <=> b.length}

This is a question that I have often wondered about and I think this thread
has gone a fair way to explaining it.

Just to make sure, I want to step through a call to inject (The sum example)
to see that I have it. Any comments would be great.

[1,2,3].inject(0) { |cur_sum, elem| cur_sum + elem }

Iteration 1
cur_sum = 0, elem = 1
cur_sum = cur_sum + elem #=> cur_sum = 0 + 1 (Replace cur_sum with the
results of the block => 1 )

Iteration 2
cur_sum = 1, elem = 2
cur_sum = cur_sum + elem #=> cur_sum = 1 + 2 (Replace cur_sum with the
results of the block => 3 )

Iteration 3
cur_sum = 3, elem = 3
cur_sum = cur_sum + elem #=> cur_sum = 3 + 3 (Replace cur_sum with the
results of the block => 6 )

Return cur_sum #=> 6

I think that part so far is ok. I get this part. but how would I step
through this?

Exactly the same - only values are different.

klass = fully_qualified_name.split('::').inject(Module) { |_module,
_symbol|
_module.const_get(_symbol)
}

instance = klass.new

class Foo;class Bar;class Baz; end end end

=> nil

"Foo::Bar::Baz".split(/::/).inject(Object) {|cl,n| print "-> ", cl,

", ", n, "\n"; cl.const_get(n)}.new
-> Object, Foo
-> Foo, Bar
-> Foo::Bar, Baz
=> #<Foo::Bar::Baz:0x3a3178>

Kind regards

robert

···

2006/5/22, Daniel N <has.sox@gmail.com>:

"Kroeger, Simon (ext)" <simon.kroeger.ext@siemens.com> writes:

Well, Enumerable#max is the way to go here, but inject isn't
that clumsy too:
longest = data.inject{|max, this| this.length > max.length ? this : max}

  longest = data.inject{|max, this| [this.length, max.length].max }

:wink: (That's how they do it in APL too, btw.)

···

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Christian Neukirchen schrieb:

  longest = data.inject{|max, this| [this.length, max.length].max }

:wink: (That's how they do it in APL too, btw.)

I know why I prefer Ruby :wink:

   data = %w"hello christian and APL"
   p data.inject{|max, this| [this.length, max.length].max }

   # => undefined method `length' for 9:Fixnum (NoMethodError)

Regards,
Pit

Hi,

Christian Neukirchen schrieb:
>
> longest = data.inject{|max, this| [this.length, max.length].max }
>
> :wink: (That's how they do it in APL too, btw.)

I know why I prefer Ruby :wink:

   data = %w"hello christian and APL"
   p data.inject{|max, this| [this.length, max.length].max }

   # => undefined method `length' for 9:Fixnum (NoMethodError)

This works:
   p data.inject(0){|max, this| [this.length, max].max }

And in APL it's 4 Unicode characters plus the symbol:
   (max)(reduce)(shape)(each)data,
   where (reduce) is an operator on (max) producing the function which
is equivalent to Ruby's inject method.

So there ... :slight_smile:

Stuart

···

On 5/24/06, Pit Capitain <pit@capitain.de> wrote:

Regards,
Pit

I must say I'm surprised that my tired old thread is still kicking
around.

If I may summarize, the reason I was confused by Inject is that it is
useful but poorly named.

many thanks for all the replies,
jp

···

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

Jeff Pritchard wrote:

I must say I'm surprised that my tired old thread is still kicking
around.

If I may summarize, the reason I was confused by Inject is that it is
useful but poorly named.

many thanks for all the replies,
jp

The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.

···

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

The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional.inject?

Don't you confuse "stupid" with "the ones I am not used to"?

yieiayeld? How the heck is the meaningless nonsense spellllled?

You mean yield? Since when normal English word is meaningless nonsense?

Regards,
Rimantas

···

--
http://rimantas.com/

it is not my style to feed trolls, but I just want to state:

you are polluting this ML
your posts are extremely annoying
I have no way to get rid of your posts without losing potential information

If your email address is authentic I consider informing CPAN about
your behavior.

Have a nice day.

Robert

···

On 3/1/07, Jenda Krynicky <jenda@cpan.org> wrote:

Jeff Pritchard wrote:
> I must say I'm surprised that my tired old thread is still kicking
> around.
>
> If I may summarize, the reason I was confused by Inject is that it is
> useful but poorly named.
>
> many thanks for all the replies,
> jp

The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.

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

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

I have to ask, from the apparent bitterness and outright hostility of
your frequent and recent posts -- did a Ruby-programmed DARPA GRand
Challenge[1] contestant run over your puppy?

Jacob Fugal

[1] http://www.darpa.mil/grandchallenge/index.asp

···

On 3/1/07, Jenda Krynicky <jenda@cpan.org> wrote:

Jeff Pritchard wrote:
> I must say I'm surprised that my tired old thread is still kicking
> around.
>
> If I may summarize, the reason I was confused by Inject is that it is
> useful but poorly named.
>
> many thanks for all the replies,
> jp

The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.