blib/lib/List/Rubyish.pm

filter: Statementstmt 12.4% 27 / 217 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 11.8% 7 / 59 Podpod 87.1% 34 / 39 totaltotal 15.0% 68 / 452 CC 68 SCAR 32.1
CC
68
Cov
8%
grep
90.0
slice
72.0
delete
72.0
CRAP 24.7
↑ Summary
1 package List::Rubyish;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use overload
6 1 ▶ '<<' => sub {
Subroutine BEGIN: called CC=1 SCAR=5.5
Subroutine __ANON__: not called CC=3 SCAR=24.8
7 0 my($self, $add) = @_;
8 0 ▶ $add = [ $add ] unless ref($add) eq 'ARRAY' || ref($add) eq ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $add eq "ARRAY" or ref $add eq ref $self 0 0 0
Condition0%0/3
ref $add eq "ARRAY" or ref $add eq ref $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $add eq "ARRAY" or ref $add eq ref $self
ref $add eq "ARRAY" ref $add eq ref $self
Truth table0%0/3
A or B
A= ref $add eq "ARRAY"B= ref $add eq ref $self
ABresult
1X1
011
000
9 0 $self->push(@$add);
10 },
11 0 ▶ '>>' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
12 0 my($self, $add) = @_;
13 0 ▶ if (ref($self) eq ref($add)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self eq ref $add) { } 0 0 0
elsif (ref $add ne 'ARRAY') { } 0 0 0
14 0 my $tmp = $self; $self = $add; $add = $tmp;
15 } elsif (ref($add) ne 'ARRAY') {
16 0 $add = [ $add ];
17 }
18 0 $self->unshift(@$add);
19 },
20 0 ▶ '+' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
21 0 my($self, $add, $flag) = @_;
22 0 ▶ $add = [ $add ] unless ref($add) eq 'ARRAY' || ref($add) eq ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $add eq "ARRAY" or ref $add eq ref $self 0 0 0
Condition0%0/3
ref $add eq "ARRAY" or ref $add eq ref $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $add eq "ARRAY" or ref $add eq ref $self
ref $add eq "ARRAY" ref $add eq ref $self
Truth table0%0/3
A or B
A= ref $add eq "ARRAY"B= ref $add eq ref $self
ABresult
1X1
011
000
23 0 $self->add($add, $flag);
24 },
25 1 fallback => 1;
26  
27 1 our $VERSION = '0.03';
28  
29 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use List::MoreUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
34 0 my $class = shift;
35 0 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
36 0 ▶ my $array = @_ > 0 ? (@_ == 1 && ref($_[0]) eq 'ARRAY') ? shift : [ @_ ] : [];
Branch: 2 branches
BranchTrueFalseTotal
@_ > 0 ? : 0 0 0
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
37 0 bless $array, $class;
38 }
39  
40 0 ▶sub push {
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
41 0 my $self = shift;
42 0 push @$self, @_;
43 0 $self;
44 }
45  
46 0 ▶sub unshift {
Subroutine unshift: not called CC=1 SCAR=6.9
Pod documented
47 0 my $self = shift;
48 0 unshift @$self, @_;
49 0 $self;
50 }
51  
52 0 ▶sub shift {
Subroutine shift: not called CC=1 SCAR=6.9
Pod documented
53 0 shift @{$_[0]};
54 }
55  
56 0 ▶sub pop {
Subroutine pop: not called CC=1 SCAR=6.9
Pod documented
57 0 pop @{$_[0]};
58 }
59  
60 0 ▶sub first {
Subroutine first: not called CC=2 SCAR=17.9
Pod documented
61 0 my ($self, $num) = @_;
62 0 ▶ if (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num) { } 0 0 0
63 0 return $self->slice(0, $num - 1);
64 } else {
65 0 return $self->[0];
66 }
67 }
68  
69 0 ▶sub last {
Subroutine last: not called CC=2 SCAR=17.9
Pod documented
70 0 my ($self, $num) = @_;
71 0 ▶ if (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num) { } 0 0 0
72 0 return $self->slice($self->_last_index - $num + 1, $self->_last_index);
73 } else {
74 0 return $self->[-1];
75 }
76 }
77  
78 0 ▶sub slice {
Subroutine slice: not called CC=8 SCAR=42.8
Pod documented
79 0 my $self = CORE::shift;
80 0 my ($start, $end) = @_;
81 0 my $last = $#{$self};
82 0 ▶ if (defined $end) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $end) { } 0 0 0
elsif (defined $start and 0 < $start and $last <= $start) { } 0 0 0
83 0 ▶ if ($start == 0 && $last <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == 0 and $last <= $end) { } 0 0 0
Condition0%0/3
$start == 0 and $last <= $end
!ll&&!rl&&r
000
MC/DC0%0/2
$start == 0 and $last <= $end
$start == 0 $last <= $end
Truth table0%0/3
A and B
A= $start == 0B= $last <= $end
ABresult
0X0
100
111
84 0 return $self;
85 } else {
86 0 ▶ $end = $last if $last < $end;
Branch: 1 branch
BranchTrueFalseTotal
if $last < $end 0 0 0
87 0 return $self->new([ @$self[ $start .. $end ] ]);
88 }
89 ▶ } elsif (defined $start && 0 < $start && $last <= $start) {
Condition0%0/6
defined $start and 0 < $start and $last <= $start
!ll&&!rl&&r
000
defined $start and 0 < $start
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start and 0 < $start and $last <= $start
defined $start 0 < $start $last <= $start
Truth table0%0/4
A and B and C
A= defined $startB= 0 < $startC= $last <= $start
ABCresult
0XX0
10X0
1100
1111
90 0 return $self->new([]);
91 } else {
92 0 return $self;
93 }
94 }
95  
96 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
97 0 my $self = CORE::shift;
98 0 require Data::Dumper;
99 0 Data::Dumper->new([ $self->to_a ])->Purity(1)->Terse(1)->Dump;
100 }
101  
102 0 ▶sub zip {
Subroutine zip: not called CC=1 SCAR=6.9
Pod documented
103 0 my $self = CORE::shift;
104 0 my $array = \@_;
105 0 my $index = 0;
106 0 ▶ $self->collect(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
107 0 my $ary = $self->new([$_]);
108 0 $ary->push($_->[$index]) for @$array;
109 0 $index++;
110 0 $ary;
111 0 });
112 }
113  
114 0 ▶sub delete {
Subroutine delete: not called CC=8 SCAR=42.8
Pod documented
115 0 my ($self, $value, $code) = @_;
116 0 my $found = 0;
117 0 ▶ if (ref $value eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'CODE') { } 0 0 0
118 0 ▶ do { my $item = $self->shift; $value->($item) ? $found = 1 : $self->push($item) } for (0..$self->_last_index);
Branch: 1 branch
BranchTrueFalseTotal
&$value($item) ? : 0 0 0
119 } else {
120 0 ▶ do { my $item = $self->shift; $item eq $value ? $found = 1 : $self->push($item) } for (0..$self->_last_index);
Branch: 1 branch
BranchTrueFalseTotal
$item eq $value ? : 0 0 0
121 }
122 $found ? $value
123 0 ▶ : ref $code eq 'CODE' ? do { local $_ = $value; return $code->($_) }
Branch: 2 branches
BranchTrueFalseTotal
$found ? : 0 0 0
ref $code eq 'CODE' ? : 0 0 0
124 : return ;
125 }
126  
127 0 ▶sub delete_at {
Subroutine delete_at: not called CC=4 SCAR=30.0
Pod documented
128 0 my ($self, $pos) = @_;
129 0 my $last_index = $self->_last_index;
130 0 ▶ return if $pos > $last_index ;
Branch: 1 branch
BranchTrueFalseTotal
if $pos > $last_index 0 0 0
131 0 my $result;
132 $_ == $pos ? $result = $self->shift
133 0 ▶ : $self->push($self->shift) for 0..$last_index;
Branch: 1 branch
BranchTrueFalseTotal
$_ == $pos ? : 0 0 0
134 0 return $result;
135 }
136  
137 0 ▶sub delete_if {
Subroutine delete_if: not called CC=4 SCAR=30.0
Pod documented
138 0 my ($self, $code) = @_;
139 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
140 0 my $last_index = $self->_last_index;
141 0 for (0..$last_index) {
142 0 my $item = $self->shift;
143 0 local $_ = $item;
144 0 ▶ $self->push($item) unless $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
unless &$code($_) 0 0 0
145 }
146 0 return $self;
147 }
148  
149 0 ▶sub reject {
Subroutine reject: not called CC=1 SCAR=6.9
Pod undocumented
150 0 my ($self, $code) = @_;
151 0 return $self->dup->delete_if($code);
152 }
153  
154 0 ▶sub inject {
Subroutine inject: not called CC=3 SCAR=24.8
Pod documented
155 0 my ($self, $result, $code) = @_;
156 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
157 0 $result = $code->($result, $_) for @{$self->dup};
158 0 return $result;
159 }
160  
161 0 ▶sub join {
Subroutine join: not called CC=1 SCAR=6.9
Pod documented
162 0 my ($self, $delimiter) = @_;
163 0 join $delimiter, @$self;
164 }
165  
166 0 ▶sub each_index {
Subroutine each_index: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($self, $code) = @_;
168 0 $self->new([ 0..$self->_last_index ])->each($code);
169 }
170  
171 0 ▶sub _last_index {
Subroutine _last_index: not called CC=2 SCAR=17.9
172 0 my $self = CORE::shift;
173 0 ▶ $self->length ? $self->length - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->length ? : 0 0 0
174 };
175  
176 0 ▶sub concat {
Subroutine concat: not called CC=1 SCAR=6.9
Pod documented
177 0 my ($self, $array) = @_;
178 0 $self->push(@$array);
179 0 $self;
180 }
181  
182 1 *append = \&concat;
183  
184 0 ▶sub prepend {
Subroutine prepend: not called CC=1 SCAR=6.9
Pod documented
185 0 my ($self, $array) = @_;
186 0 $self->unshift(@$array);
187 0 $self;
188 }
189  
190 0 ▶sub _append_undestructive {
Subroutine _append_undestructive: not called CC=1 SCAR=6.9
191 0 my ($self, $array) = @_;
192 0 $self->dup->push(@$array);
193 }
194  
195 0 ▶sub _prepend_undestructive {
Subroutine _prepend_undestructive: not called CC=1 SCAR=6.9
196 0 my ($self, $array) = @_;
197 0 $self->dup->unshift(@$array);
198 }
199  
200 0 ▶sub add {
Subroutine add: not called CC=2 SCAR=17.9
Pod undocumented
201 0 my ($self, $array, $bool) = @_;
202 0 ▶ $bool ? $self->_prepend_undestructive($array)
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 0 0 0
203 : $self->_append_undestructive($array);
204 }
205  
206 0 ▶sub each {
Subroutine each: not called CC=3 SCAR=24.8
Pod documented
207 0 my ($self, $code) = @_;
208 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
209 0 $code->($_) for @{$self->dup};
210 0 $self;
211 }
212  
213 0 ▶sub collect {
Subroutine collect: not called CC=3 SCAR=24.8
Pod documented
214 0 my ($self, $code) = @_;
215 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
216 0 my @collected = CORE::map &$code, @{$self->dup};
217 0 ▶ wantarray ? @collected : $self->new(\@collected);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
218 }
219  
220 1 *map = \&collect;
221  
222 0 ▶sub grep {
Subroutine grep: not called CC=9 SCAR=45.0
Pod documented
223 0 my ($self, $code) = @_;
224 0 ▶ $code or return;
Branch: 1 branch
BranchTrueFalseTotal
$code or return 0 0 0
225 0 my @grepped;
226 0 ▶ if (!ref($code)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $code) { } 0 0 0
elsif (ref $code eq 'CODE') { } 0 0 0
227 0 for (@$self) {
228 0 ▶ if (ref($_) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 0 0 0
229 0 ▶ CORE::push @grepped, $_ if $_->{$code};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{$code} 0 0 0
230 } else {
231 0 ▶ CORE::push @grepped, $_ if $_->$code;
Branch: 1 branch
BranchTrueFalseTotal
if $_->$code 0 0 0
232 }
233 }
234 } elsif (ref $code eq 'CODE') {
235 0 @grepped = CORE::grep &$code, @$self;
236 } else {
237 0 croak "Invalid code";
238 }
239 0 ▶ wantarray ? @grepped : $self->new(\@grepped);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
240 }
241  
242 0 ▶sub find {
Subroutine find: not called CC=5 SCAR=34.0
Pod documented
243 0 my ($self, $cond) = @_;
244 my $code = (ref $cond and ref $cond eq 'CODE')
245 ? $cond
246 0 ▶ : sub { $_ eq $cond };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $cond && ref $cond eq 'CODE' ? : 0 0 0
Condition0%0/3
ref $cond and ref $cond eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $cond and ref $cond eq "CODE"
ref $cond ref $cond eq "CODE"
Truth table0%0/3
A and B
A= ref $condB= ref $cond eq "CODE"
ABresult
0X0
100
111
247  
248 0 ▶ for (@$self) { &$code and return $_ }
Branch: 1 branch
BranchTrueFalseTotal
&$code and return $_ 0 0 0
249 0 return;
250 }
251  
252 1 *detect = \&find;
253  
254 0 ▶sub select {
Subroutine select: not called CC=5 SCAR=34.0
Pod documented
255 0 my ($self, $code) = @_;
256 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
257 0 ▶ return $self unless $self->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->size 0 0 0
258 0 my $last_index = $self->_last_index;
259 0 my $new = $self->dup;
260 0 for (0..$last_index) {
261 0 my $item = $new->shift;
262 0 local $_ = $item;
263 0 ▶ $new->push($item) if $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($_) 0 0 0
264 }
265 0 return $new;
266 }
267  
268 1 *find_all = \&select;
269  
270 0 ▶sub index_of {
Subroutine index_of: not called CC=4 SCAR=30.0
Pod documented
271 0 my ($self, $target) = @_;
272 0 ▶ my $code = (ref $target eq 'CODE') ? $target : sub { CORE::shift eq $target };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'CODE' ? : 0 0 0
273  
274 0 ▶ for (my $i = 0; $i < $self->length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->length) 0 0 0
275 0 ▶ $code->($self->[$i]) and return $i;
Branch: 1 branch
BranchTrueFalseTotal
&$code($self->[$i]) and return $i 0 0 0
276 }
277 0 return;
278 }
279  
280 0 ▶sub sort {
Subroutine sort: not called CC=3 SCAR=24.8
Pod documented
281 0 my ($self, $code) = @_;
282 0 ▶ my @sorted = $code ? CORE::sort { $code->($a, $b) } @$self : CORE::sort @$self;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 0 0 0
283 0 ▶ wantarray ? @sorted : $self->new(\@sorted);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
284 }
285  
286 0 ▶sub sort_by {
Subroutine sort_by: not called CC=3 SCAR=24.8
Pod documented
287 0 my ($self, $code, $cmp) = @_;
288  
289 my @sorted = $cmp ?
290 0 CORE::map { $_->[1] } CORE::sort { $cmp->($a->[0], $b->[0]) } CORE::map { [$code->($_), $_] } @$self :
291 0 ▶ CORE::map { $_->[1] } CORE::sort { $a->[0] <=> $b->[0] } CORE::map { [$code->($_), $_] } @$self;
Branch: 1 branch
BranchTrueFalseTotal
$cmp ? : 0 0 0
292  
293 0 ▶ wantarray ? @sorted : $self->new(\@sorted);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
294 }
295  
296 0 ▶sub compact {
Subroutine compact: not called CC=1 SCAR=6.9
Pod documented
297 0 ▶ CORE::shift->grep(sub { defined });
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 }
299  
300 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
301 0 scalar @{$_[0]};
302 }
303  
304 1 *size = \&length;
305  
306 0 ▶sub flatten {
Subroutine flatten: not called CC=1 SCAR=6.9
Pod documented
307 0 my $self = CORE::shift;
308 0 ▶ $self->collect(sub { _flatten($_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
309 }
310  
311 0 ▶sub _flatten {
Subroutine _flatten: not called CC=3 SCAR=24.8
312 0 my $element = CORE::shift;
313 (ref $element and ref $element eq 'ARRAY')
314 0 ▶ ? CORE::map { _flatten($_) } @$element
Branch: 1 branch
BranchTrueFalseTotal
ref $element && ref $element eq 'ARRAY' ? : 0 0 0
Condition0%0/3
ref $element and ref $element eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $element and ref $element eq "ARRAY"
ref $element ref $element eq "ARRAY"
Truth table0%0/3
A and B
A= ref $elementB= ref $element eq "ARRAY"
ABresult
0X0
100
111
315 : $element;
316 }
317  
318 0 ▶sub is_empty {
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod documented
319 0 !$_[0]->length;
320 }
321  
322 0 ▶sub uniq {
Subroutine uniq: not called CC=1 SCAR=6.9
Pod documented
323 0 my $self = CORE::shift;
324 0 $self->new([ List::MoreUtils::uniq(@$self) ]);
325 }
326  
327 0 ▶sub reduce {
Subroutine reduce: not called CC=2 SCAR=17.9
Pod documented
328 0 my ($self, $code) = @_;
329 0 ▶ croak "Argument must be a code" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
330 0 ▶ List::Util::reduce { $code->($a, $b) } @$self;
Subroutine __ANON__: not called CC=1 SCAR=6.9
331 }
332  
333 0 ▶sub to_a {
Subroutine to_a: not called CC=1 SCAR=6.9
Pod undocumented
334 0 my @unblessed = @{$_[0]};
335 0 \@unblessed;
336 }
337  
338 0 ▶sub as_list { # for Template::Iterator
Subroutine as_list: not called CC=1 SCAR=6.9
Pod undocumented
339 0 CORE::shift;
340 }
341  
342 0 ▶sub dup {
Subroutine dup: not called CC=1 SCAR=6.9
Pod documented
343 0 __PACKAGE__->new($_[0]->to_a);
344 }
345  
346 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
347 0 my $self = CORE::shift;
348 0 $self->new([ reverse @$self ]);
349 }
350  
351 0 ▶sub sum {
Subroutine sum: not called CC=1 SCAR=6.9
Pod documented
352 0 List::Util::sum @{$_[0]};
353 }
354  
355 1 1;
356  
357 __END__
↑ Summary