Re: Does Perl 6 use $a and $b in sorting?

[email protected] (Tobias Leich)
Newsgroups perl.perl6.language
Message-ID <[email protected]>
sort accepts something callable with an arity of 2.

Subroutines, blocks and pointies will do:

say sort { $^a cmp $^b }, 5, 3, 2, 6, 4
OUTPUT«(2 3 4 5 6)␤»

say sort { $^left cmp $^right }, 5, 3, 2, 6, 4
OUTPUT«(2 3 4 5 6)␤»

say sort -> $a, $b { $a cmp $b }, 5, 3, 2, 6, 4
OUTPUT«(2 3 4 5 6)␤»

sub foo($a, $b) { $a cmp $b }; say sort &foo, 5, 3, 2, 6, 4
OUTPUT«(2 3 4 5 6)␤»

The { $^a cmp $^b } form also creates a anonymous sub that has two
parameters, $^a and $^b.
The funny thing about these params is that their position in the
implicit signature is obtained by the
alphabetical order of the parameter names.

So, there are no $a and $b special variables, it is just the common way
of naming them.

Am 26.09.2015 um 18:00 schrieb Parrot Raiser:
> Because of the the special significance of $a and $b in Perl 5's sort
> comparison, I always avoid using the names in examples, lest it set a
> booby-trap for later.
>
> I've noticed "a" and "b' being used in some P6 examples. Are they no
> longer significant, or are they just a poor choice of identifier?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.