Perl 6 Perl 6 today #33

Cool Perl 6: Hash sort by value

Explanation: Start with the long form

sub value_cmp -> $a, $b { $a.value cmp $b.value };

%scores.sort( &value_cmp );

Each Pair in %scores is passed to &value_cmp

We can avoid the named sub

%scores.sort( -> $a, $b { $a.value cmp $b.value } );

Placeholder parameters allow inlining of param names

%scores.sort( { $^a.value cmp $^b.value } )
Copyright © 2009
http://www.pmichaud.com/2009/pres/