[Squeak] Newbie permutations
Richard A. O'Keefe
ok at cs.otago.ac.nz
Wed Apr 10 00:06:43 UTC 2002
Torge Husfeldt <jean-jacques.gelee at gmx.de> wrote:
foo := Array streamContents:
[strm |
#(1 3 5) permutationsDo:
[:perm |
strm nextPut: perm copy"copy needed because there is a bug in
OrderedCollection>>permutationsDo:"]]
The behaviour of permutationsDo: is ****NO**** bug.
(By the way, it's SequenceableCollection>>permutationsDo:,
not OrderedCollection>>permutationsDo:, which is just as well,
because #(1 3 5) is not an OrderedCollection.
1. It is not a bug because the code does EXACTLY what the method is
documented as doing. If the code and the comment disagreed,
that would be a bug. But they don't. The method works precisely
the way it is _meant_ to work.
2. Out of three alternatives:
(0) Scramble the original collection and pass that
(1) Make a shallowCopy, then scramble and pass the copy
(2) Make a shallowCopy, then on each iteration, scramble it,
copy it, and pass the new copy
the chosen one (1) is the most useful; the best engineered if you
like.
The reason is that for even quite small inputs, the amount of space
churned over by (2) would be extremely high, to no gain whatsoever.
If I do
#((a b c) (d e f) (g h i)) do: [:each| ...]
we do not call it a bug that the objects bound to 'each' are not
freshly copied for the occasion.
More information about the Squeak-dev
mailing list
|