Inject's pathological case

On Sun, 30 Mar 2008 15:27:20 +0900, 7stud – wrote:

By the way, using inject() is inefficient–not to mention confusing. You
might as well pretend it doesn’t exist.

What’s the “normal” way, not using inject?

-Thufir

On Mon, 31 Mar 2008 10:06:59 +0900, Julian L. wrote:

example program to illustrate inject num_ary = [10,37,27,398,273,28]

summing without inject:

without_inject_sum = 0
num_ary.each{|num| without_inject_sum += num}

summing with inject

with_inject_sum = num_ary.inject{|sum, num| sum + num}

Thank you for the example. I’m not convinced that inject is fantastic,
but certainly it’s something I need to understand and this example helps
to illuminates it.

-Thufir

On Sun, 30 Mar 2008 22:34:19 +0900, Rick DeNatale wrote:

Enumerable#inject is very useful, as long as you don’t treat it as
Maslow’s hammer and use it in inappropriate ways, such as giving it a
block like in the original posting to this thread.

Ah, the irony: when googling Maslow’s hammer:

Microsoft JET Database Engine error ‘80040e14’

Syntax error (missing operator) in query expression ''http://
www.google.ca/search?q=Maslow’s+hamme

ROFL,

Thufir

Hi –

On Mon, 31 Mar 2008, Yukihiro M. wrote:

|
|It picks up the synonym “reduce” in 1.9 – which I think is kind of
|too bad, since inject in Ruby has such a distinctive personality and
|cult following :slight_smile:

reduce, fold, foldl; these are some of the candidates. But I think I
have to wait for consensus for a while.

I would like to suggest the null alias :slight_smile: I think calling it #inject
is fine. It’s well-documented that the same function has other names
in other languages; I don’t think Ruby needs more than one, and it
just adds the need to explain and account for the synonym.

David

On Mar 30, 11:51 am, James G. [email protected] wrote:

On Mar 30, 2008, at 1:27 AM, 7stud – wrote:

By the way, using inject() is inefficient–not to mention confusing.
You might as well pretend it doesn’t exist.

I strongly disagree with that statement.

So 7stud, you learned Python and Ruby, decided you prefer Python, and
now hang out on the Ruby T. mailing list bad mouthing our language?
What’s the point, if you don’t mind my asking?

The same as for any troll?

Yeah, even map/collect is annoying enough.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
OUT 3rd APRIL
http://sensei.zenunit.com/

On Wed, Apr 2, 2008 at 8:41 AM, David A. Black [email protected]
wrote:

on Mon, 31 Mar 2008 00:27:58 +0900, “David A. Black”
|cult following :slight_smile:

+1 here David, but wait a minute, is that a bogey then? :wink: (Obviously
there is something like domain specific metrics).
Cheers
Robert


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Wed, Apr 2, 2008 at 2:50 AM, Julian L. [email protected]
wrote:

On 02/04/2008, at 5:41 PM, David A. Black wrote:

On Mon, 31 Mar 2008, Yukihiro M. wrote:

In message “Re: inject’s pathological case…”
on Mon, 31 Mar 2008 00:27:58 +0900, “David A. Black”
[email protected] writes:

Quoting me:

reduce, fold, foldl; these are some of the candidates. But I think I
have to wait for consensus for a while.

I would like to suggest the null alias :slight_smile: I think calling it #inject
is fine. It’s well-documented that the same function has other names
in other languages; I don’t think Ruby needs more than one, and it
just adds the need to explain and account for the synonym.

Actually the existing Enumerable#inject is kind of a combination of
Smalltalk’s inject:into: and the reduce/fold family of names.

Reduce normally means an operation which ‘sums’ the elements of a
collection. The earliest example I can think of is in APL where:

+/vector

was equivalent to
vector[0] + vector[1] + … + vector[n]

The + can be any binary operator returning a scalar, and corresponds
to a poor-mans version of the block argument to Ruby’s inject.

So +/vector is Sigma function, */vector is Pi etc.

The equivalent Ruby would be
vector.inject {|s,e| s + e}

But why inject? what are we injecting. The name only makes sense when
we use it the way Smalltalk does where the method was:

inject: initialValue into: block

So in Smalltalk to sum an Array you do

array.inject:0 into: [ s, e | s + e]

or to get the Pi of the array it’s
array.inject:1 into: [s, e | s * e]

So my archaeological theory is that Ruby took inject from Smalltalk’s
inject:into: and then made the first argument optional so that the
above two could be written as:

array.inject {|s,e| s + e}
as a simpler alternative to
array.inject(0) {|s,e| s + e}

and

array.inject {|s,e| s * e}
vs.
array.inject(1) {|s,e| s * e}

And we don’t have to provide the ‘identity’ element for the operation
we are using to reduce the array.

But when we take that initial argument away, which is what’s actually
being injected, the name makes a little less sense compared to, say
reduce.

Yeah, even map/collect is annoying enough.

When I first started using Ruby, my old Smalltalk background had me
using collect, and detect vs. map and find, now however the latter
aliases seem more natural. In Smalltalk for example collect is
inspired by the many collection classes and now feels less comfortable
since Ruby and the Ruby ‘literature’ doesn’t really talk about
collections.

I’m gradually losing my Smalltalk ‘accent’ in Ruby. I think that
that’s better than the analogues of speaking Basil Fawlty’s version of
Spanish, or Manuel’s version of English.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/