I don't know why you'd need to make a copy inside the block but this works:
| m p |
m := #(1 2 3).
p := Bag new.
m permutationsDo: [:each |
Transcript show: each ; cr.
p add: each copy.
Transcript show: p ; cr.
].
Transcript show: '------'; cr.
p do: [:each| Transcript show: each; cr]
The Transcript shows:
#(1 2 3)
a Bag(#(1 2 3))
#(1 3 2)
a Bag(#(1 2 3) #(1 3 2))
#(2 1 3)
a Bag(#(1 2 3) #(1 3 2) #(2 1 3))
#(2 3 1)
a Bag(#(2 3 1) #(1 2 3) #(1 3 2) #(2 1 3))
#(3 2 1)
a Bag(#(2 3 1) #(1 2 3) #(1 3 2) #(2 1 3) #(3 2 1))
#(3 1 2)
a Bag(#(2 3 1) #(1 2 3) #(1 3 2) #(2 1 3) #(3 2 1) #(3 1 2))
------
#(2 3 1)
#(1 2 3)
#(1 3 2)
#(2 1 3)
#(3 2 1)
#(3 1 2)
On 10/01/2012 10:14 AM, Ginny Hendry wrote:
> I would like to get a collection of permutations of another
> collection but I can't make it work.
>
> This ends up incorrectly with the same element many times in Bag p:
>
> | m p |
> m := #(1 2 3).
> p := Bag new.
> m permutationsDo: [:each |
> Transcript show: each ; cr.
> p add: each.
> Transcript show: p ; cr.
> ].
> Transcript show: '------'; cr.
> p do: [:each| Transcript show: each; cr]
>
> But this very similar code ends up correctly with different
> elements in Bag p:
>
> | m p |
> m := #(1 2 3).
> p := Bag new.
> m do: [:each |
> Transcript show: each ; cr.
> p add: each.
> Transcript show: p ; cr.
> ].
> Transcript show: '------'; cr.
> p do: [:each| Transcript show: each; cr]
>
> Any ideas about what am I doing wrong? How can I get a collection of permutations?
>
> Working in Pharo 1.4 one-click for Mac OSX.
>
> Thanks.
>
> -Ginny
>