blib/lib/perl5i/2/ARRAY.pm

filter: Statementstmt 75.8% 188 / 248 Branchbran 56.3% 53 / 94 Conditioncond 50.0% 12 / 24 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 87.5% 49 / 56 Podpod 0.0% 0 / 8 totaltotal 69.2% 306 / 442 SCAR 58.7
CC
94
Cov
69%
__ANON__
44.7
diff
44.7
__ANON__
33.9
CRAP 354.0
« blib/lib/perl5i/0/ARRAY.pm ↑ Summary blib/lib/perl5i/1/SCALAR.pm »
1 # vi: set ts=4 sw=4 ht=4 et :
2 package perl5i::2::ARRAY;
3 109 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 109 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 109 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 109 ▶no if $] >= 5.018000, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Don't accidentally turn carp/croak into methods.
10 76 require Carp::Fix::1_25;
11  
12 109 ▶use perl5i::2::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 109 ▶use perl5i::2::autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # A foreach which honors the number of parameters in the signature
16 109 ▶method foreach($code) {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine foreach: not called CC=4 SCAR=28.7
17 0 my $n = 1;
18 0 ▶ if( my $sig = $code->signature ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sig = $code->signature) 0 0 0
19 0 $n = $sig->num_positional_params;
20 0 ▶ Carp::Fix::1_25::croak("Function passed to foreach takes no arguments") unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
21 }
22  
23 0 my $idx = 0;
24 0 ▶ while ( $idx <= $#{$self} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $#{$self;}) 0 0 0
25 0 $code->(@{$self}[$idx..($idx+$n-1)]);
26 0 $idx += $n;
27 }
28  
29 0 return;
30 76 }
31  
32 109 ▶method first($filter) {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine first: not called CC=2 SCAR=12.2
33 # Deep recursion and segfault (lines 90 and 91 in first.t) if we use
34 # the same elegant approach as in grep().
35 0 ▶ if ( ref $filter eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter eq "Regexp") 0 0 0
36 0 ▶ return List::Util::first( sub { $_ ~~ $filter }, @$self );
Subroutine __ANON__: called CC=1 SCAR=1.2
37 }
38  
39 0 ▶ return List::Util::first( sub { $filter->() }, @$self );
Subroutine __ANON__: called CC=1 SCAR=1.2
40  
41 76 }
42  
43 109 ▶method map( $code ) {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine map: not called CC=2 SCAR=15.4
44 0 my @result = CORE::map { $code->($_) } @$self;
45  
46 0 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
47 76 }
48  
49 109 ▶method as_hash{
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=2 SCAR=15.1
50 0 my %result = CORE::map { $_ => 1 } @$self;
51 0 ▶ return wantarray ? %result : \%result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
52 76 }
53  
54  
55 109 ▶method pick ( $num ){
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=10 SCAR=35.2
Subroutine pick: called CC=10 SCAR=35.2
56 0 ▶ Carp::Fix::1_25::croak("pick() takes the number of elements to pick")
Branch: 2 branches
BranchTrueFalseTotal
unless defined $num 0 0 0
unless defined $num 4 52 56
57 unless defined $num;
58 0 ▶ Carp::Fix::1_25::croak("pick() takes a positive integer or zero, not '$num'")
Branch: 2 branches
BranchTrueFalseTotal
unless $num->is_integer and $num->is_positive || $num == 0 0 0 0
unless $num->is_integer and $num->is_positive || $num == 0 1 4 5
Condition0%0/12
$num->is_integer and $num->is_positive || $num == 0
!ll&&!rl&&r
000
$num->is_positive or $num == 0
l!l&&r!l&&!r
000
$num->is_integer and $num->is_positive || $num == 0
!ll&&!rl&&r
000
$num->is_positive or $num == 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$num->is_integer and $num->is_positive || $num == 0
$num->is_integer $num->is_positive $num == 0
MC/DC0%0/3
$num->is_integer and $num->is_positive || $num == 0
$num->is_integer $num->is_positive $num == 0
Truth table0%0/4
A and B or C
A= $num->is_integerB= $num->is_positiveC= $num == 0
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= $num->is_integerB= $num->is_positiveC= $num == 0
ABCresult
0XX0
1000
11X1
1011
59 unless $num->is_integer && ($num->is_positive or $num == 0);
60
61 0 ▶ if($num >= @$self){
Branch: 2 branches
BranchTrueFalseTotal
if ($num >= @$self) 0 0 0
if ($num >= @$self) 21 18 39
62 0 my @result = List::Util::shuffle(@$self);
63 0 ▶ return wantarray ? @result : \@result;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
wantarray ? : 4 35 39
64 }
65
66 # for the first position in the array, generate a random number that gives
67 # that element an n/N chance of being picked (where n is the number of elements to pick and N is the total array size);
68 # repeat for the rest of the array, each time altering the probability of
69 # the element being picked to reflect the number of elements picked so far and the number left.
70 0 my $num_left = @$self;
71 0 my @result;
72 0 my $i=0;
73 0 ▶ while($num > 0){
Branch: 2 branches
BranchTrueFalseTotal
if ($num > 0) 0 0 0
if ($num > 0) 1 34 35
74 0 my $rand = int(rand($num_left));
75 0 ▶ if($rand < $num){
Branch: 2 branches
BranchTrueFalseTotal
if ($rand < $num) 0 0 0
if ($rand < $num) 0 0 0
76 0 push(@result, $self->[$i]);
77 0 $num--;
78 }
79 0 $num_left--;
80 0 $i++;
81 }
82  
83 # Don't return the picks in the same order as the original array
84 # Simulates what would happen if you shuffled first
85 0 @result = @result->shuffle;
86  
87 0 ▶ return wantarray ? @result : \@result;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
wantarray ? : 0 0 0
88 76 }
89  
90  
91 109 ▶method pick_one() {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine pick_one: not called CC=1 SCAR=2.6
92 0 return @$self[int rand @$self];
93 76 }
94  
95  
96 109 ▶method grep($filter) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine grep: called CC=2 SCAR=6.9
97 14 my @result = CORE::grep { $_ ~~ $filter } @$self;
98  
99 14 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 13 14
100 76 }
101  
102 109 ▶method popn($times) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine popn: called CC=7 SCAR=19.5
Subroutine __ANON__: called CC=7 SCAR=19.5
103 7 ▶ Carp::Fix::1_25::croak("popn() takes the number of elements to pop")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $times 1 6 7
104 unless defined $times;
105 6 ▶ Carp::Fix::1_25::croak("popn() takes a positive integer or zero, not '$times'")
Branch: 1 branch
BranchTrueFalseTotal
unless $times->is_integer and $times->is_positive || $times == 0 2 4 6
Condition100%6/6
$times->is_integer and $times->is_positive || $times == 0
!ll&&!rl&&r
141
$times->is_positive or $times == 0
l!l&&r!l&&!r
311
MC/DC66%2/3
$times->is_integer and $times->is_positive || $times == 0
$times->is_integer $times->is_positive $times == 0
Truth table75%3/4
A and B or C
A= $times->is_integerB= $times->is_positiveC= $times == 0
ABCresult
0XX0
1000
11X1
1011
106 unless $times->is_integer && ($times->is_positive or $times == 0);
107  
108 # splice() will choke if you walk off the array, so rein it in
109 4 ▶ $times = scalar(@$self) if ($times > scalar(@$self));
Branch: 1 branch
BranchTrueFalseTotal
if $times > scalar @$self 1 3 4
110  
111 4 my @result = splice(@$self, -$times, $times);
112 4 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
113 76 }
114  
115 109 ▶method shiftn($times) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine shiftn: called CC=7 SCAR=19.5
116 7 ▶ Carp::Fix::1_25::croak("shiftn() takes the number of elements to shift")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $times 1 6 7
117 unless defined $times;
118 6 ▶ Carp::Fix::1_25::croak("shiftn() takes a positive integer or zero, not '$times'")
Branch: 1 branch
BranchTrueFalseTotal
unless $times->is_integer and $times->is_positive || $times == 0 2 4 6
Condition100%6/6
$times->is_integer and $times->is_positive || $times == 0
!ll&&!rl&&r
141
$times->is_positive or $times == 0
l!l&&r!l&&!r
311
MC/DC66%2/3
$times->is_integer and $times->is_positive || $times == 0
$times->is_integer $times->is_positive $times == 0
Truth table75%3/4
A and B or C
A= $times->is_integerB= $times->is_positiveC= $times == 0
ABCresult
0XX0
1000
11X1
1011
119 unless $times->is_integer && ($times->is_positive or $times == 0);
120  
121 # splice() will choke if you walk off the array, so rein it in
122 4 ▶ $times = scalar(@$self) if ($times > scalar(@$self));
Branch: 1 branch
BranchTrueFalseTotal
if $times > scalar @$self 1 3 4
123  
124 4 my @result = splice(@$self, 0, $times);
125 4 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
126 76 }
127  
128 sub all {
129 2 ▶ require List::MoreUtils;
Subroutine all: called CC=1 SCAR=0.0
Pod undocumented
130 2 return &List::MoreUtils::all($_[1], @{$_[0]});
131 }
132  
133 sub any {
134 2 ▶ require List::MoreUtils;
Subroutine any: called CC=1 SCAR=0.0
Pod undocumented
135 2 return &List::MoreUtils::any($_[1], @{$_[0]});
136 }
137  
138 sub none {
139 2 ▶ require List::MoreUtils;
Subroutine none: called CC=1 SCAR=0.0
Pod undocumented
140 2 return &List::MoreUtils::none($_[1], @{$_[0]});
141 }
142  
143 sub true {
144 2 ▶ require List::MoreUtils;
Subroutine true: called CC=1 SCAR=0.0
Pod undocumented
145 2 return &List::MoreUtils::true($_[1], @{$_[0]});
146 }
147  
148 sub false {
149 2 ▶ require List::MoreUtils;
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
150 2 return &List::MoreUtils::false($_[1], @{$_[0]});
151 }
152  
153 sub uniq {
154 4 ▶ require List::MoreUtils;
Subroutine uniq: called CC=2 SCAR=6.9
Pod undocumented
155 4 my @uniq = List::MoreUtils::uniq(@{$_[0]});
156 4 ▶ return wantarray ? @uniq : \@uniq;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
157 }
158  
159 sub minmax {
160 3 ▶ require List::MoreUtils;
Subroutine minmax: called CC=2 SCAR=6.9
Pod undocumented
161 3 my @minmax = List::MoreUtils::minmax(@{$_[0]});
162 3 ▶ return wantarray ? @minmax : \@minmax;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
163 }
164  
165 sub mesh {
166 3 ▶ require List::MoreUtils;
Subroutine mesh: called CC=2 SCAR=6.9
Pod undocumented
167 3 my @mesh = &List::MoreUtils::zip(@_);
168 3 ▶ return wantarray ? @mesh : \@mesh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
169 }
170  
171 # Compare differences between two arrays.
172 21 ▶my $diff_two_deeply = func($c, $d) {
Subroutine __ANON__: called CC=3 SCAR=11.0
173 21 my $diff = [];
174  
175 # For each element of $c, try to find if it is equal to any of the
176 # elements of $d. If not, it's unique, and has to be pushed into
177 # $diff.
178  
179 21 require perl5i::2::equal;
180 21 require List::MoreUtils;
181 21 foreach my $item (@$c) {
182 47 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (List::MoreUtils::any(sub { perl5i::2::equal::are_equal($item, $_); } , @$d)) 21 26 47
183 93 ▶ List::MoreUtils::any( sub { perl5i::2::equal::are_equal( $item, $_ ) }, @$d )
Subroutine __ANON__: called CC=1 SCAR=0.0
184 )
185 {
186 21 push @$diff, $item;
187 }
188 }
189  
190 21 return $diff;
191 76 };
192  
193 16 ▶my $diff_two_simply = func($c, $d) {
Subroutine __ANON__: called CC=1 SCAR=0.0
194 109 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
195 16 my %seen = map { $_ => 1 } @$d;
196  
197 16 my @diff = grep { not $seen{$_} } @$c;
198  
199 16 return \@diff;
200 76 };
201  
202 109 ▶method diff(@rest) {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine diff: not called CC=7 SCAR=38.0
203 0 ▶ unless (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rest) 0 0 0
204 0 ▶ return wantarray ? @$self : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
205 }
206  
207 0 require List::MoreUtils;
208  
209 0 ▶ my $has_refs = List::MoreUtils::any(sub { ref $_ }, @$self);
Subroutine __ANON__: called CC=1 SCAR=1.2
210  
211 0 ▶ my $diff_two = $has_refs ? $diff_two_deeply : $diff_two_simply;
Branch: 1 branch
BranchTrueFalseTotal
$has_refs ? : 0 0 0
212  
213 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
214 # ssasign ... "
215 0 ▶ die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "ARRAY";} @rest 0 0 0
216  
217 0 foreach my $array (@rest) {
218 0 $self = $diff_two->($self, $array);
219 }
220  
221 0 ▶ return wantarray ? @$self : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
222 76 }
223  
224  
225 16 ▶my $intersect_two_simply = func($c, $d) {
Subroutine __ANON__: called CC=1 SCAR=0.0
226 109 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
227 16 my %seen = map { $_ => 1 } @$d;
228  
229 16 my @intersect = grep { $seen{$_} } @$c;
230  
231 16 return \@intersect;
232 76 };
233  
234 # Compare differences between two arrays.
235 14 ▶my $intersect_two_deeply = func($c, $d) {
Subroutine __ANON__: called CC=3 SCAR=11.0
236 14 require perl5i::2::equal;
237  
238 14 my $intersect = [];
239  
240 # For each element of $c, try to find if it is equal to any of the
241 # elements of $d. If it is, it's shared, and has to be pushed into
242 # $intersect.
243  
244 14 require List::MoreUtils;
245 14 foreach my $item (@$c) {
246 33 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (List::MoreUtils::any(sub { perl5i::2::equal::are_equal($item, $_); } , @$d)) 20 13 33
247 80 ▶ List::MoreUtils::any( sub { perl5i::2::equal::are_equal( $item, $_ ) }, @$d )
Subroutine __ANON__: called CC=1 SCAR=0.0
248 )
249 {
250 20 push @$intersect, $item;
251 }
252 }
253  
254 14 return $intersect;
255 76 };
256  
257 109 ▶method intersect(@rest) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine intersect: called CC=7 SCAR=19.5
258 33 ▶ unless (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rest) 1 32 33
259 1 ▶ return wantarray ? @$self : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
260 }
261  
262 32 require List::MoreUtils;
263 32 ▶ my $has_refs = List::MoreUtils::any(sub { ref $_ }, @$self);
Subroutine __ANON__: called CC=1 SCAR=0.0
264  
265 32 ▶ my $intersect_two = $has_refs ? $intersect_two_deeply : $intersect_two_simply;
Branch: 1 branch
BranchTrueFalseTotal
$has_refs ? : 14 18 32
266  
267 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
268 # ssasign ... "
269 32 ▶ die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "ARRAY";} @rest 4 28 32
270  
271 28 foreach my $array (@rest) {
272 30 $self = $intersect_two->($self, $array);
273 }
274  
275 28 ▶ return wantarray ? @$self : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 27 28
276 76 }
277  
278 109 ▶method ltrim($charset) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
279 4 my @result = CORE::map { $_->ltrim($charset) } @$self;
280  
281 4 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
282 76 }
283  
284 109 ▶method rtrim($charset) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine rtrim: called CC=2 SCAR=6.9
285 4 my @result = CORE::map { $_->rtrim($charset) } @$self;
286  
287 4 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
288 76 }
289  
290 109 ▶method trim($charset) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine trim: called CC=2 SCAR=6.9
291 5 my @result = CORE::map { $_->trim($charset) } @$self;
292  
293 5 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
294 76 }
295  
296  
297 76 1;
« blib/lib/perl5i/0/ARRAY.pm ↑ Summary blib/lib/perl5i/1/SCALAR.pm »