| 1 | 13 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Ekorn::Pipeline::Stages::Flow; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Carp qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Ref::Util qw(is_coderef); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Ekorn::Pipeline::Errors qw(croakf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Ekorn::Pipeline::Gather qw(gather); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use Ekorn::Pipeline::Stage qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use Ekorn::Pipeline::Stop qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use namespace::clean -except => 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 13 | our @EXPORT = qw(take drop); ## no critic (Modules::ProhibitAutomaticExportation) | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 13 | our @EXPORT_OK = ( | |||||||||||||||||||||||||||
| 19 | @EXPORT, | ||||||||||||||||||||||||||||
| 20 | qw(take_first take_last take_single take_while drop_while take_if drop_if), | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | 13 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Croak at the line that called Ekorn::Pipeline. | ||||||||||||||||||||||||||||
| 25 | 13 | 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 | 58 | ▶ | sub take ($count) { | ||||||||||||||||||||||||||
|
Subroutine
take: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 58 | _check_count($count); | |||||||||||||||||||||||||||
| 31 | 89 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 89 | my $taken = 0; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 35 | 194 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 36 | 194 | ▶ | Ekorn::Pipeline::Stop->throw if $taken >= $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 192 | ▶ | return ++$taken < $count ? $_[0] : Ekorn::Pipeline::Stop->throw($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 89 | }; | |||||||||||||||||||||||||||
| 39 | 51 | }; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Return a stage dropping the first $count values, and passing on the | ||||||||||||||||||||||||||||
| 43 | # rest. | ||||||||||||||||||||||||||||
| 44 | 14 | ▶ | sub drop ($count) { | ||||||||||||||||||||||||||
|
Subroutine
drop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 14 | _check_count($count); | |||||||||||||||||||||||||||
| 46 | 42 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 42 | my $dropped = 0; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 50 | 233 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | 233 | ▶ | return $_[0] if $dropped >= $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 77 | $dropped++; | |||||||||||||||||||||||||||
| 53 | 77 | return; | |||||||||||||||||||||||||||
| 54 | 42 | }; | |||||||||||||||||||||||||||
| 55 | 7 | }; | |||||||||||||||||||||||||||
| 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 | 7 | ▶ | sub take_last : prototype() () { | ||||||||||||||||||||||||||
|
Subroutine
take_last: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 6 | my @kept; | |||||||||||||||||||||||||||
| 70 | return { | ||||||||||||||||||||||||||||
| 71 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 72 | 9 | ▶ | step => sub { @kept = ($_[0]); return }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 5 | ▶ | flush => sub () { splice @kept }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 6 | }; | |||||||||||||||||||||||||||
| 75 | 7 | }; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 7 | ▶ | sub take_if : prototype(&) ($test) { | ||||||||||||||||||||||||||
|
Subroutine
take_if: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 7 | ▶ | croakf 'Expected a code reference' unless is_coderef($test); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | return Ekorn::Pipeline::Stage->new( | ||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub ($value) { $test->($value) ? $value : () }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 108 | return Ekorn::Pipeline::Stage->new( | ||||||||||||||||||||||||||||
| 109 | 3 | ▶ | sub ($value) { $test->($value) ? () : $value }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 5 | ▶ | sub take_while : prototype(&) ($test) { | ||||||||||||||||||||||||||
|
Subroutine
take_while: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 5 | ▶ | croakf 'Expected a code reference' unless is_coderef($test); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 9 | ▶ | return Ekorn::Pipeline::Stage->new(sub ($value) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 9 | ▶ | return $value if $test->($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 3 | Ekorn::Pipeline::Stop->throw; | |||||||||||||||||||||||||||
| 120 | 4 | }); | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
MC/DC100%2/2
$dropping &&= !!&$test($value)
$dropping
!!&$test($value)
Truth table100%3/3
A &&= B
A= $droppingB= !!&$test($value)
| |||||||||||||||||||||||||||||
| 131 | 9 | ▶ | return $dropping ? () : $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 2 | }; | |||||||||||||||||||||||||||
| 133 | 2 | }; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 72 | ▶ | sub _check_count ($count) { | ||||||||||||||||||||||||||
|
Subroutine
_check_count: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 138 | 72 | ▶ | croakf 'Expected a count of 0 or more' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $count and $count =~ / \A \d+ \z /ax
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
| |||||||||||||||||||||||||||||
| 139 | unless defined $count && $count =~ m( \A \d+ \z )xa; | ||||||||||||||||||||||||||||
| 140 | 58 | return; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 13 | 1; | |||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||