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 SCAR 34.4
CC
31
Cov
97%
__ANON__
7.1
__ANON__
5.0
l_concat
4.1
CRAP 31.0
↑ Summary Iterator.pm »
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.003';
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 1 ▶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 86 ▶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]) 4 82 86
24  
25  
26 sub l_concat {
27 28 ▶ my (@vals) = grep defined, @_;
Subroutine l_concat: called CC=4 SCAR=14.0
Pod documented
28  
29 28 ▶ 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 20 28
30  
31 20 ▶ 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 20 20
Condition66%2/3
@vals == 1 and _isa($vals[0], "Lazy::Iterator")
!ll&&!rl&&r
1820
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 sub {
35 98 ▶ while (@vals) {
Subroutine __ANON__: called CC=7 SCAR=19.7
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 84 15 99
36  
37 # if it's a Scalar::Defer or a CODE reference, coerce into a
38 # Lazy::Iterator object
39 84 ▶ $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" 1 83 84
Condition50%3/6
&SCALAR_DEFER() and _isa($vals[0], 0) or ref $vals[0] eq "CODE"
l!l&&r!l&&!r
0183
&SCALAR_DEFER() and _isa($vals[0], 0)
!ll&&!rl&&r
8400
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 84 ▶ return shift @vals if not _isa($vals[0], 'Lazy::Iterator');
Branch: 1 branch
BranchTrueFalseTotal
unless _isa($vals[0], "Lazy::Iterator") 80 4 84
46  
47 # ->get the next value from the Lazy::Iterator object and return it if
48 # it's defined
49 4 ▶ if (defined(my $get = $vals[0]->get())) { return $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals[0]->get)) { } 3 1 4
50 1 else { shift @vals; }
51 }
52 15 return undef;
53 }
54 20 );
55 }
56  
57  
58 sub l_first {
59 1 ▶ my ($n, @vals) = @_;
Subroutine l_first: called CC=1 SCAR=0.0
Pod documented
60  
61 1 my $vals = l_concat @vals;
62  
63 return Lazy::Iterator->new(
64 sub {
65 4 ▶ return $vals->get() if $n-- > 0;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $n-- > 0 3 1 4
66 1 return undef;
67 }
68 1 );
69 }
70  
71  
72 sub l_grep (&@) {
73 1 ▶ my ($grep, @vals) = @_;
Subroutine l_grep: called CC=1 SCAR=0.0
Pod documented
74  
75 1 my $vals = l_concat @vals;
76  
77 return Lazy::Iterator->new(
78 sub {
79 3 ▶ while (defined(my $get = $vals->get())) {
Subroutine __ANON__: called CC=4 SCAR=13.9
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 sub l_map (&@) {
92 4 ▶ my ($map, @vals) = @_;
Subroutine l_map: called CC=1 SCAR=0.0
Pod documented
93  
94 4 my $vals = l_concat @vals;
95  
96 4 my @subvals = ();
97 return Lazy::Iterator->new(
98 sub {
99 18 ▶ return shift @subvals if @subvals;
Subroutine __ANON__: called CC=5 SCAR=16.1
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 sub l_until (&@) {
115 2 ▶ my ($until, @vals) = @_;
Subroutine l_until: called CC=1 SCAR=0.0
Pod documented
116  
117 2 my $vals = l_concat @vals;
118  
119 2 my $found = 0;
120 return Lazy::Iterator->new(
121 sub {
122 9 ▶ return undef if $found;
Subroutine __ANON__: called CC=3 SCAR=11.0
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 sub g_count {
134 2 ▶ my (@vals) = @_;
Subroutine g_count: called CC=2 SCAR=6.9
Pod documented
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 sub g_first {
146 2 ▶ my (@vals) = @_;
Subroutine g_first: called CC=1 SCAR=0.0
Pod documented
147  
148 2 my $vals = l_concat @vals;
149  
150 2 return $vals->get();
151 }
152  
153  
154 sub g_join {
155 3 ▶ my ($sep, @vals) = @_;
Subroutine g_join: called CC=2 SCAR=6.9
Pod documented
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 sub g_last {
167 2 ▶ my @vals = @_;
Subroutine g_last: called CC=2 SCAR=6.9
Pod documented
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 sub g_max {
179 2 ▶ my @vals = @_;
Subroutine g_max: called CC=3 SCAR=11.0
Pod documented
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 sub g_min {
191 2 ▶ my @vals = @_;
Subroutine g_min: called CC=3 SCAR=11.0
Pod documented
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 sub g_prod {
203 3 ▶ my @vals = @_;
Subroutine g_prod: called CC=3 SCAR=11.0
Pod documented
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 sub g_sum {
218 2 ▶ my @vals = @_;
Subroutine g_sum: called CC=2 SCAR=6.9
Pod documented
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__
↑ Summary Iterator.pm »