lib/FP/Predicates.pm

filter: Statementstmt 81.7% 183 / 224 Branchbran 58.4% 76 / 130 Conditioncond 37.5% 45 / 120 MC/DCmcdc 29.0% 18 / 62 Subroutinesub 79.0% 68 / 86 Podpod 0.0% 0 / 45 totaltotal 58.4% 390 / 667 CC 111 SCAR 22.0
CC
111
Cov
64%
is_path_segment_string
56.0
is_filename
42.0
is_path_string
30.0
CRAP 9.0
« lib/FP/Repl.pm ↑ Summary lib/Chj/Unix/Signal.pm »
1 #
2 # Copyright (c) 2014-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 =head1 NAME
11  
12 FP::Predicates
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Predicates;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 is is_string("Hi"), 1;
19 is is_string(["Hi"]), 0;
20 1 ▶ use FP::List; use FP::Equal 'is_equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
21 is_equal list(1, 2, 3, 0, -1, "hi", [1])->map(\&is_natural0),
22 list(1, 1, 1, 1, 0, 0, 0);
23  
24 package Foo {
25 1 ▶ use FP::Predicates;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 *is_age = both \&is_natural0, sub { $_[0] < 130 };
28 # ^ if you do not want this to show up as a method,
29 # wrap it in BEGIN { } to get deleted in FP::Struct's
30 # namespace cleaning step; or assign to a scalar instead (my
31 # $is_age), of course; or use an inline expression (second
32 # line below)
33  
34 1 ▶ use FP::Struct [[\&is_string, "name"], [\&is_age, "age"]];
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # use FP::Struct [[\&is_string, "name"],
37 # [both (\&is_natural0, less_than 130), "age"]];
38  
39 _END_
40 }
41  
42 is (Foo->new("Moo", 13)->age, 13);
43  
44  
45 # Experimental:
46 {
47 1 ▶ use FP::Failure '*use_failure';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 local $use_failure = 1;
49  
50 my $isp = is_pure_class("FP::Array");
51 is $isp ? "yes" : "no", "no";
52 is $isp->message,
53 "failure: is_pure_class: 'FP::Array'\n";
54 is is_pure_class("FP:: Array")->message,
55 "failure: is_pure_class\n".
56 " because:\n".
57 " failure: is_valid_class_name: 'FP:: Array'\n";
58 }
59  
60 =head1 DESCRIPTION
61  
62 Useful as predicates for FP::Struct field definitions.
63  
64 These are simple functions expecting one value and returning a
65 boolean. They are composable with `maybe`, `complement`, `either`,
66 `all_of`/`both`.
67  
68 This is a functional approach to achieve the same aim as
69 `Moose::Util::TypeConstraints`, which basically uses a syntactical
70 sublanguage instead (implemented as a mix of functions and string
71 interpretation). It was written because it's way simpler. The drawback
72 is that (currently) there's no way to get a nice message string from
73 them to say why a match fails. Perhaps it would be possible to do so
74 using more introspection? (That would be nice because message
75 generation would be fully automatic and hence consistent.) Or,
76 alternatively, modifying the functions to compose messages themselves
77 when they fail (still mostly automatic), e.g. using message objects
78 that are false.
79  
80 =head1 NOTE
81  
82 This is alpha software! Read the status section in the package README
83 or on the L<website|http://functional-perl.org/>.
84  
85 =cut
86  
87 package FP::Predicates;
88 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 61 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 61 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 61 our @EXPORT = qw(
94 is_pure
95 is_pure_object
96 is_pure_class
97 is_string
98 is_path_string
99 is_path_segment_string
100 is_nonnumeric_string
101 is_nonnullstring
102 is_natural0
103 is_natural
104 is_even is_odd
105 is_boolean01
106 is_booleanyesno
107 is_boolean
108 is_hash
109 is_array
110 is_procedure
111 is_valid_class_name
112 instance_of
113 is_instance_of
114 is_subclass_of
115  
116 is_filehandle
117  
118 is_filename
119 is_sequence
120 sequence_of
121 is_proper_sequence
122 is_seq
123  
124 less_than
125 greater_than
126 less_equal
127 greater_equal
128 is_zero
129  
130 maybe
131 is_defined
132 is_true
133 true
134 is_false
135 false
136 complement
137 either
138 all_of both
139 );
140 61 our @EXPORT_OK = qw(
141 is_coderef
142 $package_re
143 );
144 61 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
145  
146 61 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 61 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 61 ▶use FP::Abstract::Pure;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 61 ▶use Chj::BuiltinTypePredicates 'is_filehandle';
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 # ^ should probably move more lowlevel predicates there
152 61 ▶use Scalar::Util qw(looks_like_number blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 # Only use `FP::Failure` features if $FP::Failure::use_failure is
155 # true--which means that FP::Failure should be loaded, no need to
156 # require it, OK? (Not strictly true; some module could set
157 # $FP::Failure::use_failure withouth loading the latter; but really
158 # that should be considered a bug and is why I do like/prefer the
159 # approach of importing *use_failure, then FP::Failure is loaded
160 # implicitly.)
161  
162 11 ▶sub failwith {
Subroutine failwith: called CC=2 SCAR=6.9
Pod undocumented
163 11 my ($parents, $msg, @vals) = @_;
164 11 ▶ $FP::Failure::use_failure
Branch: 1 branch
BranchTrueFalseTotal
$FP::Failure::use_failure ? : 1 10 11
165 ? FP::Failure::failure(FP::Failure::message($msg, @vals), $parents)
166 : 0
167 }
168  
169 41001 ▶sub fail {
Subroutine fail: called CC=2 SCAR=6.9
Pod undocumented
170 41001 my ($msg, @vals) = @_;
171 41001 ▶ $FP::Failure::use_failure
Branch: 1 branch
BranchTrueFalseTotal
$FP::Failure::use_failure ? : 2 40999 41001
172 ? FP::Failure::failure(FP::Failure::message($msg, @vals))
173 : 0
174 }
175  
176 # XX check for read-only flags?
177  
178 # is_pure returns true for non-references, going with the assumption
179 # that the caller created a copy of those anyway, in which case there
180 # is no reason for fear from mutations from scopes before it got
181 # control of the value:
182 12 ▶sub is_pure {
Subroutine is_pure: called CC=4 SCAR=13.9
Pod undocumented
183 12 my ($v) = @_;
184 12 ▶ blessed($v) // return ((length ref $v) ? '' : 1);
Branch: 1 branch
BranchTrueFalseTotal
length ref $v ? : 1 2 3
Condition100%2/2
builtin::blessed($v) // (return length ref $v ? "" : 1)
l!l
93
MC/DC100%1/1
builtin::blessed($v) // (return length ref $v ? "" : 1)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
185 9 ▶ $v->isa("FP::Abstract::Pure") or fail "is_pure", $v
Branch: 1 branch
BranchTrueFalseTotal
$v->isa("FP::Abstract::Pure") or fail("is_pure", $v) 8 1 9
186 }
187  
188 2 ▶sub is_pure_object {
Subroutine is_pure_object: called CC=3 SCAR=11.1
Pod undocumented
189 2 my ($v) = @_;
190 2 ▶ blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
11
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
191 1 ▶ $v->isa("FP::Abstract::Pure") or fail "is_pure_object", $v
Branch: 1 branch
BranchTrueFalseTotal
$v->isa("FP::Abstract::Pure") or fail("is_pure_object", $v) 1 0 1
192 }
193  
194 2 ▶sub is_pure_class {
Subroutine is_pure_class: called CC=3 SCAR=11.1
Pod undocumented
195 2 my $r = is_valid_class_name($_[0]);
196 2 ▶ $r or return failwith [$r], "is_pure_class";
Branch: 1 branch
BranchTrueFalseTotal
$r or return failwith([$r], "is_pure_class") 1 1 2
197 1 ▶ $_[0]->isa("FP::Abstract::Pure") or fail "is_pure_class", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->isa("FP::Abstract::Pure") or fail("is_pure_class", $_[0]) 0 1 1
198 }
199  
200 233 ▶sub is_string {
Subroutine is_string: called CC=3 SCAR=11.1
Pod undocumented
201 233 my ($v) = @_;
202 233 ▶ (defined $v and not ref $v) # relax?
Branch: 1 branch
BranchTrueFalseTotal
defined $v and not ref $v or fail("is_string", $v) 232 1 233
Condition66%2/3
defined $v and not ref $v
!ll&&!rl&&r
01232
MC/DC50%1/2
defined $v and not ref $v
defined $v not ref $v
Truth table66%2/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
203 or fail "is_string", $v
204 }
205  
206 0 ▶sub is_path_string {
Subroutine is_path_string: not called CC=5 SCAR=34.0
Pod undocumented
207 0 my ($v) = @_;
208 (
209 0 ▶ defined $v and not ref $v # relax?
Branch: 1 branch
BranchTrueFalseTotal
defined $v and not ref $v and length $v and not $v =~ /\0/ or fail("is_path_string", $v) 0 0 0
Condition0%0/9
defined $v and not ref $v and length $v and not $v =~ /\0/
!ll&&!rl&&r
000
defined $v and not ref $v and length $v
!ll&&!rl&&r
000
defined $v and not ref $v
!ll&&!rl&&r
000
MC/DC0%0/4
defined $v and not ref $v and length $v and not $v =~ /\0/
defined $v not ref $v length $v not $v =~ /\0/
Truth table0%0/5
A and not(B) and C and not(D)
A= defined $vB= not ref $vC= length $vD= not $v =~ /\0/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
210 and length $v and not $v =~ /\0/
211 )
212 or fail "is_path_string", $v
213 }
214  
215 0 ▶sub is_path_segment_string {
Subroutine is_path_segment_string: not called CC=7 SCAR=40.3
Pod undocumented
216 0 my ($v) = @_;
217 (
218 0 ▶ defined $v and not ref $v # relax?
Branch: 1 branch
BranchTrueFalseTotal
defined $v and not ref $v and length $v and not $v =~ m[[\0/]] and not $v eq ".." and not $v eq "." or fail("is_path_segment_string", $v) 0 0 0
Condition0%0/15
defined $v and not ref $v and length $v and not $v =~ m[[\0/]] and not $v eq ".." and not $v eq "."
!ll&&!rl&&r
000
defined $v and not ref $v and length $v and not $v =~ m[[\0/]] and not $v eq ".."
!ll&&!rl&&r
000
defined $v and not ref $v and length $v and not $v =~ m[[\0/]]
!ll&&!rl&&r
000
defined $v and not ref $v and length $v
!ll&&!rl&&r
000
defined $v and not ref $v
!ll&&!rl&&r
000
MC/DC0%0/6
defined $v and not ref $v and length $v and not $v =~ m[[\0/]] and not $v eq ".." and not $v eq "."
defined $v not ref $v length $v not $v =~ m[[\0/]] not $v eq ".." not $v eq "."
Truth table0%0/7
A and not(B) and C and not(D) and not(E) and not(F)
A= defined $vB= not ref $vC= length $vD= not $v =~ m[[\0/]]E= not $v eq ".."F= not $v eq "."
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
219 and length $v
220 and not $v =~ m{[\0/]}
221 and not $v eq ".."
222 and not $v eq "."
223 )
224 or fail "is_path_segment_string", $v
225 }
226  
227 0 ▶sub is_nonnumeric_string {
Subroutine is_nonnumeric_string: not called CC=4 SCAR=30.0
Pod undocumented
228 0 my ($v) = @_;
229 (
230 0 ▶ defined $v and not ref $v # relax?
Branch: 1 branch
BranchTrueFalseTotal
defined $v and not ref $v and not &looks_like_number($v) or fail("is_string", $v) 0 0 0
Condition0%0/6
defined $v and not ref $v and not &looks_like_number($v)
!ll&&!rl&&r
000
defined $v and not ref $v
!ll&&!rl&&r
000
MC/DC0%0/3
defined $v and not ref $v and not &looks_like_number($v)
defined $v not ref $v not &looks_like_number($v)
Truth table0%0/4
A and not(B) and not(C)
A= defined $vB= not ref $vC= not &looks_like_number($v)
ABCresult
0XX0
10X0
1100
1111
231 and not looks_like_number($v)
232 )
233 or fail "is_string", $v
234 }
235  
236 21 ▶sub is_nonnullstring {
Subroutine is_nonnullstring: called CC=4 SCAR=17.9
Pod undocumented
237 21 my ($v) = @_;
238 (
239 21 ▶ defined $v and not ref $v # relax?
Branch: 1 branch
BranchTrueFalseTotal
defined $v and not ref $v and length $v or fail("is_nonnullstring", $v) 21 0 21
Condition33%2/6
defined $v and not ref $v and length $v
!ll&&!rl&&r
0021
defined $v and not ref $v
!ll&&!rl&&r
0021
MC/DC0%0/3
defined $v and not ref $v and length $v
defined $v not ref $v length $v
Truth table25%1/4
A and not(B) and C
A= defined $vB= not ref $vC= length $v
ABCresult
0XX0
10X0
1100
1111
240 and length $v
241 )
242 or fail "is_nonnullstring", $v
243 }
244  
245 89 ▶sub is_natural0 {
Subroutine is_natural0: called CC=4 SCAR=13.9
Pod undocumented
246 89 my ($v) = @_;
247 (
248 89 ▶ defined $v and not ref $v # relax?
Branch: 1 branch
BranchTrueFalseTotal
defined $v and not ref $v and $v =~ /^\d+\z/ or fail("is_natural0", $v) 80 9 89
Condition83%5/6
defined $v and not ref $v and $v =~ /^\d+\z/
!ll&&!rl&&r
1880
defined $v and not ref $v
!ll&&!rl&&r
0188
MC/DC66%2/3
defined $v and not ref $v and $v =~ /^\d+\z/
defined $v not ref $v $v =~ /^\d+\z/
Truth table75%3/4
A and not(B) and C
A= defined $vB= not ref $vC= $v =~ /^\d+\z/
ABCresult
0XX0
10X0
1100
1111
249 and $v =~ /^\d+\z/
250 )
251 or fail "is_natural0", $v
252 }
253  
254 54 ▶sub is_natural {
Subroutine is_natural: called CC=5 SCAR=16.7
Pod undocumented
255 54 my ($v) = @_;
256 (
257 54 ▶ defined $v and not ref $v # relax?
Branch: 1 branch
BranchTrueFalseTotal
defined $v and not ref $v and $v =~ /^\d+\z/ and $v or fail("is_natural", $v) 44 10 54
Condition66%6/9
defined $v and not ref $v and $v =~ /^\d+\z/ and $v
!ll&&!rl&&r
8244
defined $v and not ref $v and $v =~ /^\d+\z/
!ll&&!rl&&r
0846
defined $v and not ref $v
!ll&&!rl&&r
0054
MC/DC50%2/4
defined $v and not ref $v and $v =~ /^\d+\z/ and $v
defined $v not ref $v $v =~ /^\d+\z/ $v
Truth table60%3/5
A and not(B) and C and D
A= defined $vB= not ref $vC= $v =~ /^\d+\z/D= $v
ABCDresult
0XXX0
10XX0
110X0
11100
11111
258 and $v =~ /^\d+\z/ and $v
259 )
260 or fail "is_natural", $v
261 }
262  
263 # XX careful these do not check for number types first
264  
265 64 ▶sub is_even {
Subroutine is_even: called CC=2 SCAR=6.9
Pod undocumented
266 64 ▶ ($_[0] & 1) == 0 or fail "is_even", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
($_[0] & 1) == 0 or fail("is_even", $_[0]) 20 44 64
267 }
268  
269 7 ▶sub is_odd {
Subroutine is_odd: called CC=2 SCAR=6.9
Pod undocumented
270 7 ▶ ($_[0] & 1) or fail "is_odd", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
$_[0] & 1 or fail("is_odd", $_[0]) 4 3 7
271 }
272  
273 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
274 1 [map { is_even $_ } -3 .. 3]
275 }
276 61 [0, 1, 0, 1, 0, 1, 0];
277 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
278 1 [map { is_odd $_ } -3 .. 3]
279 }
280 61 [1, 0, 1, 0, 1, 0, 1];
281 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
282 1 [map { is_even $_ } 3, 3.1, 4, 4.1, -4.1]
283 }
284  
285 # XX what should it give?
286 61 [0, 0, 1, 1, 1];
287  
288 # no `is_` prefix as those are not the final predicates (they are
289 # curried forms of < and > etc.):
290  
291 # names? (number versus string comparison) (wish Perl hat generics
292 # for those instead..)
293  
294 1 ▶sub less_than {
Subroutine less_than: called CC=1 SCAR=0.0
Pod undocumented
295 1 my ($x) = @_;
296  
297 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
298 2 ▶ $_[0] < $x or fail "less_than", $x, $_[0] # last value last, ok?
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $x or fail("less_than", $x, $_[0]) 1 1 2
299 }
300 1 }
301  
302 0 ▶sub greater_than {
Subroutine greater_than: not called CC=1 SCAR=6.9
Pod undocumented
303 0 my ($x) = @_;
304  
305 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
306 0 ▶ $_[0] > $x or fail "greater_than", $x, $_[0] # last value last, ok?
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $x or fail("greater_than", $x, $_[0]) 0 0 0
307 }
308 0 }
309  
310 0 ▶sub less_equal {
Subroutine less_equal: not called CC=1 SCAR=6.9
Pod undocumented
311 0 my ($x) = @_;
312  
313 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
314 0 ▶ $_[0] <= $x or fail "less_equal", $x, $_[0] # last value last, ok?
Branch: 1 branch
BranchTrueFalseTotal
$_[0] <= $x or fail("less_equal", $x, $_[0]) 0 0 0
315 }
316 0 }
317  
318 0 ▶sub greater_equal {
Subroutine greater_equal: not called CC=1 SCAR=6.9
Pod undocumented
319 0 my ($x) = @_;
320  
321 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
322 0 ▶ $_[0] >= $x or fail "greater_equal", $x, $_[0] # last value last, ok?
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= $x or fail("greater_equal", $x, $_[0]) 0 0 0
323 }
324 0 }
325  
326 24 ▶sub is_zero {
Subroutine is_zero: called CC=2 SCAR=6.9
Pod undocumented
327 24 ▶ $_[0] == 0 or fail "is_zero", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == 0 or fail("is_zero", $_[0]) 4 20 24
328 }
329  
330 # strictly 0 or 1
331 0 ▶sub is_boolean01 {
Subroutine is_boolean01: not called CC=3 SCAR=24.8
Pod undocumented
332 (
333 0 ▶ not ref($_[0]) # relax?
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[0] and $_[0] =~ /^[01]\z/ or fail("is_boolean01", $_[0]) 0 0 0
Condition0%0/3
not ref $_[0] and $_[0] =~ /^[01]\z/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_[0] and $_[0] =~ /^[01]\z/
not ref $_[0] $_[0] =~ /^[01]\z/
Truth table0%0/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[01]\z/
ABresult
0X0
100
111
334 and $_[0] =~ /^[01]\z/
335 )
336 or fail "is_boolean01", $_[0]
337 }
338  
339 0 ▶sub is_booleanyesno {
Subroutine is_booleanyesno: not called CC=4 SCAR=30.0
Pod undocumented
340 0 my ($v) = @_;
341 0 ▶ (not ref $v and $v eq "yes" or $v eq "no") or fail "is_booleanyesno", $v
Branch: 1 branch
BranchTrueFalseTotal
not ref $v and $v eq "yes" or $v eq "no" or fail("is_booleanyesno", $v) 0 0 0
Condition0%0/6
not ref $v and $v eq "yes" or $v eq "no"
l!l&&r!l&&!r
000
not ref $v and $v eq "yes"
!ll&&!rl&&r
000
MC/DC0%0/3
not ref $v and $v eq "yes" or $v eq "no"
not ref $v $v eq "yes" $v eq "no"
Truth table0%0/5
not(A) and B or C
A= not ref $vB= $v eq "yes"C= $v eq "no"
ABCresult
11X1
0X11
1011
0X00
1000
342 }
343  
344 # undef, 0, "", or 1
345 6 ▶sub is_boolean {
Subroutine is_boolean: called CC=4 SCAR=13.9
Pod undocumented
346 (
347 6 ▶ not ref($_[0]) # relax?
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[0] and !$_[0] || $_[0] eq 1 or fail("is_boolean", $_[0]) 4 2 6
Condition83%5/6
not ref $_[0] and !$_[0] || $_[0] eq 1
!ll&&!rl&&r
024
not $_[0] or $_[0] eq 1
l!l&&r!l&&!r
132
MC/DC66%2/3
not ref $_[0] and !$_[0] || $_[0] eq 1
not ref $_[0] not $_[0] $_[0] eq 1
Truth table75%3/4
not(A) and not(B) or C
A= not ref $_[0]B= not $_[0]C= $_[0] eq 1
ABCresult
0XX0
1000
11X1
1011
348 and (!$_[0] or $_[0] eq "1")
349 )
350 or fail "is_boolean", $_[0]
351 }
352  
353 3 ▶sub is_hash {
Subroutine is_hash: called CC=3 SCAR=14.2
Pod undocumented
354 3 ▶ (defined $_[0] and ref($_[0]) eq "HASH") or fail "is_hash", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and ref $_[0] eq "HASH" or fail("is_hash", $_[0]) 3 0 3
Condition33%1/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
355 }
356  
357 13 ▶sub is_array {
Subroutine is_array: called CC=3 SCAR=11.1
Pod undocumented
358 13 ▶ (defined $_[0] and ref($_[0]) eq "ARRAY") or fail "is_array", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and ref $_[0] eq "ARRAY" or fail("is_array", $_[0]) 12 1 13
Condition66%2/3
defined $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
0112
MC/DC50%1/2
defined $_[0] and ref $_[0] eq "ARRAY"
defined $_[0] ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= defined $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
359 }
360  
361 # Usually you should prefer `is_procedure` (see below) over this, as
362 # we like to pass globs as subroutine place holders, too.
363  
364 0 ▶sub is_coderef {
Subroutine is_coderef: not called CC=3 SCAR=24.8
Pod undocumented
365 0 ▶ (defined $_[0] and ref($_[0]) eq "CODE") or fail "is_coderef", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and ref $_[0] eq "CODE" or fail("is_coderef", $_[0]) 0 0 0
Condition0%0/3
defined $_[0] and ref $_[0] eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "CODE"
defined $_[0] ref $_[0] eq "CODE"
Truth table0%0/3
A and B
A= defined $_[0]B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
366 }
367  
368 # Should this be called `is_subroutine` or `is_sub` instead, to cater
369 # for the traditional naming in Perl? But then Perl itself is
370 # inconsistent, too, calling those code refs, which matches the
371 # is_coderef naming above.
372  
373 54 ▶sub is_procedure {
Subroutine is_procedure: called CC=6 SCAR=17.9
Pod undocumented
374 (
375 defined $_[0]
376 and (ref($_[0]) eq "CODE"
377 54 ▶ or (ref \($_[0]) eq "GLOB" ? *{ $_[0] }{CODE} ? 1 : '' : ''))
Branch: 3 branches
BranchTrueFalseTotal
defined $_[0] and ref $_[0] eq "CODE" || (ref \$_[0] eq "GLOB" ? *{$_[0];}{"CODE"} ? 1 : "" : "") or fail("is_procedure", $_[0]) 52 2 54
ref \$_[0] eq 'GLOB' ? : 2 1 3
*{$_[0];}{'CODE'} ? : 1 1 2
Condition83%5/6
defined $_[0] and ref $_[0] eq "CODE" || (ref \$_[0] eq "GLOB" ? *{$_[0];}{"CODE"} ? 1 : "" : "")
!ll&&!rl&&r
0252
ref $_[0] eq "CODE" or ref \$_[0] eq "GLOB" ? *{$_[0];}{"CODE"} ? 1 : "" : ""
l!l&&r!l&&!r
5112
MC/DC66%2/3
defined $_[0] and ref $_[0] eq "CODE" || (ref \$_[0] eq "GLOB" ? *{$_[0];}{"CODE"} ? 1 : "" : "")
defined $_[0] ref $_[0] eq "CODE" ref \$_[0] eq "GLOB" ? *{$_[0];}{"CODE"} ? 1 : "" : ""
Truth table75%3/4
A and B or C
A= defined $_[0]B= ref $_[0] eq "CODE"C= ref \$_[0] eq "GLOB" ? *{$_[0];}{"CODE"} ? 1 : "" : ""
ABCresult
0XX0
1000
11X1
1011
378 )
379  
380 # XX: also check for objects that overload '&'?
381 or fail "is_procedure", $_[0]
382 }
383  
384 1 ▶ TEST { is_procedure [] } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
385 1 ▶TEST { is_procedure \&is_procedure } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
386 1 ▶TEST { is_procedure *is_procedure } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
387 1 ▶TEST { is_procedure *fifu } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
388  
389 61 my $classpart_re = qr/[a-zA-Z_]\w*/;
390  
391 61 our $package_re = qr/(?:${classpart_re}::)*$classpart_re/;
392  
393 45 ▶sub is_valid_class_name {
Subroutine is_valid_class_name: called CC=3 SCAR=11.1
Pod undocumented
394 45 my ($v) = @_;
395 45 ▶ !length ref($v) and $v =~ /^$package_re\z/
Branch: 1 branch
BranchTrueFalseTotal
not length ref $v and $v =~ /^$FP::Predicates::package_re\z/ or fail("is_valid_class_name", $v) 40 5 45
Condition66%2/3
not length ref $v and $v =~ /^$FP::Predicates::package_re\z/
!ll&&!rl&&r
0540
MC/DC50%1/2
not length ref $v and $v =~ /^$FP::Predicates::package_re\z/
not length ref $v $v =~ /^$FP::Predicates::package_re\z/
Truth table66%2/3
not(A) and B
A= not length ref $vB= $v =~ /^$FP::Predicates::package_re\z/
ABresult
0X0
100
111
396 or fail "is_valid_class_name", $v
397 }
398  
399 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
400 [
401 1 map { is_valid_class_name $_ } "foo ",
402 qw(foo foo_bar Foo::Bar Foo:Bar Foo123 123 Foo::123)
403 ]
404 }
405 61 [0, 1, 1, 1, 0, 1, 0, 0];
406  
407 35 ▶sub instance_of {
Subroutine instance_of: called CC=2 SCAR=7.0
Pod undocumented
408 35 my ($class) = @_;
409 35 ▶ is_valid_class_name $class or die "need class name string, got: $class";
Branch: 1 branch
BranchTrueFalseTotal
is_valid_class_name($class) or die "need class name string, got: $class" 35 0 35
410  
411 410 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
412 410 ▶ ((defined blessed $_[0]) ? $_[0]->isa($class) : '')
Branch: 2 branches
BranchTrueFalseTotal
defined builtin::blessed($_[0]) ? $_[0]->isa($class) : "" or fail("instance_of", $class, $_[0]) 289 121 410
defined builtin::blessed($_[0]) ? : 381 29 410
413 or fail "instance_of", $class, $_[0]
414 }
415 35 }
416  
417 16 ▶sub is_instance_of {
Subroutine is_instance_of: called CC=3 SCAR=11.0
Pod undocumented
418 16 my ($v, $class) = @_;
419  
420 # is_valid_class_name $class or die "need class name string, got: $class";
421 16 ▶ ((defined blessed $v) ? $v->isa($class) : '')
Branch: 2 branches
BranchTrueFalseTotal
defined builtin::blessed($v) ? $v->isa($class) : "" or fail("is_instance_of", $v, $class) 15 1 16
defined builtin::blessed($v) ? : 15 1 16
422 or fail "is_instance_of", $v, $class
423 }
424  
425 2 ▶sub is_subclass_of {
Subroutine is_subclass_of: called CC=3 SCAR=13.1
Pod undocumented
426 2 my ($v, $class) = @_;
427  
428 # is_valid_class_name $class or die "need class name string, got: $class";
429 2 ▶ (!length ref $v and $v->isa($class)) or fail "is_subclass_of", $v, $class
Branch: 1 branch
BranchTrueFalseTotal
not length ref $v and $v->isa($class) or fail("is_subclass_of", $v, $class) 2 0 2
Condition33%1/3
not length ref $v and $v->isa($class)
!ll&&!rl&&r
002
MC/DC0%0/2
not length ref $v and $v->isa($class)
not length ref $v $v->isa($class)
Truth table33%1/3
not(A) and B
A= not length ref $vB= $v->isa($class)
ABresult
0X0
100
111
430 }
431  
432 1 ▶TEST { my $v = "IO"; is_instance_of $v, "IO" } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
433 1 ▶TEST { my $v = bless [], "IO"; is_instance_of $v, "IO" } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
434 1 ▶TEST { my $v = "IO"; is_subclass_of $v, "IO" } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
435 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
436 1 require Chj::IO::File;
437 1 is_subclass_of "Chj::IO::File", "IO"
438 }
439 61 1;
440  
441 # is_filename in Chj::BuiltinTypePredicates
442  
443 1 ▶TEST {
Subroutine __ANON__: called CC=2 SCAR=7.0
444 [
445 10 map { is_filehandle $_ } "STDOUT",
446 undef, *STDOUT,
447 *STDOUT{IO},
448 \*STDOUT,
449 *SMK69GXDB,
450 *SMK69GXDB{IO},
451 \*SMK69GXDB,
452 bless(\*WOFWEOXVV, "ReallyNotIO"),
453 1 do {
454 1 ▶ open my $in, '<', __FILE__ or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", __FILE__ or die $! 1 0 1
455  
456 #warn "HM".<$in>; # works
457 1 bless $in, "MightActullyBeIO"
458 }
459 ]
460 }
461 61 ['', '', '', 1, 1, '', '', '', '', 1];
462  
463 # should probably be in a filesystem lib instead?
464 0 ▶sub is_filename {
Subroutine is_filename: not called CC=6 SCAR=37.4
Pod undocumented
465 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
466 0 my ($v) = @_;
467 0 ▶ (is_nonnullstring($v) and !($v =~ m|/|) and !($v eq ".") and !($v eq ".."))
Branch: 1 branch
BranchTrueFalseTotal
is_nonnullstring($v) and not $v =~ m[/] and not $v eq "." and not $v eq ".." or fail("is_filename", $v) 0 0 0
Condition0%0/9
is_nonnullstring($v) and not $v =~ m[/] and not $v eq "." and not $v eq ".."
!ll&&!rl&&r
000
is_nonnullstring($v) and not $v =~ m[/] and not $v eq "."
!ll&&!rl&&r
000
is_nonnullstring($v) and not $v =~ m[/]
!ll&&!rl&&r
000
MC/DC0%0/4
is_nonnullstring($v) and not $v =~ m[/] and not $v eq "." and not $v eq ".."
is_nonnullstring($v) not $v =~ m[/] not $v eq "." not $v eq ".."
Truth table0%0/5
A and not(B) and not(C) and not(D)
A= is_nonnullstring($v)B= not $v =~ m[/]C= not $v eq "."D= not $v eq ".."
ABCDresult
0XXX0
10XX0
110X0
11100
11111
468 or fail "is_filename", $v
469 }
470  
471 # can't be in `FP::Abstract::Sequence` since that package is for OO, well, what
472 # to do about it?
473 61 ▶use FP::Lazy; # sigh dependency, too.
Subroutine BEGIN: called CC=1 SCAR=0.0
474  
475 23 ▶sub is_sequence {
Subroutine is_sequence: called CC=4 SCAR=14.2
Pod undocumented
476 23 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 23 0 23
477 23 my $v = force $_[0];
478 23 ▶ blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
158
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
479 15 ▶ $v->isa("FP::Abstract::Sequence") or fail "is_sequence", $v
Branch: 1 branch
BranchTrueFalseTotal
$v->isa("FP::Abstract::Sequence") or fail("is_sequence", $v) 15 0 15
480 }
481  
482 29 ▶sub is_proper_sequence {
Subroutine is_proper_sequence: called CC=5 SCAR=16.3
Pod undocumented
483 29 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 29 0 29
484 29 my $v = force $_[0];
485 29 ▶ blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
1910
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
486 19 ▶ ($v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence)
Branch: 1 branch
BranchTrueFalseTotal
$v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence or fail("is_sequence", $v) 16 3 19
Condition66%2/3
$v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence
!ll&&!rl&&r
0316
MC/DC50%1/2
$v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence
$v->isa("FP::Abstract::Sequence") $v->is_proper_sequence
Truth table66%2/3
A and B
A= $v->isa("FP::Abstract::Sequence")B= $v->is_proper_sequence
ABresult
0X0
100
111
487 or fail "is_sequence", $v
488 }
489  
490 0 ▶sub sequence_of {
Subroutine sequence_of: not called CC=2 SCAR=17.9
Pod undocumented
491 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
492 0 my ($pred) = @_;
493 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
494 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
495 0 my $v = force $_[0];
496 0 ▶ blessed($v) // return;
Condition0%0/2
builtin::blessed($v) // (return)
l!l
00
MC/DC0%0/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table0%0/2
A
A= builtin::blessed($v)
Aresult
11
00
497  
498 # Since `list_every` dies on improper lists, we have to check
499 # for is_proper_sequence here, too:
500 0 ▶ ($v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence)
Branch: 1 branch
BranchTrueFalseTotal
$v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence or return fail("sequence_of", $v) 0 0 0
Condition0%0/3
$v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence
!ll&&!rl&&r
000
MC/DC0%0/2
$v->isa("FP::Abstract::Sequence") and $v->is_proper_sequence
$v->isa("FP::Abstract::Sequence") $v->is_proper_sequence
Truth table0%0/3
A and B
A= $v->isa("FP::Abstract::Sequence")B= $v->is_proper_sequence
ABresult
0X0
100
111
501 or return fail "sequence_of", $v;
502 0 $v->every($pred) # should return a fail itself, so, OK?
503 }
504 0 }
505  
506 # Like is_sequence but only returns true when the sequence isn't empty
507 # (similar to Clojure's `(seq? (seq v))`)
508 14 ▶sub is_seq {
Subroutine is_seq: called CC=5 SCAR=16.3
Pod undocumented
509 14 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 14 0 14
510 14 my $v = force $_[0];
511 14 ▶ blessed($v) // return;
Condition100%2/2
builtin::blessed($v) // (return)
l!l
95
MC/DC100%1/1
builtin::blessed($v) // (return)
builtin::blessed($v)
Truth table100%2/2
A
A= builtin::blessed($v)
Aresult
11
00
512 9 ▶ ($v->isa("FP::Abstract::Sequence") && (not $v->is_null))
Branch: 1 branch
BranchTrueFalseTotal
$v->isa("FP::Abstract::Sequence") and not $v->is_null or fail("is_sequence", $v) 5 4 9
Condition66%2/3
$v->isa("FP::Abstract::Sequence") and not $v->is_null
!ll&&!rl&&r
045
MC/DC50%1/2
$v->isa("FP::Abstract::Sequence") and not $v->is_null
$v->isa("FP::Abstract::Sequence") not $v->is_null
Truth table66%2/3
A and not(B)
A= $v->isa("FP::Abstract::Sequence")B= not $v->is_null
ABresult
0X0
100
111
513 or fail "is_sequence", $v
514 }
515  
516 15 ▶sub maybe {
Subroutine maybe: called CC=2 SCAR=6.9
Pod undocumented
517 15 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 15 0 15
518 15 my ($pred) = @_;
519  
520 99 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
521 99 my ($v) = @_;
522 defined $v
523 99 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 36 63 99
524 36 my $b = &$pred($v);
525 36 ▶ $b or failwith [$b], "maybe"
Branch: 1 branch
BranchTrueFalseTotal
$b or failwith([$b], "maybe") 35 1 36
526 }
527 : 1
528 }
529 15 }
530  
531 # (this would also be a candidate for FP::Ops)
532 43191 ▶sub is_defined {
Subroutine is_defined: called CC=3 SCAR=11.1
Pod undocumented
533 43191 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 43191 0 43191
534 43191 ▶ defined $_[0] or fail "is_defined", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] or fail("is_defined", $_[0]) 2424 40767 43191
535 }
536  
537 0 ▶sub is_true {
Subroutine is_true: not called CC=3 SCAR=24.8
Pod undocumented
538 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
539 0 ▶ $_[0] or fail "is_true", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
$_[0] or fail("is_true", $_[0]) 0 0 0
540 }
541  
542 # (this would also be a candidate as 'not' with a different name for
543 # FP::Ops)
544 0 ▶sub is_false {
Subroutine is_false: not called CC=3 SCAR=24.8
Pod undocumented
545 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
546 0 ▶ !$_[0] or fail "is_false", $_[0]
Branch: 1 branch
BranchTrueFalseTotal
not $_[0] or fail("is_false", $_[0]) 0 0 0
547 }
548  
549 0 ▶sub true {
Subroutine true: not called CC=1 SCAR=6.9
Pod undocumented
550 0 1
551 }
552  
553 1 ▶sub false {
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
554 1 0
555 }
556  
557 7 ▶sub complement {
Subroutine complement: called CC=2 SCAR=7.0
Pod undocumented
558 7 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 7 0 7
559 7 my ($f) = @_;
560 20 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
561 20 my $r = &$f(@_);
562 20 ▶ !$r
Branch: 1 branch
BranchTrueFalseTotal
not $r or failwith([fail("not")], "complement") 15 5 20
563  
564 # XX: in a perfect world we would have information about
565 # why $f *succeeded* here. Sigh. We don't. TODO?
566 or failwith [fail "not"], "complement"
567 }
568 7 }
569  
570 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
571 1 my $t = complement(\&is_natural);
572 1 [map { &$t($_) } (-1, 0, 1, 2, "foo")]
573 }
574 61 [1, 1, 0, 0, 1];
575  
576 66 ▶sub either {
Subroutine either: called CC=1 SCAR=1.0
Pod undocumented
577 66 my (@fn) = @_;
578 91 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=24.3
579 # Meh, code it up all twice. Macros anyone?
580 91 ▶ if ($FP::Failure::use_failure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FP::Failure::use_failure) { } 0 91 91
581 0 my @failures;
582 0 for my $fn (@fn) {
583 0 my $r = &$fn;
584 0 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
585 0 push @failures, $r
586 }
587 0 failwith \@failures, "either"
588 } else {
589 91 for my $fn (@fn) {
590 142 my $r = &$fn;
591 142 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 82 60 142
592 }
593 0
594 9 }
595 }
596 66 }
597  
598 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
599 1 my $t = either \&is_natural, \&is_boolean;
600 1 [map { &$t($_) } (-1, 0, 1, 2, "foo")]
601 }
602 61 [0, 1, 1, 2, 0];
603  
604 5 ▶sub all_of {
Subroutine all_of: called CC=1 SCAR=0.0
Pod undocumented
605 5 my (@fn) = @_;
606 8 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
607 8 for my $fn (@fn) {
608 14 my $r = &$fn;
609 14 ▶ return failwith [$r], "all_of" unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 4 10 14
610 }
611 1
612 4 }
613 5 }
614  
615 5 ▶sub both {
Subroutine both: called CC=2 SCAR=7.2
Pod undocumented
616 5 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 5 0 5
617 5 all_of(@_)
618 }
619  
620 61 1
« lib/FP/Repl.pm ↑ Summary lib/Chj/Unix/Signal.pm »