Update 2011-04-15: Robert asks

Perl 6 semantics


Most languages or libraries that provide a generic sort routine allow you to specify a comparator, that is a callback that tells the sort routine how two given elements compare. Perl is no exception.

For example in Perl 5, which defaults to lexicographic ordering, you can request numeric sorting like this:

use v5; my @sorted = sort { $a $b } @values;

Perl 6 offers a similar option:

use v6; my @sorted = sort { $^a $^b }, @values;

The main difference is that the arguments are not passed through the global variables $a and $b, but rather as arguments to the comparator. The comparator can be anything callable, that is a named or anonymous sub or a block. The { $^a $^b} syntax is not special to sort, I have just used placeholder variables to show the similarity with Perl 5. Other ways to write the same thing are:

my @sorted = sort -> $a, $b { $a $b }, @values; my @sorted = sort * *, @values; my @sorted = sort &infix:«», @values;

The first one is just another syntax for writing blocks, * * use * to automatically curry an argument, and the final one directly refers to the routine that implements the "space ship" operator (which does numeric comparison).

But Perl strives not only to make hard things possible, but also to make simple things easy. Which is why Perl 6 offers more convenience. Looking at sorting code, one can often find that the comparator duplicates code. Here are two common examples:

# sort words by a sort order defined in a hash: my %rank = a => 5, b => 2, c => 10, d => 3; say sort { %rank{$^a} %rank{$^b} }, 'a'..'d'; # ^^^^^^^^^^ ^^^^^^^^^^ code duplication # sort case-insensitively say sort { $^a.lc cmp $^b.lc }, @words; # ^^^^^^ ^^^^^^ code duplication

Since we love convenience and hate code duplication, Perl 6 offers a shorter solution:

# sort words by a sort order defined in a hash: say sort { %rank{$_} }, 'a'..'d'; # sort case-insensitively say sort { .lc }, @words;

sort is smart enough to recognize that the code object code now only takes a single argument, and now uses it to map each element of the input list to new values, which it then sorts with normal cmp sort semantics. But it returns the original list in the new order, not the transformed elements. This is similar to the Schwartzian Transform, but very convenient since it's built in.

So the code block now acts as a transformer, not a comparator.

You might also like
Definition of Neurolinguistics
Definition of Neurolinguistics
Ashish Kumar Principals of Programming Language
Mobile Application (Ashish Kumar)
  • This unique free application is for all students across the world. It covers 127 topics of Principals of Programming Language in detail. These 127 topics are divided...
  • Each topic is around 600 words and is complete with diagrams, equations and other forms of graphical representations along with simple text explaining the concept...
  • This USP of this application is ultra-portability . Students can access the content on-the-go from anywhere they like.
  • Basically, each topic is like a detailed flash card and will make the lives of students simpler and easier.
  • Some of topics Covered in this application are:
  • 1. Abstract Machines
  • 2. The Interpreter
  • 3. Low-level and High-level languages
  • 4. Example of an Abstract Machine
  • 5. Describe a Programming Language
Related Posts