----- Original Message ----- 
From: "Damian Conway" <[EMAIL PROTECTED]>
To: "Perl 6 Language" <[EMAIL PROTECTED]>
Sent: Thursday, February 19, 2004 8:29 PM
Subject: [perl] The Sort Problem: a definitive ruling


> C<sort> in Perl6 is a global multisub:
>
>      multi sub *sort(Criterion @by: [EMAIL PROTECTED]) {...}
>      multi sub *sort(Criterion $by: [EMAIL PROTECTED]) {...}
>      multi sub *sort(             : [EMAIL PROTECTED]) {...}
>
> where:
>
>      type KeyExtractor ::= Code(Any) returns Any;
>
>      type Comparator   ::= Code(Any, Any) returns Int;
>
>      type Criterion    ::= KeyExtractor
>                          | Comparator
>                          | Pair(KeyExtractor, Comparator)
>                          ;
   snip

> If a key-extractor block returns number, then C<< <=> >> is used to
compare
> those keys. Otherwise C<cmp> is used. In either case, the keys extracted
by
> the block are cached within the call to C<sort>, to optimize subsequent
> comparisons against the same element. That is, a key-extractor block is
only
> ever called once for each element being sorted.
>
>

   How do you decide whether a key-extractor block returns number?  Do you
look at the signature,  or do you simply evaluate the result of the
key-extractor for each element in the unsorted list?  For example, what is
the result of the following code?

      sort {$_.key} (1=> 'a', 10 => 'b', 2 =>'c');

   There is nothing in the signature of the key-extractor to suggest that
all the keys are numbers, but as it turns out they all are.  Will the sort
end up being numerical or alphabetic?


Joe Gottman


Reply via email to