[ruby-core:26463] New Features for the Array Module

View: New views
7 Messages — Rating Filter:   Alert me  

[ruby-core:26463] New Features for the Array Module

by Daniel Cohen-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

To Whom it May Concern:

I have created a new patch for the Array Module of Ruby. The patch adds two complementary features: A sum function, that sums the elements in an array if they are numbers; and a mean (or average) function that finds the mean of the elements in the the array using the sum function. Attached is a patch to implement these features. I apologize in advance if this has already been proposed.

Sincerely,
Daniel Cohen


add_array_sum_and_mean_with_u.diff (2K) Download Attachment

[ruby-core:26471] Re: New Features for the Array Module

by Kornelius Kalnbach :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel Cohen wrote:
> A sum function, that sums the elements in an array if they are
> numbers;
I use this one a lot; I actually thought it was in 1.9 already!

The ActiveSupport implementation of Array#sum is particularly useful
because #+ is defined for a lot of classes, like Strings (so #sum works
like #join) or Arrays (a one-level #flatten).

Your patch limits the method to Floats; this is not Ruby-like, in my
view, even if it is faster.

> and a mean (or average) function that finds the mean of the
> elements in the the array using the sum function.
This one, however, really only applies to numerical objects. I'm not
sure about such a method being included into standard Ruby, especially
when it can be added with

  class Array
    def mean
      sum / size
    end
  end

easily.

So:
+1 for Array#sum if it is defined as #inject(&:+)
-1 on type-limited Array#sum and Array#mean

On second thought: Why not implement this as Enumerable#sum?

[murphy]


[ruby-core:26472] Re: New Features for the Array Module

by Daniel Cohen-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Kornelius,
Thank you for you suggestions, I'll try and put out another patch that moves the sum code to enumerable, and scrap the mean function all together. Also, i'll try to use the Enumerable#inject method to make the function more generic.

Thanks,
Daniel Cohen

On Sun, Nov 1, 2009 at 9:56 AM, Kornelius Kalnbach <murphy@...> wrote:
Daniel Cohen wrote:
> A sum function, that sums the elements in an array if they are
> numbers;
I use this one a lot; I actually thought it was in 1.9 already!

The ActiveSupport implementation of Array#sum is particularly useful
because #+ is defined for a lot of classes, like Strings (so #sum works
like #join) or Arrays (a one-level #flatten).

Your patch limits the method to Floats; this is not Ruby-like, in my
view, even if it is faster.

> and a mean (or average) function that finds the mean of the
> elements in the the array using the sum function.
This one, however, really only applies to numerical objects. I'm not
sure about such a method being included into standard Ruby, especially
when it can be added with

 class Array
   def mean
     sum / size
   end
 end

easily.

So:
+1 for Array#sum if it is defined as #inject(&:+)
-1 on type-limited Array#sum and Array#mean

On second thought: Why not implement this as Enumerable#sum?

[murphy]



[ruby-core:26481] Re: New Features for the Array Module

by Yukihiro Matsumoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

In message "Re: [ruby-core:26471] Re: New Features for the Array Module"
    on Sun, 1 Nov 2009 22:56:14 +0900, Kornelius Kalnbach <murphy@...> writes:

|So:
|+1 for Array#sum if it is defined as #inject(&:+)
|-1 on type-limited Array#sum and Array#mean

-1 for #sum to calculate not based on numeric addition.
Use inject(&:+) otherwise.

                                                        matz.


[ruby-core:26482] Re: New Features for the Array Module

by Nobuyoshi Nakada-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

At Sun, 1 Nov 2009 09:20:21 +0900,
Daniel Cohen wrote in [ruby-core:26463]:
>
> To Whom it May Concern:
>
> I have created a new patch for the Array Module of Ruby. The patch adds two
> complementary features: A sum function, that sums the elements in an array
> if they are numbers; and a mean (or average) function that finds the mean of
> the elements in the the array using the sum function. Attached is a patch to
> implement these features. I apologize in advance if this has already been
> proposed.

+ if( TYPE(RARRAY_PTR(ary)[i]) == T_STRING) {
+ rb_raise(rb_eTypeError, "Element is not a number!");

This check is not necessary since NUM2DBL rejects a string.

--
Nobu Nakada


[ruby-core:26631] Re: New Features for the Array Module

by Ken Bloom-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, 01 Nov 2009 09:20:21 +0900, Daniel Cohen wrote:

> To Whom it May Concern:
>
> I have created a new patch for the Array Module of Ruby. The patch adds
> two complementary features: A sum function, that sums the elements in an
> array if they are numbers; and a mean (or average) function that finds
> the mean of the elements in the the array using the sum function.
> Attached is a patch to implement these features. I apologize in advance
> if this has already been proposed.
>
> Sincerely,
> Daniel Cohen
> To Whom it May Concern:<br><br>I have created a new patch for the Array
> Module of Ruby. The patch adds two complementary features: A sum
> function, that sums the elements in an array if they are numbers; and a
> mean (or average) function that finds the mean of the elements in the
> the array using the sum function. Attached is a patch to implement these
> features. I apologize in advance if this has already been proposed.<br>
> <br>Sincerely,<br>Daniel Cohen<br>

Shouldn't the sum of integers return an integer? Converting everything to
double when there are no doubles in the array can cause a loss of
precision, and is definitely surprising to the user who doesn't expect
this.

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/



[ruby-core:26638] Re: New Features for the Array Module

by NARUSE, Yui-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/11/09 4:08, Ken Bloom wrote:
> Shouldn't the sum of integers return an integer? Converting everything to
> double when there are no doubles in the array can cause a loss of
> precision, and is definitely surprising to the user who doesn't expect
> this.

His patch which converts integers to doubles shows,
his "Array" is not Ruby's Array; it likes NArray or some typed array.
It conflicts Ruby's large class-ism.

--
NARUSE, Yui  <naruse@...>