Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Comparing spaceships (cmp and <=> as options)

by periapt (Hermit)
on May 20, 2013 at 17:53 UTC ( #1034383=note: print w/ replies, xml ) Need Help??


in reply to Comparing spaceships (cmp and <=> as options)

Here is a version that uses a variant of the Schwartzian Transform.

my @records = ( [10, 'xyz232', 'secret project'], [ 5, 'foo123', 'world domination'], [ 7, 'bar666', 'have a beer'], ); my %sort_this_way = ( 'hours' => [0, sub { return sort { $a->[0][$a->[1]] <=> $b->[0][$b->[1]]; } @_; }], 'code' => [1, sub { return sort { $a->[0][$a->[1]] cmp $b->[0][$b->[1]]; } @_; }], 'name' => [2, sub { return sort { $a->[0][$a->[1]] cmp $b->[0][$b->[1]]; } @_; }], ); my $sort = 'hours'; # really comes from a switch my @new = map { $_->[0] } $sort_this_way{$sort}->[1]( map { [ $_ , $sort_this_way{$sort}->[0], $_->[ $sort_this_way{$sort}->[0] ] ] } @records );
Granted the sort part of the transform is actually a function call but I couldn't remember how to set up a function to not require parenthesis without defining a prototype first.


PJ
use strict; use warnings; use diagnostics;


Comment on Re: Comparing spaceships (cmp and <=> as options)
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1034383]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-08-30 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls