[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ActiveSupport Enumerable#sum should not use #size

Trans

3/7/2008 7:18:00 PM

ActiveSupport defines this for Enumerable#sum:

module Enumerable

def sum(identity = 0, &block)
return identity unless size > 0

if block_given?
map(&block).sum
else
inject { |sum, element| sum + element }
end
end

end

The use of #size shouldn't be used in an Enumerable method --since it
is not part of Enumerable's defined interface..... Ah, I was just
about to ask what anyone thought the fix to this is, but it occurs to
me that it might be:

def sum(identity = 0, &block)
if block_given?
map(&block).sum
else
inject { |sum, element| sum + element } || identity
end
end

Look right?

T.

3 Answers

Stefan Lang

3/7/2008 8:34:00 PM

0

2008/3/7, Trans <transfire@gmail.com>:
> ActiveSupport defines this for Enumerable#sum:
>
> module Enumerable
>
> def sum(identity = 0, &block)
> return identity unless size > 0
>
> if block_given?
> map(&block).sum
> else
> inject { |sum, element| sum + element }
> end
> end
>
> end
>
> The use of #size shouldn't be used in an Enumerable method --since it
> is not part of Enumerable's defined interface..... Ah, I was just
> about to ask what anyone thought the fix to this is, but it occurs to
> me that it might be:
>
> def sum(identity = 0, &block)
> if block_given?
> map(&block).sum
> else
> inject { |sum, element| sum + element } || identity

inject takes the identity as first parameter:

inject(identity) { |sum, element| sum + element }

and since they define Symbol#to_proc, this can even be

inject(identity, &:+)

> end
> end
>
> Look right?
>
>
> T.

Stefan

Drew Olson

3/7/2008 9:39:00 PM

0

Stefan Lang wrote:
> inject takes the identity as first parameter:
>
> inject(identity) { |sum, element| sum + element }
>
> and since they define Symbol#to_proc, this can even be
>
> inject(identity, &:+)
>
>> end
>> end
>>
>> Look right?
>>
>>
>> T.
>
> Stefan

This is very concise, but doesn't have the same behavior as the original
sum. This will start any sum with the identity and then build on it from
there. Not that this is incorrect behavior, it's just different from
what was originally defined. For example:

a = [1,2,3]

original sum, with identity = 10, would return 6, whereas your version
would return 16.

- Drew
--
Posted via http://www.ruby-....

Stefan Lang

3/7/2008 11:08:00 PM

0

2008/3/7, Drew Olson <olsonas@gmail.com>:
> Stefan Lang wrote:
> > inject takes the identity as first parameter:
> >
> > inject(identity) { |sum, element| sum + element }
> >
> > and since they define Symbol#to_proc, this can even be
> >
> > inject(identity, &:+)
> >
> >> end
> >> end
> >>
> >> Look right?
> >>
> >>
> >> T.
> >
> > Stefan
>
>
> This is very concise, but doesn't have the same behavior as the original
> sum. This will start any sum with the identity and then build on it from
> there. Not that this is incorrect behavior, it's just different from
> what was originally defined. For example:
>
> a = [1,2,3]
>
> original sum, with identity = 10, would return 6, whereas your version
> would return 16.
>
> - Drew

Hm, right. I find the name "identity" misleading in this case.

Stefan