On Fri, 5 Apr 2002 [EMAIL PROTECTED] wrote:
> On Fri, Apr 05, 2002 at 12:17:27AM +0300, Ilmari Karonen wrote:
> > The point of taking a reference is that the Fisher-Yates algorithm is an
> > in-place shuffle.  If your array happens to be a couple of megabytes in
> > size, you start to appreciate this feature.  So, since we have this nice
> 
> Ah, well, one could give the same argument for sort and map, but
> noone seems to object to them not performing in situ operations.

Well, map does allow in-place modification.  I seem to recall you taking
advantage of this feature before, and defending it quite vehemently
against accusations of obfuscation...  ;-)

And the request for an in-place sort seems to pop up quite regularly.  I
think someone has written a module that implements one.


> The advantages of taking a list as argument are there too:
>     my @shuffled = shuffle 1 .. 10;
> reads better than:
>     shuffle \my @shuffled = 1 .. 10;

Granted.  But we're giving a code example here, and adding optional
copy-and-shuffle semantics to the function would clutter the
implementation much more than it would simplify the interface.

And, as already stated, having _only_ copy-and-shuffle would not allow
the user to do an in-place shuffle, if they ever happen to need one.


>     @array = shuffle @array;   # Tada!

That's not an in-place shuffle.  It's a copy-and-shuffle, where the
final copy just happens to overwrite the original.  It still takes O(n)
extra memory[1], whereas a real in-place algorithm takes O(1).


[1] Actually, I suppose that could be optimized back to an in-place
shuffle if Perl had lazy array assignment.  Maybe Perl 6 will...

-- 
Ilmari Karonen - http://www.sci.fi/~iltzu/
"It'd be strange indeed if finding a fix for a bug in intuit_start turned
 out to be easier than writing the test for it."
                                        -- Hugo on the perl5-porters list


Reply via email to