Inject's pathological case

That is quite a convoluted way to say

result.any?

Kind regards

  robert

···

On 30.03.2008 16:32, Robert Dober wrote:

I found this code in Ruby1.9 though

       if result.size > 0 and result.inject(false) {|k,s| s or k}

that kind of code explains why inject has a bad reputation.

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 Mon, 31 Mar 2008 10:06:59 +0900, Julian Leviston 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}

Hi --

···

On Mon, 31 Mar 2008, Rick DeNatale wrote:

On Sun, Mar 30, 2008 at 10:30 AM, Marc Heiler <shevegen@linuxmail.org> wrote:

Understand that it's a way to reduce/fold the elements

> of an enumerable into a single value.

Not that this is important, but personally I never liked the name
.inject
Though fold(l/r) is not much better either.

A peculiar thing is that the wikipedia page gives "also known variously
as " four different names as alternative/example.

It's because the same thing has surfaced in several languages using
different terms, and those languages have in turn influenced others.

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

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.rubypal.com for details and updates!

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

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

              matz.

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

--
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.rubypal.com for details and updates!

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

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

···

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

--
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.rubypal.com for details and updates!

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

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

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

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>

···

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:

--
Rick DeNatale

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