This project is archived and is in readonly mode.
Enumerable#sum should not rely on size
Reported by Marc-André Lafortune | April 14th, 2009 @ 06:27 AM | in 2.3.4
Enumerable#sum should be valid for any Enumerable, not just those that respond_to? :size (like Array but unlike Range, for example).
My simple change maintains compatibility and doesn't rely on 'size'. I added a test attempting to sum a range which raises an exception with the previous definition of sum.
Thanks.
Comments and changes to this ticket
-
CancelProfileIsBroken August 6th, 2009 @ 02:18 PM
- Tag changed from enumerable, patch, sum to bugmash, enumerable, patch, sum
-
Renaud Kern August 8th, 2009 @ 11:01 AM
patch and test pass. It works with Range, Array and Set. It's more consistent.
-
José Valim August 8th, 2009 @ 11:27 AM
Patch and tests pass, I'm just not sure if we should optimize ranges sum to use Arithmetic Progression sum. This way we would avoid instantiate and iterate through all the collection:
class Range def sum(identity=0, &block) if block_given map(&block).sum(identity) else (last - first + 1) * (last + first) / 2 end end end Range.new(1, 4).sum #=> 10 Range.new(1, 10000).sum #=> 50005000
-
Dan Pickett August 9th, 2009 @ 12:04 AM
+1 verified
I like Jose's optimization idea for range as well.
-
Jeremy Kemper August 9th, 2009 @ 01:52 AM
- State changed from new to committed
- Tag changed from bugmash, enumerable, patch, sum to enumerable, patch, sum
- Milestone changed from 2.x to 2.3.4
2-3-stable: 819c347f436f54acea5821289c6340da863bf138
master: 29096268ccce2b13e1490c8b673ffe0b498555fc
José, that'd make a nice followup patch!
-
Repository August 9th, 2009 @ 05:26 AM
(from [29096268ccce2b13e1490c8b673ffe0b498555fc]) Enumerable#sum now works will all enumerables, even if they don't respond to :size
[#2489 state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/29096268ccce2b13e1490c8b673ffe... -
Repository August 9th, 2009 @ 05:26 AM
(from [819c347f436f54acea5821289c6340da863bf138]) Enumerable#sum now works will all enumerables, even if they don't respond to :size
[#2489 state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/819c347f436f54acea5821289c6340... -
Repository August 9th, 2009 @ 04:03 PM
(from [1bd4d1c67459a91415ee73a8f55d2309c0d62a87]) Optimize Range#sum to use arithmetic progression when a block is not given [#2489].
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/1bd4d1c67459a91415ee73a8f55d23... -
Repository August 9th, 2009 @ 06:19 PM
(from [e0adfa82c05f9c975005f102b4bcaebfcd17d241]) Optimize Range#sum only for integers [#2489] http://github.com/rails/rails/commit/e0adfa82c05f9c975005f102b4bcae...
-
Repository August 9th, 2009 @ 09:47 PM
(from [ae47f7575da3dc3c74fa63136d00492ba4beb278]) Improving test coverage for Range#sum [#2489]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/ae47f7575da3dc3c74fa63136d0049...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 2489 Enumerable#sum should not rely on size [#2489 state:committed]
- 2489 Enumerable#sum should not rely on size [#2489 state:committed]
- 2489 Enumerable#sum should not rely on size (from [1bd4d1c67459a91415ee73a8f55d2309c0d62a87]) Optimiz...
- 2489 Enumerable#sum should not rely on size (from [e0adfa82c05f9c975005f102b4bcaebfcd17d241]) Optimiz...
- 2489 Enumerable#sum should not rely on size (from [ae47f7575da3dc3c74fa63136d00492ba4beb278]) Improvi...