lib/FP/Combinators.pm

filter: Statementstmt 88.7% 103 / 116 Branchbran 41.6% 10 / 24 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.2% 45 / 51 Podpod 0.0% 0 / 8 totaltotal 77.6% 163 / 210 CC 19 SCAR 7.5
CC
19
Cov
80%
compose_1side
6.0
rot3right
6.0
__ANON__
6.0
CRAP 2.1
« lib/FP/Trampoline.pm ↑ Summary lib/FP/Carp.pm »
1 #
2 # Copyright (c) 2014-2021 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::Combinators - function combinators
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Ops 'div';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::Combinators 'flip';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 is div(2,3), 2/3;
20 is flip(\&div)->(2,3), 3/2;
21  
22 =head1 DESCRIPTION
23  
24 =over 4
25  
26 I<A combinator is a higher-order function that uses only function
27 application and earlier defined combinators to define a result from
28 its arguments.>
29  
30 L<Combinator (wikipedia.org)|https://en.wikipedia.org/wiki/Combinator>
31  
32 =back
33  
34 =head1 SEE ALSO
35  
36 L<FP::Optional>, L<FP::Combinators2>, L<FP::Predicates>, L<FP::Ops>
37  
38 =head1 NOTE
39  
40 This is alpha software! Read the status section in the package README
41 or on the L<website|http://functional-perl.org/>.
42  
43 =cut
44  
45 package FP::Combinators;
46 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 60 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 60 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 60 our @EXPORT = qw();
52 60 our @EXPORT_OK = qw(compose compose_scalar maybe_compose compose_1side
53 flip flip2of3 rot3right rot3left);
54 60 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
55  
56 60 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 60 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 2 ▶sub compose {
Subroutine compose: called CC=1 SCAR=0.0
Pod undocumented
60 2 my (@fn) = reverse @_;
61 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
62 2 my (@v) = @_;
63 2 for my $fn (@fn) {
64 5 @v = &$fn(@v);
65 }
66 2 ▶ wantarray ? @v : $v[-1] ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
67 }
68 2 }
69  
70 # same as compose, but request scalar context between the calls:
71  
72 2 ▶sub compose_scalar {
Subroutine compose_scalar: called CC=1 SCAR=0.0
Pod undocumented
73 2 my (@fn) = reverse @_;
74 2 my $f0 = pop @fn;
75 2 my $fx = shift @fn;
76 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
77 2 my $v = &$fx;
78 2 for my $fn (@fn) {
79 1 $v = &$fn($v);
80 }
81 2 @_ = ($v);
82 2 goto &$f0
83 }
84 2 }
85  
86 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 ▶ compose(sub { $_[0] + 1 }, sub { $_[0] + $_[1] })->(2, 3)
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
88 }
89 60 6;
90 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 1 ▶ compose_scalar(sub { $_[0] + 1 }, sub { $_[0] + $_[1] })->(2, 3)
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
92 }
93 60 6;
94  
95 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 1 ▶ compose(sub { $_[0] / ($_[1] // 5) }, sub {@_}, sub { $_[1], $_[0] })
Subroutine __ANON__: called CC=2 SCAR=7.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$_[1] // 5
l!l
10
MC/DC0%0/1
$_[1] // 5
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
97 1 ->(2, 3)
98 }
99 60 1.5;
100 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 1 ▶ compose_scalar(sub { $_[0] / ($_[1] // 5) }, sub {@_}, sub { $_[1], $_[0] })
Subroutine __ANON__: called CC=2 SCAR=7.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$_[1] // 5
l!l
01
MC/DC0%0/1
$_[1] // 5
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
102 1 ->(2, 3)
103 }
104 60 1 / 5;
105  
106 # a compose that short-cuts when there is no defined intermediate
107 # result:
108  
109 3 ▶sub maybe_compose {
Subroutine maybe_compose: called CC=1 SCAR=0.0
Pod undocumented
110 3 my (@fn) = reverse @_;
111 3 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
112 3 my (@v) = @_;
113 3 for (@fn) {
114  
115 # return undef, not (), for 'maybe_'; the latter would ask
116 # for convention 'perhaps_', ok?
117 7 ▶ return undef unless @v > 1 or defined $v[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @v > 1 or defined $v[0] 2 5 7
Condition66%2/3
@v > 1 or defined $v[0]
l!l&&r!l&&!r
502
MC/DC50%1/2
@v > 1 or defined $v[0]
@v > 1 defined $v[0]
Truth table66%2/3
A or B
A= @v > 1B= defined $v[0]
ABresult
1X1
011
000
118 5 @v = &$_(@v);
119 }
120 1 ▶ wantarray ? @v : $v[-1] ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
121 }
122 3 }
123  
124 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.2
125 0 ▶ maybe_compose(sub { die "foo @_" }, sub {undef}, sub {@_})->(2, 3)
Subroutine __ANON__: not called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
126 }
127 60 undef;
128 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.4
129 0 ▶ maybe_compose(sub { die "foo @_" }, sub {undef})->(2, 3)
Subroutine __ANON__: not called CC=1 SCAR=0.4
Subroutine __ANON__: called CC=1 SCAR=0.4
130 }
131 60 undef;
132 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 1 ▶ maybe_compose(sub { [@_] }, sub {@_})->(2, 3)
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
134 }
135 60 [2, 3];
136  
137 # a compose with 1 "side argument" (passed to subsequent invocations unmodified)
138 0 ▶sub compose_1side {
Subroutine compose_1side: not called CC=2 SCAR=17.9
Pod undocumented
139 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
140 0 my ($f, $g) = @_;
141 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 my ($a, $b) = @_;
143  
144 #XX TCO?
145 0 &$f(scalar &$g($a, $b), $b)
146 }
147 0 }
148  
149 60 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 # XX should flip work like the curried versions (e.g. in Haskell),
152 # i.e. not care about remaining arguments and simply flip the first
153 # two? That would save the need for flip2of3 etc., but it would also
154 # be less helpful for error-checking.
155  
156 1726 ▶sub flip {
Subroutine flip: called CC=2 SCAR=7.1
Pod undocumented
157 1726 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1726 0 1726
158 1726 my ($f) = @_;
159 1356 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
160 1356 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1356 0 1356
161 1356 @_ = ($_[1], $_[0]);
162 1356 goto &$f
163 }
164 1726 }
165  
166 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
167 1 ▶ flip(sub { $_[0] / $_[1] })->(2, 3)
Subroutine __ANON__: called CC=1 SCAR=0.0
168 }
169 60 3 / 2;
170  
171 # same as flip but pass a 3rd argument unchanged (flip 2 in 3)
172 246 ▶sub flip2of3 {
Subroutine flip2of3: called CC=2 SCAR=7.1
Pod undocumented
173 246 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 246 0 246
174 246 my ($f) = @_;
175 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
176 5 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 5 0 5
177 5 @_ = ($_[1], $_[0], $_[2]);
178 5 goto &$f
179 }
180 246 }
181  
182 0 ▶sub rot3right {
Subroutine rot3right: not called CC=2 SCAR=17.9
Pod undocumented
183 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
184 0 my ($f) = @_;
185 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
186 0 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 0 0 0
187 0 @_ = ($_[2], $_[0], $_[1]);
188 0 goto &$f
189 }
190 0 }
191  
192 395 ▶sub rot3left {
Subroutine rot3left: called CC=2 SCAR=7.1
Pod undocumented
193 395 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 395 0 395
194 395 my ($f) = @_;
195 388 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
196 388 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 388 0 388
197 388 @_ = ($_[1], $_[2], $_[0]);
198 388 goto &$f
199 }
200 395 }
201  
202 60 1
« lib/FP/Trampoline.pm ↑ Summary lib/FP/Carp.pm »