A functional splice, though, shouldn't have side-effects.Then use a slice instead. No, that's not Perl keyword, instead, it's a pattern you implement using array indexes.
Since we need the same random index more than once, I stuff it into a temporary variable. I don't think you consider those very "functional", but what the heck...
sub quite_functional_FY { my $i = int rand @_; return @_ ? ($_[$i], quite_functional_FY(@_[0 .. $i-1, $i+1 .. $#_ +])) : (); }
I think you can get a faster implementation by avoiding the recursion for lists that don't need shuffling: lists of one item (or less: 0).
sub quite_functional_FY { my $i = int rand @_; return @_ > 1 ? ($_[$i], quite_functional_FY(@_[0 .. $i-1, $i+1 .. + $#_])) : @_; }
In reply to Re^3: Functional shuffle
by bart
in thread Functional shuffle
by Roy Johnson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |