develooper Front page | perl.perl6.language | Postings from February 2004

dynamic arguments (was: The Sort Problem)

Thread Next
From:
Jonathan Lang
Date:
February 12, 2004 20:14
Subject:
dynamic arguments (was: The Sort Problem)
Message ID:
20040213041440.28310.qmail@web40802.mail.yahoo.com
Jonathan Lang wrote:
> Luke Palmer wrote:
> > 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.
> 
> The problem, then, isn't specific to sort; it happens any time that you
> find yourself needing to do the same things to multiple arguments.  As
> such, the solution ought to be more general than just sort.  
> 
> You're looking for something to the effect of:
> 
>   @sorted = sort { parameters($^a <=> $^b).foo('bar').compute }
> 
> That is, you need a way to factor C<.foo('bar').compute> out from each
> of the arguments that it applies to.  For list arguments, this is
> straightforward: pipe the list arguments through a map before you pipe
> them into the routine.  A similar approach could be used with named
> arguments.  The problem comes when you deal with positional arguments.  

How about including something similar to <==, but which binds the elements
of the list to the various positional parameters?  For instance: 

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

Where

  @x = $a, $b, $c; 
  routine args @x; 

is equivelent to

  routine $a, $b, $c;

=====
Jonathan "Dataweaver" Lang

__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About