On Friday, February 20, 2004, at 05:48 , Damian Conway wrote:

Joe Gottman asked:

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?

Whilst I'd very much like it to analyse the keys, detect that they're all numbers, and use C<< <=> >>

Eek! Please don't even TRY to do that. It'd be creepy if the same call to sort could swap at runtime between numeric and string comparisons based upon its input. I would hope that the determination be made at compile time.


Consider the poor schmuck sorting new objects in preparation for a merge sort, only to find that his new array isn't sorted the same as his old array was, even though they came back from the exact same call to sort... Blech.


But if sort's arguments were specifically typed, i.e.:


        my @array of Int;
        @array = sort @array;

Does this meet the "key extractor returns number" qualification?



Gordon Henriksen
[EMAIL PROTECTED]

Reply via email to