Pipeline/Stages/Flow.pm

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