Damian Conway wrote:
> sub sort (^&comparator, @list) {
> for (1..@list**3) {
> my ($i, $j) = (rand(@list), rand(@list));
> @list[$i,$j] = @list[$j,$i]
> unless $comparator->(a: $list[$i], b: $list[$j]);
> }
> return @list;
> }
I think this does the right thing too:
@out = sort ^0 cmp ^a, @in;
Since numbered placeholders have higher priority than named, it
should create the function
sub ($, $a) { $_[0] cmp $_[1] }
When the curry is evaluated, the a: parameter is bound to $_[1]
and the b: parameter fills in $_[0].
If this code is used instead:
@out = sort ^1 cmp ^a, @in;
Then a different function would be generated:
sub ($a, $) { $_[1] cmp $_[0] }
But the code still works.
That's kind of interesting -- the numbered placeholders define the
ordering for the parameters passed anonymously, while the named
placeholders define ordering for the parameters passed by keyword.
I keep feeling like there's something here to trip on, but I
can't find it.
- Ken
P.S. What the heck kind of sort is that?! O(N**3) random permutation
sort?