Util.pm

filter: Statementstmt 99.1% 116 / 117 Branchbran 97.9% 47 / 48 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 27 / 27 Podpod 100% 13 / 13 totaltotal 95.4% 209 / 219 CC 31 SCAR 11.5
CC
31
Cov
97%
__ANON__
7.1
__ANON__
5.0
l_concat
4.1
CRAP 3.2
« Iterator.pm ↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Lazy::Util;
5 1 $Lazy::Util::VERSION = '0.004';
6 #ABSTRACT: Perl utilities for lazy evaluation
7  
8  
9 1 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter qw/ import /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Lazy::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶use constant SCALAR_DEFER => eval { require Scalar::Defer; 1 };
Subroutine BEGIN: called CC=1 SCAR=0.1
15  
16 1 our @EXPORT_OK = qw/
17 l_concat l_first l_grep l_map l_until g_count g_first g_join g_last g_max
18 g_min g_prod g_sum
19 /;
20  
21 1 our %EXPORT_TAGS = (all => [@EXPORT_OK],);
22  
23 91 ▶sub _isa { defined blessed $_[0] and $_[0]->isa($_[1]); }
Subroutine _isa: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($_[0]) and $_[0]->isa($_[1]) 8 83 91
24  
25  
26 29 ▶sub l_concat {
Subroutine l_concat: called CC=4 SCAR=14.0
Pod documented
27 29 my (@vals) = grep defined, @_;
28  
29 29 ▶ return Lazy::Iterator->new(sub {undef}) if @vals == 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if @vals == 0 8 21 29
30  
31 21 ▶ return $vals[0] if @vals == 1 and _isa($vals[0], 'Lazy::Iterator');
Branch: 1 branch
BranchTrueFalseTotal
if @vals == 1 and _isa($vals[0], "Lazy::Iterator") 0 21 21
Condition66%2/3
@vals == 1 and _isa($vals[0], "Lazy::Iterator")
!ll&&!rl&&r
1830
MC/DC0%0/2
@vals == 1 and _isa($vals[0], "Lazy::Iterator")
@vals == 1 _isa($vals[0], "Lazy::Iterator")
Truth table66%2/3
A and B
A= @vals == 1B= _isa($vals[0], "Lazy::Iterator")
ABresult
0X0
100
111
32  
33 return Lazy::Iterator->new(
34 102 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
35 102 ▶ while (@vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 88 16 104
36  
37 # if it's a Scalar::Defer or a CODE reference, coerce into a
38 # Lazy::Iterator object
39 88 ▶ $vals[0] = Lazy::Iterator->new($vals[0])
Branch: 1 branch
BranchTrueFalseTotal
if &SCALAR_DEFER() and _isa($vals[0], 0) or ref $vals[0] eq "CODE" 2 86 88
Condition50%3/6
&SCALAR_DEFER() and _isa($vals[0], 0) or ref $vals[0] eq "CODE"
l!l&&r!l&&!r
0286
&SCALAR_DEFER() and _isa($vals[0], 0)
!ll&&!rl&&r
8800
MC/DC33%1/3
&SCALAR_DEFER() and _isa($vals[0], 0) or ref $vals[0] eq "CODE"
&SCALAR_DEFER() _isa($vals[0], 0) ref $vals[0] eq "CODE"
Truth table40%2/5
A and B or C
A= &SCALAR_DEFER()B= _isa($vals[0], 0)C= ref $vals[0] eq "CODE"
ABCresult
11X1
0X11
1011
0X00
1000
40 if SCALAR_DEFER && _isa($vals[0], 0)
41 or ref $vals[0] eq 'CODE';
42  
43 # if by this point it's not a Lazy::Iterator object, simply return it
44 # and remove from @vals
45 88 ▶ return shift @vals if not _isa($vals[0], 'Lazy::Iterator');
Branch: 1 branch
BranchTrueFalseTotal
unless _isa($vals[0], "Lazy::Iterator") 80 8 88
46  
47 # ->get the next value from the Lazy::Iterator object and return it if
48 # it's defined
49 8 ▶ if (defined(my $get = $vals[0]->get())) { return $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals[0]->get)) { } 6 2 8
50 2 else { shift @vals; }
51 }
52 16 return undef;
53 }
54 21 );
55 }
56  
57  
58 1 ▶sub l_first {
Subroutine l_first: called CC=1 SCAR=0.0
Pod documented
59 1 my ($n, @vals) = @_;
60  
61 1 my $vals = l_concat @vals;
62  
63 return Lazy::Iterator->new(
64 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
65 4 ▶ return $vals->get() if $n-- > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n-- > 0 3 1 4
66 1 return undef;
67 }
68 1 );
69 }
70  
71  
72 1 ▶sub l_grep (&@) {
Subroutine l_grep: called CC=1 SCAR=0.0
Pod documented
73 1 my ($grep, @vals) = @_;
74  
75 1 my $vals = l_concat @vals;
76  
77 return Lazy::Iterator->new(
78 3 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
79 3 ▶ while (defined(my $get = $vals->get())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 5 1 6
80 5 for ($get) {
81 5 ▶ if ($grep->($get)) { return $get }
Branch: 1 branch
BranchTrueFalseTotal
if (&$grep($get)) 2 3 5
82 }
83 }
84  
85 1 return undef;
86 }
87 1 );
88 }
89  
90  
91 4 ▶sub l_map (&@) {
Subroutine l_map: called CC=1 SCAR=0.0
Pod documented
92 4 my ($map, @vals) = @_;
93  
94 4 my $vals = l_concat @vals;
95  
96 4 my @subvals = ();
97 return Lazy::Iterator->new(
98 18 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
99 18 ▶ return shift @subvals if @subvals;
Branch: 1 branch
BranchTrueFalseTotal
if @subvals 1 17 18
100  
101 17 ▶ while (not @subvals) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subvals) 30 13 43
102 30 my $get = $vals->get();
103 30 ▶ return undef if not defined $get;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $get 4 26 30
104  
105 26 @subvals = $map->($get) for $get;
106 }
107  
108 13 return shift @subvals;
109 }
110 4 );
111 }
112  
113  
114 2 ▶sub l_until (&@) {
Subroutine l_until: called CC=1 SCAR=0.0
Pod documented
115 2 my ($until, @vals) = @_;
116  
117 2 my $vals = l_concat @vals;
118  
119 2 my $found = 0;
120 return Lazy::Iterator->new(
121 9 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
122 9 ▶ return undef if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 2 7 9
123  
124 7 my $get = $vals->get();
125 7 $found = $until->($get) for $get;
126  
127 7 return $get;
128 }
129 2 );
130 }
131  
132  
133 2 ▶sub g_count {
Subroutine g_count: called CC=2 SCAR=6.9
Pod documented
134 2 my (@vals) = @_;
135  
136 2 my $vals = l_concat @vals;
137  
138 2 my $n = 0;
139 2 ▶ while (defined $vals->get()) { $n++; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vals->get) 4 2 6
140  
141 2 return $n;
142 }
143  
144  
145 2 ▶sub g_first {
Subroutine g_first: called CC=1 SCAR=0.0
Pod documented
146 2 my (@vals) = @_;
147  
148 2 my $vals = l_concat @vals;
149  
150 2 return $vals->get();
151 }
152  
153  
154 3 ▶sub g_join {
Subroutine g_join: called CC=2 SCAR=6.9
Pod documented
155 3 my ($sep, @vals) = @_;
156  
157 3 my $vals = l_concat @vals;
158  
159 3 my $ret = $vals->get();
160 3 ▶ while (defined(my $get = $vals->get())) { $ret .= $sep . $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 2 3 5
161  
162 3 return $ret;
163 }
164  
165  
166 2 ▶sub g_last {
Subroutine g_last: called CC=2 SCAR=6.9
Pod documented
167 2 my @vals = @_;
168  
169 2 my $vals = l_concat @vals;
170  
171 2 my $ret = undef;
172 2 ▶ while (defined(my $get = $vals->get())) { $ret = $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 3 2 5
173  
174 2 return $ret;
175 }
176  
177  
178 2 ▶sub g_max {
Subroutine g_max: called CC=3 SCAR=11.0
Pod documented
179 2 my @vals = @_;
180  
181 2 my $vals = l_concat @vals;
182  
183 2 my $ret = $vals->get();
184 2 ▶ while (defined(my $get = $vals->get())) { $ret = $get if $get > $ret; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 6 2 8
if $get > $ret 3 3 6
185  
186 2 return $ret;
187 }
188  
189  
190 2 ▶sub g_min {
Subroutine g_min: called CC=3 SCAR=11.0
Pod documented
191 2 my @vals = @_;
192  
193 2 my $vals = l_concat @vals;
194  
195 2 my $ret = $vals->get();
196 2 ▶ while (defined(my $get = $vals->get())) { $ret = $get if $get < $ret; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 7 2 9
if $get < $ret 1 6 7
197  
198 2 return $ret;
199 }
200  
201  
202 3 ▶sub g_prod {
Subroutine g_prod: called CC=3 SCAR=11.0
Pod documented
203 3 my @vals = @_;
204  
205 3 my $vals = l_concat @vals;
206  
207 3 my $ret = 1;
208 3 ▶ while (defined(my $get = $vals->get())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 7 2 9
209 7 $ret *= $get;
210 7 ▶ return 0 if $ret == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ret == 0 1 6 7
211 }
212  
213 2 return $ret;
214 }
215  
216  
217 2 ▶sub g_sum {
Subroutine g_sum: called CC=2 SCAR=6.9
Pod documented
218 2 my @vals = @_;
219  
220 2 my $vals = l_concat @vals;
221  
222 2 my $ret = 0;
223 2 ▶ while (defined(my $get = $vals->get())) { $ret += $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 3 2 5
224  
225 2 return $ret;
226 }
227  
228 1 1;
229  
230 __END__
« Iterator.pm ↑ Summary