Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion The Sort Problem
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Luke Palmer  
View profile  
 More options Feb 11 2004, 10:48 pm
Newsgroups: perl.perl6.language
From: fibon...@babylonia.flatirons.org (Luke Palmer)
Date: Wed, 11 Feb 2004 20:11:34 -0700
Local: Wed, Feb 11 2004 10:11 pm
Subject: The Sort Problem
I've been thinking about this problem which comes up in my code a lot:

    @sorted = sort { $^a.foo('bar').compute <=> $^b.foo('bar').compute }
                @unsorted;

Often the expressions on each side are even longer than that.  But one
thing remains:  both sides are exactly the same, substitute a $^b for a
$^a.

I can see a couple less-than-desirable ways around this redundancy:

    @sorted = sort { infix:<=>( *($^a, $^b)».foo('bar').compute ) }
                @unsorted;

Which doesn't work if .compute returns a list... not to mention its
horrible ugliness.  Another is to define a variant of sort (haven't had
much practice with A6 material recently; here we go!):

    multi sub sort (&block($) = { $_ } : *@data) {
        sort { block($^a) cmp block($^b) } @data;
    }

    @sorted = sort { .foo('bar').compute } @unsorted;

Which has the disadvantage of forcing you to use C<cmp> and forcing an
ascending sort.

Any other ideas?  Is a more general solution necessary?

Luke


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.