| 1 | # | ||||||||||
| 2 | # Copyright (c) 2013-2022 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 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | FP::Cmp - 3-way comparison helpers | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SEE ALSO | ||||||||||
| 20 | |||||||||||
| 21 | =head1 NOTE | ||||||||||
| 22 | |||||||||||
| 23 | This is alpha software! Read the status section in the package README | ||||||||||
| 24 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | package FP::Cmp; | ||||||||||
| 29 | 60 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 60 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 60 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 60 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | #use experimental 'signatures'; | ||||||||||
| 35 | 60 | ▶ | use Exporter "import"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 60 | our @EXPORT = qw(cmp_complement cmp_then); | |||||||||
| 38 | 60 | our @EXPORT_OK = qw(); | |||||||||
| 39 | 60 | our %EXPORT_TAGS = (default => \@EXPORT, all => [@EXPORT, @EXPORT_OK]); | |||||||||
| 40 | |||||||||||
| 41 | 60 | ▶ | use FP::Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 60 | ▶ | use Chj::TEST; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 60 | ▶ | use FP::Ops qw(binary_operator); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | # see also `complement` from FP::Predicates | ||||||||||
| 46 | 2 | ▶ | sub cmp_complement { | ||||||||
|
Subroutine
cmp_complement: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 47 | 2 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 2 | my ($cmp) = @_; | |||||||||
| 49 | 9 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 9 | -&$cmp(@_) | |||||||||
| 51 | } | ||||||||||
| 52 | 2 | } | |||||||||
| 53 | |||||||||||
| 54 | 1 | ▶ | TEST { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 1 | my $f = cmp_complement binary_operator "cmp"; | |||||||||
| 56 | [ | ||||||||||
| 57 | 1 | map { &$f(@$_) } ( | |||||||||
| 58 | [2, 4], | ||||||||||
| 59 | [4, 2], | ||||||||||
| 60 | [3, 3], | ||||||||||
| 61 | ["abc", "bbc"], | ||||||||||
| 62 | ["ab", "ab"], | ||||||||||
| 63 | ["bbc", "abc"] | ||||||||||
| 64 | ) | ||||||||||
| 65 | ] | ||||||||||
| 66 | } | ||||||||||
| 67 | 60 | [1, -1, 0, 1, 0, -1]; | |||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub cmp_then { | ||||||||
|
Subroutine
cmp_then: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 70 | |||||||||||
| 71 | # chain of cmp until one is non-0 | ||||||||||
| 72 | 0 | my @cmp = @_; | |||||||||
| 73 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 74 | 0 | my ($a, $b) = @_; | |||||||||
| 75 | 0 | for my $cmp (@cmp) { | |||||||||
| 76 | 0 | ▶ | if (my $res = $cmp->($a, $b)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | return $res | |||||||||
| 78 | } | ||||||||||
| 79 | } | ||||||||||
| 80 | 0 | ||||||||||
| 81 | 0 | } | |||||||||
| 82 | 0 | } | |||||||||
| 83 | |||||||||||
| 84 | 60 | 1 | |||||||||