[Pharo-project] Fwd: [Pharo-users] another confusing result

3 views
Skip to first unread message

Mariano Martinez Peck

unread,
Jan 14, 2012, 6:55:55 AM1/14/12
to Pharo Development


---------- Forwarded message ----------
From: Larry White <ljw...@gmail.com>
Date: Tue, Jan 10, 2012 at 12:52 AM
Subject: Re: [Pharo-users] another confusing result
To: A friendly place where any question about pharo is welcome <pharo...@lists.gforge.inria.fr>


Thanks.  I wonder if this method needs to stay the same for compatibility reasons or if it could be modified in Pharo to produce a less surprising result. In other words, is this a bug or a feature? 


On Mon, Jan 9, 2012 at 1:41 PM, Runar Jordahl <runar....@gmail.com> wrote:
Here is a copy of something I wrote about this earlier:

In Pharo 1.3, SequenceableCollection>>combinations:atATimeDo: will,
for all combinations, send the same collection instance as argument
for the block. Therefore, if you use this collection itself, you will
be surprised:

|answer|
answer := OrderedCollection new.
#(a b c) combinations: 2 atATimeDo: [:each | answer add: each].
answer

Here I expect to end up with a collection looking like this:
1:  #(#a #b)
2:  #(#a #c)
3:  #(#b #c)

But I end up with:
1:  #(#c #c)
2:  #(#c #c)
3:  #(#c #c)

One fix is to change the client code to copy the argument for the block:
|answer|
answer := OrderedCollection new.
#(a b c) combinations: 2 atATimeDo: [:each | answer add: each copy].
answer

Another solution is enhancing
SequenceableCollection>>combinationsAt:in:after:do: to copy the
collection:
nn + 1 to: self size do: [ :index |
              aCollection at: jj put: (self at: index).
              jj = aCollection size
                      ifTrue: [ aBlock value: aCollection copy ]
                      ifFalse: [
                              self
                                      combinationsAt: jj + 1
                                      in: aCollection
                                      after: index
                                      do: aBlock ] ]



Kind regards
Runar Jordahl





--
Mariano
http://marianopeck.wordpress.com

Milan Mimica

unread,
Jan 16, 2012, 12:18:42 PM1/16/12
to Pharo-...@lists.gforge.inria.fr
I would just remove the method. Probably the best fix ever.
--
Milan Mimica
http://sparklet.sf.net
Reply all
Reply to author
Forward
0 new messages