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

dynamic arguments (was: The Sort Problem)

Thread Next
Jonathan Lang
February 12, 2004 20:14
dynamic arguments (was: The Sort Problem)
Message ID:
Jonathan Lang wrote:
> Luke Palmer wrote:
> > I've been thinking about this problem which comes up in my code a lot:
> > 
> >    @sorted = sort { $^'bar').compute <=> $^'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 {$'bar').compute}, $^a, $^b }


  @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.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About