develooper Front page | perl.perl5.porters | Postings from July 2021

Re: Elevator pitch, deprecating $a/$b sort globals by using sub{}

Thread Previous | Thread Next
From:
Darren Duncan
Date:
July 4, 2021 20:42
Subject:
Re: Elevator pitch, deprecating $a/$b sort globals by using sub{}
Message ID:
958c9fbc-6a64-d7a7-d354-fb53c83e4c6f@darrenduncan.net
On 2021-07-04 8:40 a.m., Martijn Lievaart wrote:
> Karl Wiliamson's remarks about $a/$b as a quirk got me thinking. With signatures 
> as a language feature it's feasable to make sort not only take a blok or a 
> subroutine name, but also a sub. That means $a and $b for sort can be deprecated 
> in the long run, and eventually turned off with a feature flag if we so choose.
> 
> So basically,
> 
>      sort { $a->{x} <=> $b->{x} } @list
> 
> could be written as:
> 
>      sort sub($a,$b){ $a->{x} <=> $b->{x} } @list
> 
> which is currently a syntax error.

If we're going to change something I would propose a minimal change is that in 
contexts like this make $_ a collection type whose elements are the operands. 
For example, make it an arrayref.

Then we can have:

   sort { $_->[0] <-> $_->[1] } @list

Or:

   sort { my_sort(@{$_}) @list

Which is then most similar to the other list operators using $_ already:

   map { my_map($_) } @list

Over a decade ago when I implemented sort methods for my Set::Relation module I 
used $_ like this because there didn't seem any way to emulate the 
topicalization of $a and $b like I could for $_ in my map/grep/etc methods.

-- Darren Duncan

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