FP::Array_sort - 'sensible' sorting setup
use FP::Array_sort; # for `array_sort`, `on`, and `cmp_complement` use FP::Ops 'number_cmp'; use FP::Array ':all'; use FP::Equal 'is_equal'; is_equal array_sort([[10, 'a'], [15, 'b'], [-3, 'c']], on *array_first, *number_cmp), [[-3, 'c'], [10, 'a'], [15, 'b']];
Perl's sort is rather verbose and uses repetition of the accessor code:
sort { &$foo ($a) <=> &$foo ($b) } @$ary
Abstracting the repetition of the accessor as a function (`on`) and wrapping sort as a higher-order function makes it more straight-forward:
array_sort $ary, on ($foo, \&number_cmp)
In method interfaces the need becomes more obvious: if $ary is one of the FP sequences (FP::PureArray, FP::List, FP::StrictList, FP::Stream) that supports `sort` (TODO) then:
$s->sort (on $foo, \&number_cmp)
or if the comparison function already exists:
$numbers->sort (\&number_cmp)
FP::Ops, FP::Combinators
This is alpha software! Read the status section in the package README or on the website.
To install FunctionalPerl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm FunctionalPerl
CPAN shell
perl -MCPAN -e shell install FunctionalPerl
For more information on module installation, please visit the detailed CPAN module installation guide.