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.

  # 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;

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?

-- 
Aaron Crane * GBdirect Ltd.
http://training.gbdirect.co.uk/courses/perl/

Reply via email to