| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2013-2020 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | FP::Array_sort - 'sensible' sorting setup | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use FP::Array_sort; # for `array_sort` and `on` | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use FP::Ops 'real_cmp'; use FP::Array ':all'; use FP::Equal 'is_equal'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | is_equal array_sort([[10, 'a'], [15, 'b'], [-3, 'c']], | ||||||||||||||||||||||||||||
| 20 | on *array_first, \&real_cmp), | ||||||||||||||||||||||||||||
| 21 | [[-3, 'c'], [10, 'a'], [15, 'b']]; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Perl's sort is rather verbose and uses repetition of the accessor | ||||||||||||||||||||||||||||
| 27 | code: | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =for test ignore | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sort { &$foo ($a) <=> &$foo ($b) } @$ary | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Abstracting the repetition of the accessor as a function (`on`) and | ||||||||||||||||||||||||||||
| 34 | wrapping sort as a higher-order function makes it more | ||||||||||||||||||||||||||||
| 35 | straight-forward: | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | array_sort $ary, on ($foo, \&real_cmp) | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | In method interfaces the need becomes more obvious: if $ary is one of | ||||||||||||||||||||||||||||
| 40 | the FP sequences (FP::PureArray, FP::List, FP::StrictList, FP::Stream) | ||||||||||||||||||||||||||||
| 41 | that supports `sort` (TODO) then: | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | $s->sort (on $foo, \&real_cmp) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | or if the comparison function already exists: | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $numbers->sort (\&real_cmp) | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | L<FP::Ops>, L<FP::Combinators> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 NOTE | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||
| 56 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # XX Should `on` be moved to `FP::Combinators`? | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | package FP::Array_sort; | ||||||||||||||||||||||||||||
| 63 | 60 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 60 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 60 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 60 | ▶ | use Exporter "import"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 60 | our @EXPORT = qw(array_sort array_sortCompare on on_maybe); | |||||||||||||||||||||||||||
| 69 | 60 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 70 | 60 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 60 | ▶ | use FP::Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 60 | ▶ | use Chj::TEST; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 60 | ▶ | use FP::Ops qw(real_cmp); # for pod snippets, only, right? | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 13 | ▶ | sub array_sort { | ||||||||||||||||||||||||||
|
Subroutine
array_sort: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 13 | ▶ | @_ == 1 or @_ == 2 or fp_croak_arity "1 or 2"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 or @_ == 2
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1
@_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
| |||||||||||||||||||||||||||||
| 78 | 13 | my ($in, $maybe_cmp) = @_; | |||||||||||||||||||||||||||
| 79 | 13 | ▶ | if (defined $maybe_cmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 11 | [sort { &$maybe_cmp($a, $b) } @$in] | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | [ | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # To use the FP::Abstract::Compare protocol, use | ||||||||||||||||||||||||||||
| 85 | # array_sortCompare instead--we need to be backwards compatible | ||||||||||||||||||||||||||||
| 86 | # here. | ||||||||||||||||||||||||||||
| 87 | 2 | sort @$in | |||||||||||||||||||||||||||
| 88 | ] | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | sub array_sortCompare { | ||||||||||||||||||||||||||
|
Subroutine
array_sortCompare: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | my ($in) = @_; | |||||||||||||||||||||||||||
| 95 | 1 | [sort { $a->FP_Compare_compare($b) } @$in] | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 5 | ▶ | sub on { | ||||||||||||||||||||||||||
|
Subroutine
on: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 5 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 5 | my ($select, $cmp) = @_; | |||||||||||||||||||||||||||
| 101 | 14 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 102 | 14 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 14 | my ($a, $b) = @_; | |||||||||||||||||||||||||||
| 104 | 14 | &$cmp(&$select($a), &$select($b)) | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 5 | } | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub on_maybe { | ||||||||||||||||||||||||||
|
Subroutine
on_maybe: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($maybe_select, $cmp) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | defined $maybe_select ? on($maybe_select, $cmp) : $cmp | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 60 | 1 | |||||||||||||||||||||||||||