LINQ/Collection.pm

filter: Statementstmt 99.0% 397 / 401 Branchbran 96.8% 153 / 160 (2 uncoverable) Conditioncond 85.1% 23 / 27 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 97.4% 77 / 79 Podpod 100% 47 / 47 totaltotal 97.2% 710 / 732 (2 uncoverable) CC 97 SCAR 14.1
CC
97
Cov
98%
join
12.0
order_by
9.0
sequence_equal
8.0
CRAP 4.1
« LINQ/Iterator.pm ↑ Summary LINQ/Field.pm »
1 116 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package LINQ::Collection;
6  
7 116 our $AUTHORITY = 'cpan:TOBYINK';
8 116 our $VERSION = '0.003';
9  
10 116 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 116 ▶use LINQ::Util::Internal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 116 requires qw( to_list );
14  
15 24 ▶my $_coerce = sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
16 24 my ( $thing ) = @_;
17
18 24 require Scalar::Util;
19 24 ▶ if ( Scalar::Util::blessed( $thing ) and $thing->DOES( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing) and $thing->DOES(__PACKAGE__)) 12 12 24
Condition66%2/3
builtin::blessed($thing) and $thing->DOES(__PACKAGE__)
!ll&&!rl&&r
12012
MC/DC50%1/2
builtin::blessed($thing) and $thing->DOES(__PACKAGE__)
builtin::blessed($thing) $thing->DOES(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($thing)B= $thing->DOES(__PACKAGE__)
ABresult
0X0
100
111
20 12 return $thing;
21 }
22
23 12 ▶ if ( ref( $thing ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq "ARRAY") 12 0 12
24 12 require LINQ::Array;
25 12 return LINQ::Array::->new( $thing );
26 }
27
28 LINQ::Util::Internal::throw(
29 0 "CallerError",
30 message => "Expected a LINQ collection; got '$thing'"
31 );
32 116 };
33  
34 84 ▶sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
35 84 my $self = shift;
36 84 my $map = LINQ::Util::Internal::assert_code( @_ );
37
38 84 my $iter = $self->to_iterator;
39 84 my $stopped;
40
41 84 require LINQ;
42 LINQ::LINQ(
43 468 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
44 # uncoverable branch true
45 468 ▶ return LINQ::END() if $stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $stopped 0 468 468
46 468 my @got = $iter->();
47 468 ▶ if ( @got ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@got) 384 84 468
48 384 local $_;
49 384 return scalar $map->( $_ = $got[0] );
50 }
51 84 ++$stopped;
52 84 return LINQ::END();
53 }
54 84 );
55 } #/ sub select
56  
57 160 ▶sub where {
Subroutine where: called CC=1 SCAR=0.0
Pod documented
58 160 my $self = shift;
59 160 my $filter = LINQ::Util::Internal::assert_code( @_ );
60
61 160 my $iter = $self->to_iterator;
62 160 my $stopped;
63
64 160 require LINQ;
65 LINQ::LINQ(
66 424 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
67 GETVAL: {
68 424 ▶ return LINQ::END() if $stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $stopped 2 1149 1151
69 1149 my @got = $iter->();
70 1149 ▶ if ( @got ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@got) 1022 127 1149
71 1022 local $_;
72 1022 my $pass = $filter->( $_ = $got[0] );
73 1009 ▶ return $got[0] if $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $pass 282 727 1009
74 727 redo GETVAL;
75 }
76 127 ++$stopped;
77 127 return LINQ::END();
78 } #/ GETVAL:
79 }
80 160 );
81 } #/ sub where
82  
83 4 ▶sub select_many {
Subroutine select_many: called CC=1 SCAR=0.0
Pod documented
84 4 my $self = shift;
85 4 my $map = LINQ::Util::Internal::assert_code( @_ );
86
87 4 my $outer = $self->to_iterator;
88 4 my $inner;
89 my $end;
90
91 4 require LINQ;
92 LINQ::LINQ(
93 28 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
94 BODY: {
95 28 ▶ return LINQ::END() if $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end 4 52 56
96 52 ▶ if ( not $inner ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inner) 28 24 52
97 28 $inner = $outer->();
98 28 ▶ if ( defined $inner ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inner) { } 24 4 28
99 24 local $_;
100 24 $inner = $map->( $_ = $inner )->$_coerce->to_iterator;
101 }
102 else {
103 4 $end = 1;
104 4 redo BODY;
105 }
106 } #/ if ( not $inner )
107 48 my @got = $inner->();
108 48 ▶ if ( not @got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@got) 24 24 48
109 24 undef $inner;
110 24 redo BODY;
111 }
112 24 return @got;
113 } #/ BODY:
114 }
115 4 );
116 } #/ sub select_many
117  
118 6 ▶sub min {
Subroutine min: called CC=2 SCAR=6.9
Pod documented
119 6 my $self = shift;
120 6 ▶ return $self->select( @_ )->min if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 4 6
121 4 require List::Util;
122 4 &List::Util::min( $self->to_list );
123 }
124  
125 6 ▶sub max {
Subroutine max: called CC=2 SCAR=6.9
Pod documented
126 6 my $self = shift;
127 6 ▶ return $self->select( @_ )->max if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 4 6
128 4 require List::Util;
129 4 &List::Util::max( $self->to_list );
130 }
131  
132 12 ▶sub sum {
Subroutine sum: called CC=2 SCAR=6.9
Pod documented
133 12 my $self = shift;
134 12 ▶ return $self->select( @_ )->sum if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 8 12
135 8 require List::Util;
136 8 &List::Util::sum( $self->to_list );
137 }
138  
139 4 ▶sub average {
Subroutine average: called CC=1 SCAR=0.0
Pod documented
140 4 my $self = shift;
141 4 $self->sum( @_ ) / $self->count();
142 }
143  
144 5 ▶sub aggregate {
Subroutine aggregate: called CC=1 SCAR=0.0
Pod documented
145 5 my $self = shift;
146 5 my $code = LINQ::Util::Internal::assert_code( shift );
147 5 ▶ my $wrapper = sub { $code->( $a, $b ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
148 5 require List::Util;
149 5 &List::Util::reduce( $wrapper, @_, $self->to_list );
150 }
151  
152 23 ▶my $_prepare_join = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
153 23 my $x = shift;
154 23 my $y = shift;
155
156 23 ▶ my $hint = ref( $_[0] ) ? -inner : shift( @_ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 18 23
157 23 my $x_keys = LINQ::Util::Internal::assert_code( shift );
158 23 my $y_keys = LINQ::Util::Internal::assert_code( shift );
159 23 my $joiner = LINQ::Util::Internal::assert_code( @_ );
160
161 23 ▶ $hint =~ /\A-(inner|left|right|outer)\z/
Branch: 1 branch
BranchTrueFalseTotal
$hint =~ /\A-(inner|left|right|outer)\z/ or LINQ::Util::Internal::throw("CallerError", "message", "Expected a recognized join type; got '${hint}'") 23 0 23
162 or LINQ::Util::Internal::throw(
163 "CallerError",
164 message => "Expected a recognized join type; got '$hint'"
165 );
166
167 my @x_mapped =
168 23 ▶ $x->select( sub { [ scalar( $x_keys->( $_[0] ) ), $_[0] ] } )->to_list;
Subroutine __ANON__: called CC=1 SCAR=0.0
169 my @y_mapped =
170 23 ▶ $y->select( sub { [ scalar( $y_keys->( $_[0] ) ), $_[0] ] } )->to_list;
Subroutine __ANON__: called CC=1 SCAR=0.0
171
172 23 return ( \@x_mapped, \@y_mapped, $hint, $joiner );
173 116 };
174  
175 14 ▶sub join {
Subroutine join: called CC=12 SCAR=24.8
Pod documented
176 14 my ( $x_mapped, $y_mapped, $hint, $joiner ) = $_prepare_join->( @_ );
177
178 14 my @joined;
179 14 my ( @found_x, @found_y );
180
181 14 for my $Xi ( 0 .. $#$x_mapped ) {
182 68 my $X = $x_mapped->[$Xi];
183
184 68 for my $Yi ( 0 .. $#$y_mapped ) {
185 384 my $Y = $y_mapped->[$Yi];
186
187 384 ▶ if ( $X->[0] eq $Y->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($X->[0] eq $Y->[0]) 68 316 384
188 68 my $a = $X->[1];
189 68 my $b = $Y->[1];
190 68 $found_x[$Xi]++;
191 68 $found_y[$Yi]++;
192
193 68 push @joined, scalar $joiner->( $a, $b );
194 }
195 } #/ for my $Yi ( 0 .. $#$y_mapped)
196 } #/ for my $Xi ( 0 .. $#$x_mapped)
197
198 14 ▶ if ( $hint eq -left or $hint eq -outer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hint eq "-left" or $hint eq "-outer") 7 7 14
Condition100%3/3
$hint eq "-left" or $hint eq "-outer"
l!l&&r!l&&!r
437
MC/DC100%2/2
$hint eq "-left" or $hint eq "-outer"
$hint eq "-left" $hint eq "-outer"
Truth table100%3/3
A or B
A= $hint eq "-left"B= $hint eq "-outer"
ABresult
1X1
011
000
199 7 for my $Xi ( 0 .. $#$x_mapped ) {
200 35 ▶ next if $found_x[$Xi];
Branch: 1 branch
BranchTrueFalseTotal
if $found_x[$Xi] 23 12 35
201 12 my $a = $x_mapped->[$Xi][1];
202 12 my $b = undef;
203 12 push @joined, scalar $joiner->( $a );
204 }
205 }
206
207 14 ▶ if ( $hint eq -right or $hint eq -outer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hint eq "-right" or $hint eq "-outer") 6 8 14
Condition100%3/3
$hint eq "-right" or $hint eq "-outer"
l!l&&r!l&&!r
338
MC/DC100%2/2
$hint eq "-right" or $hint eq "-outer"
$hint eq "-right" $hint eq "-outer"
Truth table100%3/3
A or B
A= $hint eq "-right"B= $hint eq "-outer"
ABresult
1X1
011
000
208 6 for my $Yi ( 0 .. $#$y_mapped ) {
209 36 ▶ next if $found_y[$Yi];
Branch: 1 branch
BranchTrueFalseTotal
if $found_y[$Yi] 30 6 36
210 6 my $a = undef;
211 6 my $b = $y_mapped->[$Yi][1];
212 6 push @joined, scalar $joiner->( undef, $b );
213 }
214 }
215
216 14 LINQ::Util::Internal::create_linq( \@joined );
217 } #/ sub join
218  
219 9 ▶sub group_join {
Subroutine group_join: called CC=5 SCAR=16.1
Pod documented
220 9 my ( $x_mapped, $y_mapped, $hint, $joiner ) = $_prepare_join->( @_ );
221
222 9 ▶ $hint =~ /\A-(left|inner)\z/ or LINQ::Util::Internal::throw(
Branch: 1 branch
BranchTrueFalseTotal
$hint =~ /\A-(left|inner)\z/ or LINQ::Util::Internal::throw("CallerError", "message", "Join type '${hint}' not supported for group_join") 5 4 9
223 "CallerError",
224 message => "Join type '$hint' not supported for group_join",
225 );
226
227 5 my @joined;
228 5 my ( @found_x, @found_y );
229
230 5 for my $Xi ( 0 .. $#$x_mapped ) {
231 23 my $X = $x_mapped->[$Xi];
232 23 my @group = map $_->[1], grep $X->[0] eq $_->[0], @$y_mapped;
233
234 23 ▶ if ( @group or $hint eq -left ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@group or $hint eq "-left") 19 4 23
Condition100%3/3
@group or $hint eq "-left"
l!l&&r!l&&!r
1454
MC/DC100%2/2
@group or $hint eq "-left"
@group $hint eq "-left"
Truth table100%3/3
A or B
A= @groupB= $hint eq "-left"
ABresult
1X1
011
000
235 19 my $a = $X->[1];
236 19 my $b = LINQ::Util::Internal::create_linq( \@group );
237 19 push @joined, scalar $joiner->( $a, $b );
238 }
239 } #/ for my $Xi ( 0 .. $#$x_mapped)
240
241 5 LINQ::Util::Internal::create_linq( \@joined );
242 } #/ sub group_join
243  
244 13 ▶sub take {
Subroutine take: called CC=1 SCAR=0.0
Pod documented
245 13 my $self = shift;
246 13 my ( $n ) = @_;
247 13 ▶ $self->take_while( sub { $n-- > 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
248 }
249  
250 21 ▶sub take_while {
Subroutine take_while: called CC=1 SCAR=0.0
Pod documented
251 21 my $self = shift;
252 21 my $filter = LINQ::Util::Internal::assert_code( @_ );
253 21 my $stopped = 0;
254 21 my $iter = $self->to_iterator;
255
256 21 require LINQ;
257 LINQ::LINQ(
258 100 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
259 # uncoverable branch true
260 100 ▶ return LINQ::END() if $stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $stopped 0 100 100
261 100 my @got = $iter->();
262 98 ▶ if ( !@got or !$filter->( $_ = $got[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@got and &$filter($_ = $got[0])) 19 79 98
Condition100%3/3
@got and &$filter($_ = $got[0])
!ll&&!rl&&r
41579
MC/DC100%2/2
@got and &$filter($_ = $got[0])
@got &$filter($_ = $got[0])
Truth table100%3/3
A and B
A= @gotB= &$filter($_ = $got[0])
ABresult
0X0
100
111
263 19 $stopped++;
264 19 return LINQ::END();
265 }
266 79 return $got[0];
267 }
268 21 );
269 } #/ sub take_while
270  
271 8 ▶sub skip {
Subroutine skip: called CC=1 SCAR=0.0
Pod documented
272 8 my $self = shift;
273 8 my ( $n ) = @_;
274 8 ▶ $self->skip_while( sub { $n-- > 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
275 }
276  
277 14 ▶sub skip_while {
Subroutine skip_while: called CC=1 SCAR=0.0
Pod documented
278 14 my $self = shift;
279 14 my $filter = LINQ::Util::Internal::assert_code( @_ );
280 14 my $stopped = 0;
281 14 my $started = 0;
282 14 my $iter = $self->to_iterator;
283
284 14 require LINQ;
285 LINQ::LINQ(
286 76 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
287 SKIPPING: {
288 76 ▶ return LINQ::END() if $stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $stopped 14 104 118
289 104 my @got = $iter->();
290 104 ▶ if ( !@got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@got) 14 90 104
291 14 $stopped++;
292 14 redo SKIPPING;
293 }
294 90 ▶ return $got[0] if $started;
Branch: 1 branch
BranchTrueFalseTotal
if $started 50 40 90
295 40 ▶ if ( $filter->( $_ = $got[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$filter($_ = $got[0])) 28 12 40
296 28 redo SKIPPING;
297 }
298 12 ++$started;
299 12 return $got[0];
300 } #/ SKIPPING:
301 }
302 14 );
303 } #/ sub skip_while
304  
305 12 ▶sub concat {
Subroutine concat: called CC=1 SCAR=0.0
Pod documented
306 12 my @collections = map $_->to_iterator, @_;
307 12 my $idx = 0;
308
309 12 require LINQ;
310 LINQ::LINQ(
311 100 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
312 FIND_NEXT: {
313 100 ▶ return LINQ::END() if not @collections;
Branch: 1 branch
BranchTrueFalseTotal
unless @collections 10 111 121
314
315 111 my @got = $collections[0]->();
316 111 ▶ if ( not @got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@got) 21 90 111
317 21 shift @collections;
318 21 redo FIND_NEXT;
319 }
320
321 90 return $got[0];
322 } #/ FIND_NEXT:
323 }
324 12 );
325 } #/ sub concat
326  
327 49 ▶sub order_by {
Subroutine order_by: called CC=9 SCAR=22.0
Pod documented
328 49 my $self = shift;
329 49 my ( $hint, $keygen ) = ( -numeric, undef );
330 49 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 47 2 49
331 47 ▶ $hint = ref( $_[0] ) ? -numeric : shift( @_ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 37 47
332 47 ▶ $keygen = @_ ? LINQ::Util::Internal::assert_code( @_ ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 37 10 47
333 }
334
335 49 ▶ if ( not $keygen ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keygen) 12 37 49
336 12 ▶ if ( $hint eq -string ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hint eq '-string') { } 2 10 12
elsif ($hint eq '-numeric') { } 8 2 10
337 return LINQ::Util::Internal::create_linq(
338 2 [ sort { $a cmp $b } $self->to_list ] );
339 }
340 elsif ( $hint eq -numeric ) {
341 return LINQ::Util::Internal::create_linq(
342 8 [ sort { $a <=> $b } $self->to_list ] );
343 }
344 } #/ if ( not $keygen )
345
346 39 ▶ if ( $hint eq -string ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hint eq '-string') { } 23 16 39
elsif ($hint eq '-numeric') { } 14 2 16
347 return LINQ::Util::Internal::create_linq(
348 [
349 map $_->[1],
350 23 sort { $a->[0] cmp $b->[0] }
351 map [ $keygen->( $_ ), $_ ],
352 $self->to_list
353 ]
354 );
355 } #/ if ( $hint eq -string )
356
357 elsif ( $hint eq -numeric ) {
358 return LINQ::Util::Internal::create_linq(
359 [
360 map $_->[1],
361 14 sort { $a->[0] <=> $b->[0] }
362 map [ $keygen->( $_ ), $_ ],
363 $self->to_list
364 ]
365 );
366 } #/ elsif ( $hint eq -numeric)
367
368 LINQ::Util::Internal::throw(
369 2 "CallerError",
370 message => "Expected '-numeric' or '-string'; got '$hint'"
371 );
372 } #/ sub order_by
373  
374 2 ▶sub then_by {
Subroutine then_by: called CC=1 SCAR=0.0
Pod documented
375 2 LINQ::Util::Internal::throw( "Unimplemented", method => "then_by" );
376 }
377  
378 6 ▶sub order_by_descending {
Subroutine order_by_descending: called CC=1 SCAR=0.0
Pod documented
379 6 my $self = shift;
380 6 $self->order_by( @_ )->reverse;
381 }
382  
383 2 ▶sub then_by_descending {
Subroutine then_by_descending: called CC=1 SCAR=0.0
Pod documented
384 2 LINQ::Util::Internal::throw( "Unimplemented", method => "then_by_descending" );
385 }
386  
387 8 ▶sub reverse {
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
388 8 my $self = shift;
389 8 LINQ::Util::Internal::create_linq(
390 [ reverse( $self->to_list ) ],
391 );
392 }
393  
394 2 ▶sub group_by {
Subroutine group_by: called CC=3 SCAR=11.0
Pod documented
395 2 my $self = shift;
396 2 my $keygen = LINQ::Util::Internal::assert_code( @_ );
397
398 2 my @keys;
399 my %values;
400
401 2 for ( $self->to_list ) {
402 20 my $key = $keygen->( $_ );
403 20 ▶ unless ( $values{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($values{$key}) 6 14 20
404 6 push @keys, $key;
405 6 $values{$key} = [];
406 }
407 20 push @{ $values{$key} }, $_;
408 }
409
410 2 require LINQ::Grouping;
411 LINQ::Util::Internal::create_linq(
412 [
413 map 'LINQ::Grouping'->new(
414 key => $_,
415 2 values => LINQ::Util::Internal::create_linq( $values{$_} ),
416 ),
417 @keys
418 ]
419 );
420 } #/ sub group_by
421  
422 8 ▶sub distinct {
Subroutine distinct: called CC=2 SCAR=6.9
Pod documented
423 8 my $self = shift;
424 my $compare =
425 8 ▶ @_ ? LINQ::Util::Internal::assert_code( @_ ) : sub { $_[0] == $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 4 8
426
427 8 my @already;
428 $self->where(
429 46 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
430 46 my $maybe = $_[0];
431 46 for my $got ( @already ) {
432 86 ▶ return !!0 if $compare->( $maybe, $got );
Branch: 1 branch
BranchTrueFalseTotal
if &$compare($maybe, $got) 12 74 86
433 }
434 34 push @already, $maybe;
435 34 return !!1;
436 }
437 8 );
438 } #/ sub distinct
439  
440 4 ▶sub union {
Subroutine union: called CC=1 SCAR=0.0
Pod documented
441 4 my $self = shift;
442 4 my ( $other, @compare ) = @_;
443 4 $self->concat( $other )->distinct( @compare );
444 }
445  
446 4 ▶sub intersect {
Subroutine intersect: called CC=2 SCAR=6.9
Pod documented
447 4 my $self = shift;
448 4 my $other = shift;
449 my @compare =
450 4 ▶ @_ ? LINQ::Util::Internal::assert_code( @_ ) : sub { $_[0] == $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 2 4
451 4 ▶ $self->where( sub { $other->contains( $_, @compare ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
452 }
453  
454 4 ▶sub except {
Subroutine except: called CC=2 SCAR=6.9
Pod documented
455 4 my $self = shift;
456 4 my $other = shift;
457 my @compare =
458 4 ▶ @_ ? LINQ::Util::Internal::assert_code( @_ ) : sub { $_[0] == $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 2 4
459 4 ▶ $self->where( sub { not $other->contains( $_, @compare ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
460 }
461  
462 18 ▶sub sequence_equal {
Subroutine sequence_equal: called CC=8 SCAR=20.8
Pod documented
463 18 my $self = shift;
464 18 my ( $other, @compare ) = @_;
465
466 18 my $compare;
467 18 ▶ if ( @compare ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@compare) 12 6 18
468 12 $compare = LINQ::Util::Internal::assert_code( @compare );
469 }
470
471 18 my $iter1 = $self->to_iterator;
472 18 my $iter2 = $other->to_iterator;
473
474 18 while ( 1 ) {
475 114 my @got1 = $iter1->();
476 114 my @got2 = $iter2->();
477
478 114 ▶ if ( not @got1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @got1) { } 10 104 114
elsif (not @got2) { } 2 102 104
479 10 ▶ if ( @got2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@got2) { } 4 6 10
480 4 return !!0;
481 }
482 else {
483 6 return !!1;
484 }
485 }
486 elsif ( not @got2 ) {
487 2 return !!0;
488 }
489
490 102 ▶ if ( $compare ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compare) { } 60 42 102
491 60 ▶ return !!0 unless $compare->( $got1[0], $got2[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless &$compare($got1[0], $got2[0]) 4 56 60
492 }
493 else {
494 42 ▶ return !!0 unless $got1[0] == $got2[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $got1[0] == $got2[0] 2 40 42
495 }
496 } #/ while ( 1 )
497 } #/ sub sequence_equal
498  
499 44 ▶my $_with_default = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
500 44 my $self = shift;
501 44 my $method = shift;
502 44 my @args = @_;
503 44 my $default = pop( @args );
504
505 44 my $return;
506 44 ▶ eval { $return = $self->$method( @args ); 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $return = $self->$method(@args); 1 } } or do { my $e = $@; require Scalar::Util; die $e unless builtin::blessed($e); die $e unless $e->isa("LINQ::Exception::NotFound") or $e->isa("LINQ::Exception::MultipleFound"); die $e unless $e->collection == $self; return $default } 26 18 44
507 18 my $e = $@; # catch
508
509 # Rethrow any non-blessed errors.
510 18 require Scalar::Util;
511 18 ▶ die( $e ) unless Scalar::Util::blessed( $e );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($e) 6 12 18
512
513 # Rethrow any errors of the wrong class.
514 12 ▶ die( $e )
Branch: 1 branch
BranchTrueFalseTotal
unless $e->isa("LINQ::Exception::NotFound") or $e->isa("LINQ::Exception::MultipleFound") 0 12 12
Condition66%2/3
$e->isa("LINQ::Exception::NotFound") or $e->isa("LINQ::Exception::MultipleFound")
l!l&&r!l&&!r
1020
MC/DC0%0/2
$e->isa("LINQ::Exception::NotFound") or $e->isa("LINQ::Exception::MultipleFound")
$e->isa("LINQ::Exception::NotFound") $e->isa("LINQ::Exception::MultipleFound")
Truth table66%2/3
A or B
A= $e->isa("LINQ::Exception::NotFound")B= $e->isa("LINQ::Exception::MultipleFound")
ABresult
1X1
011
000
515 unless $e->isa( 'LINQ::Exception::NotFound' )
516 || $e->isa( 'LINQ::Exception::MultipleFound' );
517
518 # Rethrow any errors which resulted from the wrong source.
519 12 ▶ die( $e ) unless $e->collection == $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $e->collection == $self 0 12 12
520
521 12 return $default;
522 };
523
524 26 return $return;
525 116 };
526  
527 13 ▶sub first {
Subroutine first: called CC=3 SCAR=11.0
Pod documented
528 13 my $self = shift;
529 13 ▶ my $found = @_ ? $self->where( @_ ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 10 3 13
530 13 ▶ return $found->element_at( 0 ) if $found->count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $found->count > 0 7 4 11
531 4 LINQ::Util::Internal::throw( 'NotFound', collection => $self );
532 }
533  
534 6 ▶sub first_or_default {
Subroutine first_or_default: called CC=1 SCAR=0.0
Pod documented
535 6 shift->$_with_default( first => @_ );
536 }
537  
538 13 ▶sub last {
Subroutine last: called CC=3 SCAR=11.0
Pod documented
539 13 my $self = shift;
540 13 ▶ my $found = @_ ? $self->where( @_ ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 11 2 13
541 13 ▶ return $found->element_at( -1 ) if $found->count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $found->count > 0 7 4 11
542 4 LINQ::Util::Internal::throw( 'NotFound', collection => $self );
543 }
544  
545 6 ▶sub last_or_default {
Subroutine last_or_default: called CC=1 SCAR=0.0
Pod documented
546 6 shift->$_with_default( last => @_ );
547 }
548  
549 32 ▶sub single {
Subroutine single: called CC=4 SCAR=13.9
Pod documented
550 32 my $self = shift;
551 32 ▶ my $found = @_ ? $self->where( @_ ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 30 2 32
552 32 ▶ return $found->element_at( 0 ) if $found->count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $found->count == 1 22 8 30
553 8 ▶ $found->count == 0
Branch: 1 branch
BranchTrueFalseTotal
$found->count == 0 ? : 4 4 8
554 ? LINQ::Util::Internal::throw( 'NotFound', collection => $self )
555 : LINQ::Util::Internal::throw( 'MultipleFound', collection => $self,
556 found => $found );
557 }
558  
559 8 ▶sub single_or_default {
Subroutine single_or_default: called CC=1 SCAR=0.0
Pod documented
560 8 shift->$_with_default( single => @_ );
561 }
562  
563 60 ▶sub element_at {
Subroutine element_at: called CC=3 SCAR=11.0
Pod documented
564 60 my $self = shift;
565 60 my ( $i ) = @_;
566
567 60 my @list = $self->to_list;
568
569 60 ▶ if ( $i > $#list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $#list) 2 58 60
570 2 LINQ::Util::Internal::throw( 'NotFound', collection => $self );
571 }
572
573 58 ▶ if ( $i < 0 - @list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0 - @list) 2 56 58
574 2 LINQ::Util::Internal::throw( 'NotFound', collection => $self );
575 }
576
577 56 $list[$i];
578 } #/ sub element_at
579  
580 24 ▶sub element_at_or_default {
Subroutine element_at_or_default: called CC=1 SCAR=0.0
Pod documented
581 24 shift->$_with_default( element_at => @_ );
582 }
583  
584 32 ▶sub any {
Subroutine any: called CC=2 SCAR=6.9
Pod documented
585 32 my $self = shift;
586 32 ▶ my $iter = @_ ? $self->where( @_ )->to_iterator : $self->to_iterator;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 30 2 32
587 32 my @got = $iter->();
588 32 !!scalar @got;
589 }
590  
591 8 ▶sub all {
Subroutine all: called CC=1 SCAR=0.0
Pod documented
592 8 my $self = shift;
593 8 my $check = LINQ::Util::Internal::assert_code( @_ );
594 8 ▶ my $iter = $self->where( sub { not $check->( $_ ) } )->to_iterator;
Subroutine __ANON__: called CC=1 SCAR=0.0
595 8 my @got = $iter->();
596 8 !scalar @got;
597 }
598  
599 31 ▶sub contains {
Subroutine contains: called CC=4 SCAR=13.9
Pod documented
600 31 my $self = shift;
601 31 my ( $x, @args ) = @_;
602
603 31 ▶ if ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 24 7 31
604 24 splice( @args, 1, 0, $x );
605 24 return $self->any( LINQ::Util::Internal::assert_code( @args ) );
606 }
607
608 7 my $iter = $self->to_iterator;
609 7 while ( 1 ) {
610 187 ▶ my @got = $iter->() or return !!0;
Branch: 1 branch
BranchTrueFalseTotal
my(@got) = &$iter() or 184 3 187
611 184 ▶ return !!1 if $got[0] == $x;
Branch: 1 branch
BranchTrueFalseTotal
if $got[0] == $x 4 180 184
612 }
613 } #/ sub contains
614  
615 99 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
616 99 my $self = shift;
617 99 ▶ return $self->where( @_ )->count if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 98 99
618 98 my @list = $self->to_list;
619 92 return scalar( @list );
620 }
621  
622 61 ▶sub to_array {
Subroutine to_array: called CC=1 SCAR=0.0
Pod documented
623 61 my $self = shift;
624 61 [ $self->to_list ];
625 }
626  
627 4 ▶sub to_dictionary {
Subroutine to_dictionary: called CC=1 SCAR=0.0
Pod documented
628 4 my $self = shift;
629 4 my ( $keygen ) = LINQ::Util::Internal::assert_code( @_ );
630 4 +{ map +( $keygen->( $_ ), $_ ), $self->to_list };
631 }
632  
633 2 ▶sub to_lookup {
Subroutine to_lookup: called CC=1 SCAR=0.0
Pod documented
634 2 my $self = shift;
635 2 $self->to_dictionary( @_ );
636 }
637  
638 195 ▶sub to_iterator {
Subroutine to_iterator: called CC=1 SCAR=0.0
Pod documented
639 195 my $self = shift;
640 195 my @list = $self->to_list;
641 195 ▶ sub { @list ? shift( @list ) : () };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@list ? : 908 164 1072
642 }
643  
644 7 ▶sub cast {
Subroutine cast: called CC=2 SCAR=6.9
Pod documented
645 7 my $self = shift;
646 7 my ( $type ) = @_;
647
648 7 my $cast = $self->of_type( @_ );
649 7 ▶ return $cast if $self->count == $cast->count;
Branch: 1 branch
BranchTrueFalseTotal
if $self->count == $cast->count 3 4 7
650
651 4 LINQ::Util::Internal::throw( "Cast", collection => $self, type => $type );
652 }
653  
654 23 ▶sub of_type {
Subroutine of_type: called CC=7 SCAR=20.0
Pod documented
655 23 my $self = shift;
656 23 my ( $type ) = @_;
657
658 23 require Scalar::Util;
659
660 23 ▶ unless ( Scalar::Util::blessed( $type ) and $type->can( 'check' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($type) and $type->can("check")) 8 15 23
Condition100%3/3
builtin::blessed($type) and $type->can("check")
!ll&&!rl&&r
4415
MC/DC100%2/2
builtin::blessed($type) and $type->can("check")
builtin::blessed($type) $type->can("check")
Truth table100%3/3
A and B
A= builtin::blessed($type)B= $type->can("check")
ABresult
0X0
100
111
661 8 LINQ::Util::Internal::throw(
662 "CallerError",
663 message => "Expected type constraint; got '$type'",
664 );
665 }
666
667 15 ▶ if ( $type->isa( 'Type::Tiny' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->isa("Type::Tiny")) 11 4 15
668 11 my $check = $type->compiled_check;
669
670 11 ▶ if ( $type->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion) 7 4 11
671 7 my $coercion = $type->coercion->compiled_coercion;
672 7 return $self->select( $coercion )->where( $check );
673 }
674
675 4 return $self->where( $check );
676 } #/ if ( $type->isa( 'Type::Tiny'...))
677
678 4 ▶ if ( $type->can( 'has_coercion' ) and $type->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->can("has_coercion") and $type->has_coercion) 0 4 4
Condition33%1/3
$type->can("has_coercion") and $type->has_coercion
!ll&&!rl&&r
400
MC/DC0%0/2
$type->can("has_coercion") and $type->has_coercion
$type->can("has_coercion") $type->has_coercion
Truth table33%1/3
A and B
A= $type->can("has_coercion")B= $type->has_coercion
ABresult
0X0
100
111
679 return $self
680 0 ▶ ->select( sub { $type->coerce( $_ ) } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
681 0 ▶ ->where( sub { $type->check( $_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
682 }
683
684 4 ▶ return $self->where( sub { $type->check( $_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
685 } #/ sub of_type
686  
687 4 ▶sub zip {
Subroutine zip: called CC=1 SCAR=0.0
Pod documented
688 4 my $self = shift;
689 4 my $other = shift;
690 4 my $map = LINQ::Util::Internal::assert_code( @_ );
691
692 4 my $iter1 = $self->to_iterator;
693 4 my $iter2 = $other->to_iterator;
694 4 my @results;
695
696 4 require LINQ;
697 LINQ::LINQ(
698 44 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
699 44 my @r1 = $iter1->();
700 44 my @r2 = $iter2->();
701 44 ▶ return LINQ::END() unless @r1 && @r2;
Branch: 1 branch
BranchTrueFalseTotal
unless @r1 and @r2 4 40 44
Condition100%3/3
@r1 and @r2
!ll&&!rl&&r
2240
MC/DC100%2/2
@r1 and @r2
@r1 @r2
Truth table100%3/3
A and B
A= @r1B= @r2
ABresult
0X0
100
111
702 40 $map->( $r1[0], $r2[0] );
703 }
704 4 );
705 } #/ sub zip
706  
707 4 ▶sub default_if_empty {
Subroutine default_if_empty: called CC=2 SCAR=6.9
Pod documented
708 4 my $self = shift;
709 4 my $item = shift;
710
711 4 ▶ if ( $self->count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count == 0) 2 2 4
712 2 return LINQ::Util::Internal::create_linq( [$item] );
713 }
714
715 2 return $self;
716 } #/ sub default_if_empty
717  
718 9 ▶sub foreach {
Subroutine foreach: called CC=4 SCAR=13.9
Pod documented
719 9 my $self = shift;
720 9 my $code = LINQ::Util::Internal::assert_code( @_ );
721
722 9 my $ok = eval {
723 9 local $LINQ::IN_LOOP = 1;
724 9 ▶ $self->where( sub { $code->( $_ ); 0 } )->to_list;
Subroutine __ANON__: called CC=1 SCAR=0.0
725 2 1;
726 };
727 9 ▶ if ( not $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 7 2 9
728 7 my $e = $@;
729 7 require Scalar::Util;
730 7 ▶ die( $e ) unless Scalar::Util::blessed( $e );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($e) 2 5 7
731 5 ▶ die( $e ) unless $e->isa( 'LINQ::LAST' );
Branch: 1 branch
BranchTrueFalseTotal
unless $e->isa("LINQ::LAST") 2 3 5
732 }
733 5 return;
734 } #/ sub foreach
735  
736 116 1;
737  
738 __END__
« LINQ/Iterator.pm ↑ Summary LINQ/Field.pm »