Aaron Crane writes: > Luke Palmer wrote: > > Any other ideas? > > How about something like this, modulo any errors in my Perl 6 syntax? > > sub sort(?&cmp = &infix:cmp, +$key, +$desc, [EMAIL PROTECTED]) { ... } > > I think that allows all of these: > > # P5: @sorted = sort @unsorted; > @sorted = sort @unsorted; > > The simplest case is the same as the Perl 5, which seems a pleasant feature.
This is a feature that, IMO, we can not do without. I still do things like: print "$_: %hash{$_}" for sort keys %hash; All the time, and I don't want to give that up for some "clearer" syntax, as there is no such thing. > > # P5: @sorted = sort { $a <=> $b } @unsorted; > @sorted = sort { $^a <=> $^b } @unsorted; # or: > @sorted = sort &infix:<=> <== @unsorted; > > This also seems reasonable. > > # P5: @sorted = sort { $a->foo('bar')->compute <=> $b->foo('bar')->compute } > # @unsorted > # or: @sorted = map { $_->[1] } > # sort { $a->[0] <=? $b->[0] } > # map { [ $_->foo('bar')->compute, $_ ] } > # @unsorted > @sorted = sort &infix:<=>, key => { $_.foo('bar').compute } <== @unsorted; Ok, I have to say, that's pretty good. Er, really good. I like it a lot. > I think my suggestion wins big here. We've only had to specify how to > extract the key, and sort itself takes care of everything else. And it looks > to me like this sort function has enough information about the programmer's > intent for it to optimise in all sorts of exciting ways -- it should be able > to do the equivalent of the GRT internally, for example. > > Just for kicks, this one demonstrates all the features. It's the same as > before, but in descending order: > > @unsorted > ==> sort &infix:<=>, desc => 1, key => { $_.foo('bar').compute } > ==> @sorted; > > What problems can anyone spot with this suggestion? I don't like the C<desc> flag. But I can't, at the moment, think of any way around it short of: @unsorted ==> sort { $^b <=> $^a }, key => { .foo('bar').compute } ==> @sorted Which people have made pretty clear that they don't like. Oh, by the way, your Perl 6 syntax is immaculate. Bravo :-) Luke