A new version of Collections was added to project The Inbox:
http://source.squeak.org/inbox/Collections-ul.587.mcz ==================== Summary ==================== Name: Collections-ul.587 Author: ul Time: 18 November 2014, 11:53:01.797 pm UUID: 30ba8551-0c97-4cf5-b830-9386f272b8d2 Ancestors: Collections-ul.586 Optimized Interval >> #sum. =============== Diff against Collections-ul.586 =============== Item was added: + ----- Method: Interval>>sum (in category 'accessing') ----- + sum + "Optimized version. Use the sum(n*i - k, i=a..b) = -1/2*(a - b - 1)*(n * (a + b) - 2 * k) equation with a = 1, n = step, b = self size." + + | b | + b := self size. + ^b * ((b + 1) * step - (step - start * 2)) / 2! |
Hi All,
The reason why I put this into The Inbox instead of The Trunk is that it changes the behavior of #sum when the Interval is empty. The current implementation raises an error - which is correct for a Collection which may contain any object - but I think for Intervals the preferred behavior is to return 0. Any objections? Levente On Tue, 18 Nov 2014, [hidden email] wrote: > A new version of Collections was added to project The Inbox: > http://source.squeak.org/inbox/Collections-ul.587.mcz > > ==================== Summary ==================== > > Name: Collections-ul.587 > Author: ul > Time: 18 November 2014, 11:53:01.797 pm > UUID: 30ba8551-0c97-4cf5-b830-9386f272b8d2 > Ancestors: Collections-ul.586 > > Optimized Interval >> #sum. > > =============== Diff against Collections-ul.586 =============== > > Item was added: > + ----- Method: Interval>>sum (in category 'accessing') ----- > + sum > + "Optimized version. Use the sum(n*i - k, i=a..b) = -1/2*(a - b - 1)*(n * (a + b) - 2 * k) equation with a = 1, n = step, b = self size." > + > + | b | > + b := self size. > + ^b * ((b + 1) * step - (step - start * 2)) / 2! > > > |
On Tue, Nov 18, 2014 at 3:08 PM, Levente Uzonyi <[hidden email]> wrote: Hi All, I want to add a non-objection, an anti-objection, or whatever will cause you to put this in trunk ;-) Clearly the bug is in the original sum. It should read sum "Compute the sum of all the elements in the receiver" ^self isEmpty ifTrue: [0] ifFalse: [self reduce:[:a :b| a + b]] Levente best,
Eliot |
In reply to this post by Levente Uzonyi-2
On Tue, Nov 18, 2014 at 5:08 PM, Levente Uzonyi <[hidden email]> wrote:
> Hi All, > > The reason why I put this into The Inbox instead of The Trunk is that it > changes the behavior of #sum when the Interval is empty. > The current implementation raises an error - which is correct for a > Collection which may contain any object - but I think for Intervals the > preferred behavior is to return 0. Any objections? 0 seems very reasonable. |
In reply to this post by Eliot Miranda-2
There was a thread[1] about this back in 2008 with some good points.
I think it would be worth introducing #sumWithZero:. With that in the image we could change Collection >> #sum as you suggested with a more explicit comment. It could state that it's intended to be used only with Collections containing Numbers only, and #sumWithZero: should be used in every other case. Levente [1] http://lists.squeakfoundation.org/pipermail/squeak-dev/2008-August/130592.html On Tue, 18 Nov 2014, Eliot Miranda wrote: > > > On Tue, Nov 18, 2014 at 3:08 PM, Levente Uzonyi <[hidden email]> wrote: > Hi All, > > The reason why I put this into The Inbox instead of The Trunk is that it changes the behavior of #sum when the Interval is empty. > The current implementation raises an error - which is correct for a Collection which may contain any object - but I think for Intervals the preferred behavior is to return 0. Any objections? > > > I want to add a non-objection, an anti-objection, or whatever will cause you to put this in trunk ;-) Clearly the bug is in the original sum. It should read > > sum > "Compute the sum of all the elements in the receiver" > > ^self isEmpty ifTrue: [0] ifFalse: [self reduce:[:a :b| a + b]] > > Levente > > On Tue, 18 Nov 2014, [hidden email] wrote: > > A new version of Collections was added to project The Inbox: > http://source.squeak.org/inbox/Collections-ul.587.mcz > > ==================== Summary ==================== > > Name: Collections-ul.587 > Author: ul > Time: 18 November 2014, 11:53:01.797 pm > UUID: 30ba8551-0c97-4cf5-b830-9386f272b8d2 > Ancestors: Collections-ul.586 > > Optimized Interval >> #sum. > > =============== Diff against Collections-ul.586 =============== > > Item was added: > + ----- Method: Interval>>sum (in category 'accessing') ----- > + sum > + "Optimized version. Use the sum(n*i - k, i=a..b) = -1/2*(a - b - 1)*(n * (a + b) - 2 * k) equation with a = 1, n = step, b = self size." > + > + | b | > + b := self size. > + ^b * ((b + 1) * step - (step - start * 2)) / 2! > > > > > > > > -- > best,Eliot > > |
Free forum by Nabble | Edit this page |