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

Re: The Sort Problem: a definitive ruling

Thread Previous | Thread Next
From:
Damian Conway
Date:
February 19, 2004 19:07
Subject:
Re: The Sort Problem: a definitive ruling
Message ID:
40357835.9020100@conway.org
Dave Whipp wondered:

>>     @sorted = sort {-M} @unsorted;
> 
> One thing I've been trying to figure out reading this: what is the signature
> of prefix:-M ? i.e. how does it tell the outer block that it (the
> outer-block) needs a parameter?

It doesn't. As A6 explained:

	http://dev.perl.org/perl6/apocalypse/A06.html#Bare_subs

any block that doesn't have placeholder-specified parameters but which refers 
(even implicitly) to $_ will automatically have the signature of ($_).

That's why:

	@odd = grep { $_ % 2 } @nums;

will still work in Perl 6.

Since a bare C<-M> implicitly refers to $_, the surrounding block 
automagically gets a one-parameter signature and hence is (correctly!) 
interpreted as a key extractor.

Don't you just love it when a plan^H^H^H^Hdesign comes together? ;-)


> There seems to be some transitive magic going on here. 

There is. Kinda. Just not the type of magic you thought.


> Could similar magic be used to have infix:<=> require two
> higher-order variables (e.g. could "sort { <=> } @unsorted" be made to
> work?)

No. But this will work:

	sort &infix:<=> @unsorted

Damian

Thread Previous | 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