[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: inject's pathological case...

Yukihiro Matsumoto

3/31/2008 6:29:00 AM

Hi,

In message "Re: inject's pathological case..."
on Mon, 31 Mar 2008 00:27:58 +0900, "David A. Black" <dblack@rubypal.com> writes:

|> AFAIK, Matz correct me if I'm wrong, Ruby got the name inject from
|> Smalltalk, along with collect, select, detect and several other
|> methods. The other three I mention have alias, map for collect,
|> find_all for select, and find for detect, but inject is still just
|> inject in Ruby.
|
|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 :-)

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

matz.

4 Answers

David A. Black

4/2/2008 6:42:00 AM

0

Hi --

On Mon, 31 Mar 2008, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "Re: inject's pathological case..."
> on Mon, 31 Mar 2008 00:27:58 +0900, "David A. Black" <dblack@rubypal.com> writes:
>
> |> AFAIK, Matz correct me if I'm wrong, Ruby got the name inject from
> |> Smalltalk, along with collect, select, detect and several other
> |> methods. The other three I mention have alias, map for collect,
> |> find_all for select, and find for detect, but inject is still just
> |> inject in Ruby.
> |
> |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 :-)
>
> 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 :-) 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

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Julian Leviston

4/2/2008 6:51:00 AM

0

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


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

> Hi --
>
> On Mon, 31 Mar 2008, Yukihiro Matsumoto wrote:
>
>> Hi,
>>
>> In message "Re: inject's pathological case..."
>> on Mon, 31 Mar 2008 00:27:58 +0900, "David A. Black" <dblack@rubypal.com
>> > writes:
>>
>> |> AFAIK, Matz correct me if I'm wrong, Ruby got the name inject from
>> |> Smalltalk, along with collect, select, detect and several other
>> |> methods. The other three I mention have alias, map for collect,
>> |> find_all for select, and find for detect, but inject is still just
>> |> inject in Ruby.
>> |
>> |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 :-)
>>
>> 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 :-) 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


Robert Dober

4/2/2008 12:42:00 PM

0

On Wed, Apr 2, 2008 at 8:41 AM, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
>
>
> On Mon, 31 Mar 2008, Yukihiro Matsumoto wrote:
>
>
> > Hi,
> >
> > In message "Re: inject's pathological case..."
> > on Mon, 31 Mar 2008 00:27:58 +0900, "David A. Black"
> <dblack@rubypal.com> writes:
> >
> > |> AFAIK, Matz correct me if I'm wrong, Ruby got the name inject from
> > |> Smalltalk, along with collect, select, detect and several other
> > |> methods. The other three I mention have alias, map for collect,
> > |> find_all for select, and find for detect, but inject is still just
> > |> inject in Ruby.
> > |
> > |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 :-)
> >
> > 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 :-) 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
>
> --
> Rails training from David A. Black and Ruby Power and Light:
> ADVANCING WITH RAILS April 14-17 New York City
> INTRO TO RAILS June 9-12 Berlin
> ADVANCING WITH RAILS June 16-19 Berlin
> See http://www.r... for details and updates!
>
>

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

--
http://ruby-smalltalk.blo...

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

Rick DeNatale

4/2/2008 3:32:00 PM

0

On Wed, Apr 2, 2008 at 2:50 AM, Julian Leviston <julian@coretech.net.au> wrote:
> On 02/04/2008, at 5:41 PM, David A. Black wrote:
> > On Mon, 31 Mar 2008, Yukihiro Matsumoto wrote:

> > > In message "Re: inject's pathological case..."
> > > on Mon, 31 Mar 2008 00:27:58 +0900, "David A. Black"
> <dblack@rubypal.com> writes:

Quoting me:

> > > |> AFAIK, Matz correct me if I'm wrong, Ruby got the name inject from
> > > |> Smalltalk, along with collect, select, detect and several other
> > > |> methods. The other three I mention have alias, map for collect,
> > > |> find_all for select, and find for detect, but inject is still just
> > > |> inject in Ruby.
> > > |
> > > |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 :-)
> > >
> > > 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 :-) 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. <G>

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...