Pipeline/Stages/Flow.pm

filter: Statementstmt 100% 75 / 75 Branchbran 100% 26 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 33 / 33 Podpod 100% 9 / 9 totaltotal 100% 153 / 153 CC 16 SCAR 5.6
CC
16
Cov
100%
__ANON__
3.0
__ANON__
3.0
_check_count
3.0
CRAP 1.8
« Pipeline/Stages/Batch.pm ↑ Summary Pipeline/Sinks.pm »
1 15 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Stages::Flow;
4  
5 15 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Ref::Util qw(is_coderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Ekorn::Pipeline::Gather qw(gather);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Ekorn::Pipeline::Stage qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Ekorn::Pipeline::Stop qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 15 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 15 our @EXPORT = qw(take drop); ## no critic (Modules::ProhibitAutomaticExportation)
17  
18 15 our @EXPORT_OK = (
19 @EXPORT,
20 qw(take_first take_last take_single take_while drop_while take_if drop_if),
21 );
22 15 our %EXPORT_TAGS = (all => \@EXPORT_OK);
23  
24 # Croak at the line that called Ekorn::Pipeline.
25 15 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
26  
27 # Return a stage passing on the first $count values, then stopping the
28 # pipeline, so nothing after them is fetched.
29 74 ▶sub take ($count) {
Subroutine take: called CC=1 SCAR=0.0
Pod documented
30 74 _check_count($count);
31 175 ▶ return gather {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 175 my $taken = 0;
33  
34 # Without a signature, as it's called for each value.
35 306 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
36 306 ▶ Ekorn::Pipeline::Stop->throw if $taken >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if $taken >= $count 2 304 306
37 304 ▶ return ++$taken < $count ? $_[0] : Ekorn::Pipeline::Stop->throw($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
++$taken < $count ? : 190 114 304
38 175 };
39 67 };
40 }
41  
42 # Return a stage dropping the first $count values, and passing on the
43 # rest.
44 15 ▶sub drop ($count) {
Subroutine drop: called CC=1 SCAR=0.0
Pod documented
45 15 _check_count($count);
46 66 ▶ return gather {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 66 my $dropped = 0;
48  
49 # Without a signature, as it's called for each value.
50 321 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
51 321 ▶ return $_[0] if $dropped >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped >= $count 220 101 321
52 101 $dropped++;
53 101 return;
54 66 };
55 8 };
56 }
57  
58 # Return a stage passing on the first value, then stopping the
59 # pipeline: take(1), named as keep_first's analogue.
60 4 ▶sub take_first : prototype() () {
Subroutine take_first: called CC=1 SCAR=0.0
Pod documented
61 4 return take(1);
62 }
63  
64  
65 # Return a stage holding each value, and passing on the last once they
66 # run out; nothing if there were none.
67 9 ▶sub take_last : prototype() () {
Subroutine take_last: called CC=1 SCAR=0.0
Pod documented
68 31 ▶ return gather {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 31 my @kept;
70 return {
71 # Without a signature, as it's called for each value.
72 99 ▶ step => sub { @kept = ($_[0]); return },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 30 ▶ flush => sub () { splice @kept },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 31 };
75 9 };
76 }
77  
78  
79 # Return a stage passing on the one value, asserting it's the only one:
80 # at a second, it warns and stops the pipeline.
81 5 ▶sub take_single : prototype() () {
Subroutine take_single: called CC=1 SCAR=0.0
Pod documented
82 4 ▶ return gather {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 4 my $seen = 0;
84  
85 # Without a signature, as it's called for each value.
86 5 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
87 5 ▶ return $_[0] unless $seen++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen++ 3 2 5
88 2 Carp::carp 'Expected only one item, but there were more';
89 2 Ekorn::Pipeline::Stop->throw;
90 4 };
91 5 };
92 }
93  
94  
95 # Return a stage passing on each value the test given is true of, and
96 # dropping the rest.
97 9 ▶sub take_if : prototype(&) ($test) {
Subroutine take_if: called CC=2 SCAR=6.9
Pod documented
98 9 ▶ croakf 'Expected a code reference' unless is_coderef($test);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($test) 1 8 9
99 return Ekorn::Pipeline::Stage->new(
100 8 ▶ sub ($value) { $test->($value) ? $value : () });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&$test($value) ? : 16 14 30
101 }
102  
103  
104 # Return a stage dropping each value the test given is true of, and
105 # passing on the rest.
106 4 ▶sub drop_if : prototype(&) ($test) {
Subroutine drop_if: called CC=2 SCAR=6.9
Pod documented
107 4 ▶ croakf 'Expected a code reference' unless is_coderef($test);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($test) 1 3 4
108 return Ekorn::Pipeline::Stage->new(
109 3 ▶ sub ($value) { $test->($value) ? () : $value });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&$test($value) ? : 7 6 13
110 }
111  
112  
113 # Return a stage passing on values while the test given is true of them,
114 # and stopping the pipeline at the first it's false of.
115 6 ▶sub take_while : prototype(&) ($test) {
Subroutine take_while: called CC=2 SCAR=6.9
Pod documented
116 6 ▶ croakf 'Expected a code reference' unless is_coderef($test);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($test) 1 5 6
117 49 ▶ return Ekorn::Pipeline::Stage->new(sub ($value) {
Subroutine __ANON__: called CC=2 SCAR=6.9
118 49 ▶ return $value if $test->($value);
Branch: 1 branch
BranchTrueFalseTotal
if &$test($value) 38 11 49
119 11 Ekorn::Pipeline::Stop->throw;
120 5 });
121 }
122  
123 # Return a stage dropping values while the test given is true of them,
124 # and passing on the rest, from the first it's false of.
125 3 ▶sub drop_while : prototype(&) ($test) {
Subroutine drop_while: called CC=2 SCAR=6.9
Pod documented
126 3 ▶ croakf 'Expected a code reference' unless is_coderef($test);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($test) 1 2 3
127 2 ▶ return gather {
Subroutine __ANON__: called CC=1 SCAR=0.0
128 2 my $dropping = 1;
129 9 ▶ return sub ($value) {
Subroutine __ANON__: called CC=3 SCAR=11.0
130 9 ▶ $dropping &&= !!$test->($value);
Condition100%3/3
$dropping &&= !!&$test($value)
!ll&&!rl&&r
423
MC/DC100%2/2
$dropping &&= !!&$test($value)
$dropping !!&$test($value)
Truth table100%3/3
A &&= B
A= $droppingB= !!&$test($value)
ABresult
0X0
100
111
131 9 ▶ return $dropping ? () : $value;
Branch: 1 branch
BranchTrueFalseTotal
$dropping ? : 3 6 9
132 2 };
133 2 };
134 }
135  
136  
137 89 ▶sub _check_count ($count) {
Subroutine _check_count: called CC=3 SCAR=11.0
138 89 ▶ croakf 'Expected a count of 0 or more'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and $count =~ / \A \d+ \z /ax 14 75 89
Condition100%3/3
defined $count and $count =~ / \A \d+ \z /ax
!ll&&!rl&&r
21275
MC/DC100%2/2
defined $count and $count =~ / \A \d+ \z /ax
defined $count $count =~ / \A \d+ \z /ax
Truth table100%3/3
A and B
A= defined $countB= $count =~ / \A \d+ \z /ax
ABresult
0X0
100
111
139 unless defined $count && $count =~ m( \A \d+ \z )xa;
140 75 return;
141 }
142  
143 15 1;
144 __END__
« Pipeline/Stages/Batch.pm ↑ Summary Pipeline/Sinks.pm »