Util.pm

filter: Statementstmt 99.2% 131 / 132 Branchbran 98.2% 55 / 56 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 31 / 31 Podpod 100% 15 / 15 totaltotal 95.9% 238 / 248 CC 35 SCAR 11.3
CC
35
Cov
97%
__ANON__
7.1
__ANON__
5.0
l_concat
4.1
CRAP 3.1
« Util/OO.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.002';
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::Util::OO;
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_nuniq l_uniq l_until g_count g_first g_join
18 g_last g_max 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::Util::OO->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::Util::OO');
Branch: 1 branch
BranchTrueFalseTotal
if @vals == 1 and _isa($vals[0], "Lazy::Util::OO") 0 20 20
Condition66%2/3
@vals == 1 and _isa($vals[0], "Lazy::Util::OO")
!ll&&!rl&&r
1820
MC/DC0%0/2
@vals == 1 and _isa($vals[0], "Lazy::Util::OO")
@vals == 1 _isa($vals[0], "Lazy::Util::OO")
Truth table66%2/3
A and B
A= @vals == 1B= _isa($vals[0], "Lazy::Util::OO")
ABresult
0X0
100
111
32  
33 return Lazy::Util::OO->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::Util::OO object
39 84 ▶ $vals[0] = Lazy::Util::OO->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::Util::OO object, simply return it
44 # and remove from @vals
45 84 ▶ return shift @vals if not _isa($vals[0], 'Lazy::Util::OO');
Branch: 1 branch
BranchTrueFalseTotal
unless _isa($vals[0], "Lazy::Util::OO") 80 4 84
46  
47 # ->get the next value from the Lazy::Util::OO 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::Util::OO->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::Util::OO->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 2 ▶ my ($map, @vals) = @_;
Subroutine l_map: called CC=1 SCAR=0.0
Pod documented
93  
94 2 my $vals = l_concat @vals;
95  
96 2 my @subvals = ();
97 return Lazy::Util::OO->new(
98 sub {
99 8 ▶ return shift @subvals if @subvals;
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if @subvals 1 7 8
100  
101 7 ▶ while (not @subvals) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subvals) 10 5 15
102 10 my $get = $vals->get();
103 10 ▶ return undef if not defined $get;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $get 2 8 10
104  
105 8 @subvals = $map->($get) for $get;
106 }
107  
108 5 return shift @subvals;
109 }
110 2 );
111 }
112  
113  
114 sub l_nuniq {
115 1 ▶ my (@vals) = @_;
Subroutine l_nuniq: called CC=1 SCAR=0.0
Pod documented
116  
117 1 my $vals = l_concat @vals;
118  
119 1 my %uniq;
120 return Lazy::Util::OO->new(
121 sub {
122 5 ▶ while (defined(my $get = $vals->get())) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 9 1 10
123 9 my $key = 0 + $get;
124 9 ▶ $uniq{$key}++ or return $get;
Branch: 1 branch
BranchTrueFalseTotal
$uniq{$key}++ or return $get 4 5 9
125 }
126 1 return undef;
127 }
128 1 );
129 }
130  
131  
132 sub l_uniq {
133 1 ▶ my (@vals) = @_;
Subroutine l_uniq: called CC=1 SCAR=0.0
Pod documented
134  
135 1 my $vals = l_concat @vals;
136  
137 1 my %uniq;
138 return Lazy::Util::OO->new(
139 sub {
140 5 ▶ while (defined(my $get = $vals->get())) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 9 1 10
141 9 ▶ $uniq{$get}++ or return $get;
Branch: 1 branch
BranchTrueFalseTotal
$uniq{$get}++ or return $get 4 5 9
142 }
143 1 return undef;
144 }
145 1 );
146 }
147  
148  
149 sub l_until (&@) {
150 2 ▶ my ($until, @vals) = @_;
Subroutine l_until: called CC=1 SCAR=0.0
Pod documented
151  
152 2 my $vals = l_concat @vals;
153  
154 2 my $found = 0;
155 return Lazy::Util::OO->new(
156 sub {
157 9 ▶ return undef if $found;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if $found 2 7 9
158  
159 7 my $get = $vals->get();
160 7 $found = $until->($get) for $get;
161  
162 7 return $get;
163 }
164 2 );
165 }
166  
167  
168 sub g_count {
169 2 ▶ my (@vals) = @_;
Subroutine g_count: called CC=2 SCAR=6.9
Pod documented
170  
171 2 my $vals = l_concat @vals;
172  
173 2 my $n = 0;
174 2 ▶ while (defined $vals->get()) { $n++; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vals->get) 4 2 6
175  
176 2 return $n;
177 }
178  
179  
180 sub g_first {
181 2 ▶ my (@vals) = @_;
Subroutine g_first: called CC=1 SCAR=0.0
Pod documented
182  
183 2 my $vals = l_concat @vals;
184  
185 2 return $vals->get();
186 }
187  
188  
189 sub g_join {
190 3 ▶ my ($sep, @vals) = @_;
Subroutine g_join: called CC=2 SCAR=6.9
Pod documented
191  
192 3 my $vals = l_concat @vals;
193  
194 3 my $ret = $vals->get();
195 3 ▶ while (defined(my $get = $vals->get())) { $ret .= $sep . $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 2 3 5
196  
197 3 return $ret;
198 }
199  
200  
201 sub g_last {
202 2 ▶ my @vals = @_;
Subroutine g_last: called CC=2 SCAR=6.9
Pod documented
203  
204 2 my $vals = l_concat @vals;
205  
206 2 my $ret = undef;
207 2 ▶ while (defined(my $get = $vals->get())) { $ret = $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 3 2 5
208  
209 2 return $ret;
210 }
211  
212  
213 sub g_max {
214 2 ▶ my @vals = @_;
Subroutine g_max: called CC=3 SCAR=11.0
Pod documented
215  
216 2 my $vals = l_concat @vals;
217  
218 2 my $ret = $vals->get();
219 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
220  
221 2 return $ret;
222 }
223  
224  
225 sub g_min {
226 2 ▶ my @vals = @_;
Subroutine g_min: called CC=3 SCAR=11.0
Pod documented
227  
228 2 my $vals = l_concat @vals;
229  
230 2 my $ret = $vals->get();
231 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
232  
233 2 return $ret;
234 }
235  
236  
237 sub g_prod {
238 3 ▶ my @vals = @_;
Subroutine g_prod: called CC=3 SCAR=11.0
Pod documented
239  
240 3 my $vals = l_concat @vals;
241  
242 3 my $ret = 1;
243 3 ▶ while (defined(my $get = $vals->get())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 7 2 9
244 7 $ret *= $get;
245 7 ▶ return 0 if $ret == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ret == 0 1 6 7
246 }
247  
248 2 return $ret;
249 }
250  
251  
252 sub g_sum {
253 2 ▶ my @vals = @_;
Subroutine g_sum: called CC=2 SCAR=6.9
Pod documented
254  
255 2 my $vals = l_concat @vals;
256  
257 2 my $ret = 0;
258 2 ▶ while (defined(my $get = $vals->get())) { $ret += $get; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $get = $vals->get)) 3 2 5
259  
260 2 return $ret;
261 }
262  
263 1 1;
264  
265 __END__
« Util/OO.pm ↑ Summary