Pipeline/Stages/Flow.pm

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