| 1 | package Async::Stream; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | no warnings qw(ambiguous); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Async::Stream::Item; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Async::Stream::Iterator; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | Async::Stream - it's convenient way to work with async data flow. | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | Version 0.11 | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 5 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | Module helps to organize your async code to stream. | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | use Async::Stream; | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | my @urls = qw( | ||||||||||||||||||||||||||||||||
| 33 | http://ucoz.com | ||||||||||||||||||||||||||||||||
| 34 | http://ya.ru | ||||||||||||||||||||||||||||||||
| 35 | http://google.com | ||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | my $stream = Async::Stream::FromArray->new(@urls); | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | $stream | ||||||||||||||||||||||||||||||||
| 41 | ->transform(sub { | ||||||||||||||||||||||||||||||||
| 42 | $return_cb = shift; | ||||||||||||||||||||||||||||||||
| 43 | http_get $_, sub { | ||||||||||||||||||||||||||||||||
| 44 | $return_cb->({headers => $_[0], body => $_[0]}) | ||||||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||||||
| 46 | }) | ||||||||||||||||||||||||||||||||
| 47 | ->grep(sub { $_->{headers}->{Status} =~ /^2/ }) | ||||||||||||||||||||||||||||||||
| 48 | ->each(sub { | ||||||||||||||||||||||||||||||||
| 49 | print $_->{body}; | ||||||||||||||||||||||||||||||||
| 50 | }); | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | =head2 new($generator) | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | Constructor creates instance of class. | ||||||||||||||||||||||||||||||||
| 57 | Class method gets 1 arguments - generator subroutine references to generate items. | ||||||||||||||||||||||||||||||||
| 58 | Generator will get a callback which it will use for returning result. | ||||||||||||||||||||||||||||||||
| 59 | If generator is exhausted then returning callback is called without arguments. | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | my $i = 0; | ||||||||||||||||||||||||||||||||
| 62 | my $stream = Async::Stream->new(sub { | ||||||||||||||||||||||||||||||||
| 63 | $return_cb = shift; | ||||||||||||||||||||||||||||||||
| 64 | if ($i < 10) { | ||||||||||||||||||||||||||||||||
| 65 | $return_cb->($i++); | ||||||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||||||
| 67 | $return_cb->(); | ||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | }); | ||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||
| 71 | 45 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 72 | 45 | my $class = shift; | |||||||||||||||||||||||||||||||
| 73 | 45 | my $generator = shift; | |||||||||||||||||||||||||||||||
| 74 | 45 | my %args = @_; | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 45 | ▶ | if (ref $generator ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 77 | 1 | croak 'First argument can be only subroutine reference'; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | ▶ | elsif ($args{prefetch} && $args{prefetch} < 0) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{"prefetch"} and $args{"prefetch"} < 0
MC/DC0%0/2
$args{"prefetch"} and $args{"prefetch"} < 0
$args{"prefetch"}
$args{"prefetch"} < 0
Truth table66%2/3
A and B
A= $args{"prefetch"}B= $args{"prefetch"} < 0
| |||||||||||||||||||||||||||||||||
| 80 | 0 | croak 'Prefetch can\'t be less then zero'; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | my $self = bless { | ||||||||||||||||||||||||||||||||
| 84 | _head => undef, | ||||||||||||||||||||||||||||||||
| 85 | 44 | ▶ | _prefetch => int($args{prefetch} // 0), | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"prefetch"} // 0
MC/DC100%1/1
$args{"prefetch"} // 0
$args{"prefetch"}
Truth table100%2/2
A
A= $args{"prefetch"}
| |||||||||||||||||||||||||||||||||
| 86 | }, $class; | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 44 | $self->_set_head($generator); | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 44 | return $self; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | =head2 head() | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | Method returns stream's head item. | ||||||||||||||||||||||||||||||||
| 96 | Head is a instance of class Async::Stream::Item. | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | my $stream_head = $stream->head; | ||||||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||||||
| 100 | 72 | ▶ | sub head { | ||||||||||||||||||||||||||||||
|
Subroutine
head: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 101 | 72 | return $_[0]->{_head}; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | =head2 set_prefetch($number) | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | Method returns stream's head item. | ||||||||||||||||||||||||||||||||
| 107 | Head is a instance of class Async::Stream::Item. | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | $stream->set_prefetch(4); | ||||||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub set_prefetch { | ||||||||||||||||||||||||||||||
|
Subroutine
set_prefetch: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 112 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | my $prefetch = shift // 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
CORE::shift() // 0
MC/DC0%0/1
CORE::shift() // 0
CORE::shift()
Truth table50%1/2
A
A= CORE::shift()
| |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | if ($prefetch < 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 0 | croak 'Prefetch can\'t be less then zero'; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 1 | $self->{_prefetch} = $prefetch; | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | =head2 iterator() | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | Method returns stream's iterator. | ||||||||||||||||||||||||||||||||
| 128 | Iterator is a instance of class Async::Stream::Iterator. | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | my $stream_iterator = $stream->iterator; | ||||||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 63 | ▶ | sub iterator { | ||||||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 134 | 63 | return Async::Stream::Iterator->new( $_[0]->head ); | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | =head2 shift($return_cb) | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | Remove first item from stream and return it to return callback | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | $stream->shift(sub { | ||||||||||||||||||||||||||||||||
| 142 | if (@_){ | ||||||||||||||||||||||||||||||||
| 143 | my $item = shift; | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | ... | ||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||||||
| 149 | 7 | ▶ | sub shift { | ||||||||||||||||||||||||||||||
|
Subroutine
shift: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 150 | 7 | my ($self, $return_cb) = @_; | |||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 7 | my $head = $self->head; | |||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | $head->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 154 | 7 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 155 | 6 | $self->{_head} = $_[0]; | |||||||||||||||||||||||||||||||
| 156 | 6 | $return_cb->($_[0]->val); | |||||||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||||||
| 158 | 1 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | 7 | }); | |||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 7 | return $self; | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | =head1 CONVEYOR METHODS | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | =head2 peek($action) | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | This method helps to debug streams data flow. | ||||||||||||||||||||||||||||||||
| 171 | You can use this method for printing or logging steam data and track data | ||||||||||||||||||||||||||||||||
| 172 | mutation between stream's transformations. | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | $stream->peek(sub { print $_, "\n" })->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||||||
| 176 | 2 | ▶ | sub peek { | ||||||||||||||||||||||||||||||
|
Subroutine
peek: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 177 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 178 | 2 | my $action = shift; | |||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | if (ref $action ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 185 | 4 | ▶ | my $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 186 | 4 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 187 | 4 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 188 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 189 | 3 | $action->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 190 | 3 | $return_cb->($_[0]); | |||||||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||||||
| 192 | 1 | $return_cb->() | |||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||
| 194 | 4 | }); | |||||||||||||||||||||||||||||||
| 195 | 1 | }; | |||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 1 | $self->_set_head($generator, prefetch => 0); | |||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | =head2 grep($predicate) | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | The method greps current stream. Filter works like lazy grep. | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | $stream->grep(sub {$_ % 2})->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||||||
| 209 | 2 | ▶ | sub grep { | ||||||||||||||||||||||||||||||
|
Subroutine
grep: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 210 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 211 | 2 | my $predicate = shift; | |||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 2 | ▶ | if (ref $predicate ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 214 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 1 | ▶ | my $next_cb; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 220 | 4 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 221 | 4 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 222 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 223 | 3 | my $is_valid; | |||||||||||||||||||||||||||||||
| 224 | 3 | $is_valid = $predicate->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 225 | 3 | ▶ | if ($is_valid) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 2 | $return_cb->($_[0]); | |||||||||||||||||||||||||||||||
| 227 | } else { | ||||||||||||||||||||||||||||||||
| 228 | 1 | $next_cb->($return_cb); | |||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | } else { | ||||||||||||||||||||||||||||||||
| 231 | 1 | $return_cb->() | |||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | 4 | }); | |||||||||||||||||||||||||||||||
| 234 | 1 | }; | |||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 1 | $self->_set_head($next_cb, prefetch => 0); | |||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | =head2 map($transformer) | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | Method makes synchronous transformation for stream, like usual map for array. | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | $stream->map(sub { $_ * 2 })->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||||||||||||||
| 248 | 1 | ▶ | sub map { | ||||||||||||||||||||||||||||||
|
Subroutine
map: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 249 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 250 | 1 | my $transformer = shift; | |||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | 1 | ▶ | if (ref $transformer ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 253 | 0 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 1 | ▶ | my $next_cb; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 259 | 4 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 260 | 4 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 261 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 262 | 3 | $return_cb->($transformer->()) for ($_[0]); | |||||||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||||||
| 264 | 1 | $return_cb->() | |||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | 4 | }); | |||||||||||||||||||||||||||||||
| 267 | 1 | }; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 1 | $self->_set_head($next_cb, prefetch => 0); | |||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | =head2 transform($transformer) | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | Method transform current stream. | ||||||||||||||||||||||||||||||||
| 277 | Transform works like lazy map with async response. | ||||||||||||||||||||||||||||||||
| 278 | You can use the method for example for async http request or another async | ||||||||||||||||||||||||||||||||
| 279 | operation. | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | $stream->transform(sub { | ||||||||||||||||||||||||||||||||
| 282 | $return_cb = shift; | ||||||||||||||||||||||||||||||||
| 283 | $return_cb->($_ * 2) | ||||||||||||||||||||||||||||||||
| 284 | })->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||||||
| 287 | 4 | ▶ | sub transform { | ||||||||||||||||||||||||||||||
|
Subroutine
transform: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 288 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 289 | 4 | my $transformer = shift; | |||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 4 | ▶ | if (ref $transformer ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 292 | 2 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 2 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 2 | ▶ | my $next_cb; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 298 | 10 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 299 | 10 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 300 | 10 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 301 | 7 | $transformer->($return_cb) for ($_[0]); | |||||||||||||||||||||||||||||||
| 302 | } else { | ||||||||||||||||||||||||||||||||
| 303 | 3 | $return_cb->() | |||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | 10 | }); | |||||||||||||||||||||||||||||||
| 306 | 2 | }; | |||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 2 | $self->_set_head($next_cb); | |||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | =head2 append(@list_streams) | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | The method appends one or several streams to tail of current stream. | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | $stream->append($stream1)->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 318 | =cut | ||||||||||||||||||||||||||||||||
| 319 | 2 | ▶ | sub append { | ||||||||||||||||||||||||||||||
|
Subroutine
append: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 320 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 321 | 2 | my @streams = @_; | |||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 2 | for my $stream (@streams) { | |||||||||||||||||||||||||||||||
| 324 | 2 | ▶ | if (!$stream->isa('Async::Stream')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 325 | 1 | croak 'Arguments can be only Async::Stream or instances of derived class' | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | 1 | ▶ | my $generator; $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 332 | 8 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 333 | 8 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 334 | 8 | ▶ | if (@_){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 335 | 6 | $return_cb->($_[0]); | |||||||||||||||||||||||||||||||
| 336 | } elsif (@streams) { | ||||||||||||||||||||||||||||||||
| 337 | 1 | $iterator = (shift @streams)->iterator; | |||||||||||||||||||||||||||||||
| 338 | 1 | $generator->($return_cb); | |||||||||||||||||||||||||||||||
| 339 | } else { | ||||||||||||||||||||||||||||||||
| 340 | 1 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||
| 342 | 8 | }); | |||||||||||||||||||||||||||||||
| 343 | 1 | }; | |||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | 1 | $self->_set_head($generator, prefetch => 0); | |||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | =head2 skip($number) | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | The method skips $number items in stream. | ||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | $stream->skip(5)->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 355 | =cut | ||||||||||||||||||||||||||||||||
| 356 | 3 | ▶ | sub skip { | ||||||||||||||||||||||||||||||
|
Subroutine
skip: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 357 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 358 | 3 | my $skip = int shift; | |||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | 3 | ▶ | croak 'First argument can be only non-negative integer' if ($skip < 0); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | 2 | ▶ | if ($skip) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 363 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 364 | 1 | ▶ | my $generator; $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 365 | 4 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 366 | 4 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 367 | 4 | ▶ | if (@_){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 368 | 3 | ▶ | if ($skip-- > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 369 | 1 | $generator->($return_cb); | |||||||||||||||||||||||||||||||
| 370 | } else { | ||||||||||||||||||||||||||||||||
| 371 | 2 | $return_cb->($_[0]); | |||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||
| 373 | } else { | ||||||||||||||||||||||||||||||||
| 374 | 1 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||
| 376 | 4 | }); | |||||||||||||||||||||||||||||||
| 377 | 1 | }; | |||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | 1 | $self->_set_head($generator, prefetch => 0); | |||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||
| 381 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | =head2 limit($number) | ||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | The method limits $number items in stream. | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | $stream->limit(5)->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 392 | =cut | ||||||||||||||||||||||||||||||||
| 393 | 4 | ▶ | sub limit { | ||||||||||||||||||||||||||||||
|
Subroutine
limit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 394 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 395 | 4 | my $limit = int shift; | |||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | 4 | ▶ | croak 'First argument can be only non-negative integer' if ($limit < 0); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | 3 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | $self->_set_head( | ||||||||||||||||||||||||||||||||
| 402 | 4 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 403 | 4 | my ($return_cb) = @_; | |||||||||||||||||||||||||||||||
| 404 | 4 | ▶ | return $return_cb->() if ($limit-- <= 0); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 405 | 1 | $iterator->next($return_cb); | |||||||||||||||||||||||||||||||
| 406 | }, | ||||||||||||||||||||||||||||||||
| 407 | 3 | prefetch => 0 | |||||||||||||||||||||||||||||||
| 408 | ); | ||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | =head2 spray($number) | ||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||
| 415 | The method helps to divide items of stream to several items. | ||||||||||||||||||||||||||||||||
| 416 | For example you can use this method to get some page, | ||||||||||||||||||||||||||||||||
| 417 | then get all link from that page and make from these link another items. | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | $stream->spray(sub{ return (1 .. $_) })->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 420 | =cut | ||||||||||||||||||||||||||||||||
| 421 | 2 | ▶ | sub spray { | ||||||||||||||||||||||||||||||
|
Subroutine
spray: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 422 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 423 | 2 | my $splitter = shift; | |||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | 2 | ▶ | if (ref $splitter ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 426 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||
| 429 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||
| 431 | 1 | my @buffer; | |||||||||||||||||||||||||||||||
| 432 | my $generator; | ||||||||||||||||||||||||||||||||
| 433 | 9 | ▶ | $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 434 | 9 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 435 | 9 | ▶ | if (@buffer) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 436 | 4 | $return_cb->(shift @buffer); | |||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||
| 438 | else { | ||||||||||||||||||||||||||||||||
| 439 | 5 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 440 | 5 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 441 | 4 | @buffer = $splitter->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 442 | 4 | ▶ | if (@buffer) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 443 | 2 | $return_cb->(shift @buffer); | |||||||||||||||||||||||||||||||
| 444 | } else { | ||||||||||||||||||||||||||||||||
| 445 | 2 | $generator->($return_cb); | |||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||
| 447 | } else { | ||||||||||||||||||||||||||||||||
| 448 | 1 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||
| 450 | 5 | return; | |||||||||||||||||||||||||||||||
| 451 | 5 | }); | |||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||
| 453 | 9 | return; | |||||||||||||||||||||||||||||||
| 454 | 1 | }; | |||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | 1 | $self->_set_head($generator, prefetch => 0); | |||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||
| 458 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | =head2 sort($comparator) | ||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||
| 464 | The method sorts whole stream. | ||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | $stream->sort(sub{$a <=> $b})->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 467 | =cut | ||||||||||||||||||||||||||||||||
| 468 | 2 | ▶ | sub sort { | ||||||||||||||||||||||||||||||
|
Subroutine
sort: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 469 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 470 | 2 | my $comporator = shift; | |||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||
| 472 | 2 | ▶ | if (ref $comporator ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 473 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||
| 476 | 1 | my $pkg = caller; | |||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | 1 | my $is_sorted = 0; | |||||||||||||||||||||||||||||||
| 479 | 1 | my @stream_items; | |||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||
| 481 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||
| 483 | 4 | ▶ | my $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 484 | 4 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 485 | 4 | ▶ | if ($is_sorted) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 486 | 3 | ▶ | $return_cb->( @stream_items ? shift @stream_items : () ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 487 | } else { | ||||||||||||||||||||||||||||||||
| 488 | 1 | ▶ | my $next_cb; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 489 | 4 | my $next_cb = $next_cb; | |||||||||||||||||||||||||||||||
| 490 | 4 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 491 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 492 | 3 | push @stream_items, $_[0]; | |||||||||||||||||||||||||||||||
| 493 | 3 | $next_cb->(); | |||||||||||||||||||||||||||||||
| 494 | } else { | ||||||||||||||||||||||||||||||||
| 495 | 1 | ▶ | if (@stream_items) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 496 | { | ||||||||||||||||||||||||||||||||
| 497 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 498 | 1 | local *{ $pkg . '::a' } = *{ __PACKAGE__ . '::a' }; | |||||||||||||||||||||||||||||||
| 499 | 1 | local *{ $pkg . '::b' } = *{ __PACKAGE__ . '::b' }; | |||||||||||||||||||||||||||||||
| 500 | 1 | @stream_items = sort $comporator @stream_items; | |||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||
| 502 | 1 | $is_sorted = 1; | |||||||||||||||||||||||||||||||
| 503 | 1 | $return_cb->(shift @stream_items); | |||||||||||||||||||||||||||||||
| 504 | } else { | ||||||||||||||||||||||||||||||||
| 505 | 0 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||
| 508 | 4 | }); | |||||||||||||||||||||||||||||||
| 509 | 1 | };$next_cb->(); | |||||||||||||||||||||||||||||||
| 510 | 1 | weaken $next_cb; | |||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||
| 512 | 1 | }; | |||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||
| 514 | 1 | $self->_set_head($generator, prefetch => 0); | |||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||
| 519 | =head2 cut_sort($predicate, $comparator) | ||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||
| 521 | Sometimes stream can be infinity and you can't you $stream->sort, | ||||||||||||||||||||||||||||||||
| 522 | you need certain parts of streams for example cut part by length of items. | ||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||
| 524 | $stream | ||||||||||||||||||||||||||||||||
| 525 | ->cut_sort(sub {length $a != length $b},sub {$a <=> $b}) | ||||||||||||||||||||||||||||||||
| 526 | ->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 527 | =cut | ||||||||||||||||||||||||||||||||
| 528 | 5 | ▶ | sub cut_sort { | ||||||||||||||||||||||||||||||
|
Subroutine
cut_sort: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 529 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||
| 530 | 5 | my $cut = shift; | |||||||||||||||||||||||||||||||
| 531 | 5 | my $comporator = shift; | |||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | 5 | ▶ | if (ref $cut ne 'CODE' or ref $comporator ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $cut ne "CODE" or ref $comporator ne "CODE"
MC/DC100%2/2
ref $cut ne "CODE" or ref $comporator ne "CODE"
ref $cut ne "CODE"
ref $comporator ne "CODE"
Truth table100%3/3
A or B
A= ref $cut ne "CODE"B= ref $comporator ne "CODE"
| |||||||||||||||||||||||||||||||||
| 534 | 3 | croak 'First and Second arguments can be only subrotine references' | |||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||
| 537 | 2 | my $pkg = caller; | |||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||
| 539 | 2 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | 2 | my $prev; | |||||||||||||||||||||||||||||||
| 542 | my @cur_slice; | ||||||||||||||||||||||||||||||||
| 543 | 2 | my @sorted_array; | |||||||||||||||||||||||||||||||
| 544 | 2 | ▶ | my $generator; $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 545 | 12 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 546 | 12 | ▶ | if (@sorted_array) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 547 | 3 | $return_cb->(shift @sorted_array); | |||||||||||||||||||||||||||||||
| 548 | } else { | ||||||||||||||||||||||||||||||||
| 549 | 9 | ▶ | if (!defined $prev) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 550 | 2 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 551 | 2 | ▶ | if (@_){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 552 | 1 | $prev = $_[0]; | |||||||||||||||||||||||||||||||
| 553 | 1 | @cur_slice = ($prev); | |||||||||||||||||||||||||||||||
| 554 | 1 | $generator->($return_cb); | |||||||||||||||||||||||||||||||
| 555 | } else { | ||||||||||||||||||||||||||||||||
| 556 | 1 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||
| 558 | 2 | }); | |||||||||||||||||||||||||||||||
| 559 | } else { | ||||||||||||||||||||||||||||||||
| 560 | 7 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 561 | 7 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 562 | 5 | my $is_cut; | |||||||||||||||||||||||||||||||
| 563 | { | ||||||||||||||||||||||||||||||||
| 564 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 565 | 5 | local *{ $pkg . '::a' } = \$prev; | |||||||||||||||||||||||||||||||
| 566 | 5 | local *{ $pkg . '::b' } = \$_[0]; | |||||||||||||||||||||||||||||||
| 567 | 5 | $is_cut = $cut->(); | |||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||
| 569 | 5 | $prev = $_[0]; | |||||||||||||||||||||||||||||||
| 570 | 5 | ▶ | if ($is_cut) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 571 | { | ||||||||||||||||||||||||||||||||
| 572 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 573 | 2 | local *{ $pkg . '::a' } = *{ __PACKAGE__ . '::a' }; | |||||||||||||||||||||||||||||||
| 574 | 2 | local *{ $pkg . '::b' } = *{ __PACKAGE__ . '::b' }; | |||||||||||||||||||||||||||||||
| 575 | 2 | @sorted_array = sort $comporator @cur_slice; | |||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||||
| 577 | 2 | @cur_slice = ($prev); | |||||||||||||||||||||||||||||||
| 578 | 2 | $return_cb->(shift @sorted_array); | |||||||||||||||||||||||||||||||
| 579 | } else { | ||||||||||||||||||||||||||||||||
| 580 | 3 | push @cur_slice, $prev; | |||||||||||||||||||||||||||||||
| 581 | 3 | $generator->($return_cb); | |||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||
| 583 | } else { | ||||||||||||||||||||||||||||||||
| 584 | 2 | ▶ | if (@cur_slice) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 585 | { | ||||||||||||||||||||||||||||||||
| 586 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 587 | 1 | local *{ $pkg . '::a' } = *{ __PACKAGE__ . '::a' }; | |||||||||||||||||||||||||||||||
| 588 | 1 | local *{ $pkg . '::b' } = *{ __PACKAGE__ . '::b' }; | |||||||||||||||||||||||||||||||
| 589 | 1 | @sorted_array = sort $comporator @cur_slice; | |||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||
| 591 | 1 | @cur_slice = (); | |||||||||||||||||||||||||||||||
| 592 | 1 | $return_cb->(shift @sorted_array); | |||||||||||||||||||||||||||||||
| 593 | } else { | ||||||||||||||||||||||||||||||||
| 594 | 1 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||
| 597 | 7 | }); | |||||||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||
| 600 | 2 | }; | |||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||
| 602 | 2 | $self->_set_head($generator, prefetch => 0); | |||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||
| 604 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||
| 607 | =head2 reverse() | ||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||
| 609 | Revers order of stream's items. Can't be done on endless stream. | ||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||
| 611 | $stream->reverse; | ||||||||||||||||||||||||||||||||
| 612 | =cut | ||||||||||||||||||||||||||||||||
| 613 | 1 | ▶ | sub reverse { | ||||||||||||||||||||||||||||||
|
Subroutine
reverse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 614 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||
| 616 | 1 | my $is_received = 0; | |||||||||||||||||||||||||||||||
| 617 | 1 | my @stream_items; | |||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||
| 619 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||
| 621 | 4 | ▶ | my $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 622 | 4 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 623 | 4 | ▶ | if ($is_received) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 624 | 3 | ▶ | $return_cb->( @stream_items ? pop @stream_items : () ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 625 | } else { | ||||||||||||||||||||||||||||||||
| 626 | 1 | ▶ | my $next_cb; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 627 | 4 | my $next_cb = $next_cb; | |||||||||||||||||||||||||||||||
| 628 | 4 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||
| 629 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 630 | 3 | push @stream_items, $_[0]; | |||||||||||||||||||||||||||||||
| 631 | 3 | $next_cb->(); | |||||||||||||||||||||||||||||||
| 632 | } else { | ||||||||||||||||||||||||||||||||
| 633 | 1 | ▶ | if (@stream_items) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 634 | 1 | $is_received = 1; | |||||||||||||||||||||||||||||||
| 635 | 1 | $return_cb->(pop @stream_items); | |||||||||||||||||||||||||||||||
| 636 | } else { | ||||||||||||||||||||||||||||||||
| 637 | 0 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||||||
| 640 | 4 | }); | |||||||||||||||||||||||||||||||
| 641 | 1 | };$next_cb->(); | |||||||||||||||||||||||||||||||
| 642 | 1 | weaken $next_cb; | |||||||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||||||
| 644 | 1 | }; | |||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||
| 646 | 1 | $self->_set_head($generator, prefetch => 0); | |||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||
| 648 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 649 | } | ||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||
| 651 | =head2 merge_in($comparator, @list_streams); | ||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||
| 653 | Merge additional streams into current stream by comparing each item of stream. | ||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||
| 655 | $stream->merge_in(sub{$a <=> $b}, $stream1, $stream2); | ||||||||||||||||||||||||||||||||
| 656 | =cut | ||||||||||||||||||||||||||||||||
| 657 | 3 | ▶ | sub merge_in { | ||||||||||||||||||||||||||||||
|
Subroutine
merge_in: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 658 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 659 | 3 | my $comporator = shift; | |||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||
| 661 | 3 | ▶ | if (ref $comporator ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 662 | 1 | croak 'First argument can be only reference to subroutine'; | |||||||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||
| 665 | 2 | my $pkg = caller; | |||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||
| 667 | 2 | my @iterators; | |||||||||||||||||||||||||||||||
| 668 | 2 | for my $stream ($self, @_) { | |||||||||||||||||||||||||||||||
| 669 | 4 | ▶ | if ($stream->isa('Async::Stream')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 670 | 3 | push @iterators, [$stream->iterator]; | |||||||||||||||||||||||||||||||
| 671 | } else { | ||||||||||||||||||||||||||||||||
| 672 | 1 | croak 'Arguments can be only Async::Stream or instances of derived class' | |||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||
| 676 | 7 | ▶ | my $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 677 | 7 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 678 | 7 | my $requested_item = grep { @{$_} == 1 } @iterators; | |||||||||||||||||||||||||||||||
| 679 | 7 | ▶ | for (my $i = 0; $i < @iterators; $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 680 | 8 | ▶ | if (@{$iterators[$i]} == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 681 | 8 | my $iterator_id = $i; | |||||||||||||||||||||||||||||||
| 682 | 8 | ▶ | $iterators[$iterator_id][0]->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 683 | 8 | $requested_item--; | |||||||||||||||||||||||||||||||
| 684 | 8 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 685 | 6 | my $item = shift; | |||||||||||||||||||||||||||||||
| 686 | 6 | push @{$iterators[$iterator_id]}, $item; | |||||||||||||||||||||||||||||||
| 687 | } else { | ||||||||||||||||||||||||||||||||
| 688 | 2 | $iterators[$iterator_id] = undef; | |||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||
| 691 | 8 | ▶ | if ($requested_item == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 692 | ### it's awful and need to optimize ### | ||||||||||||||||||||||||||||||||
| 693 | { | ||||||||||||||||||||||||||||||||
| 694 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 695 | 5 | ▶ | my $comp = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 696 | 5 | local ${ $pkg . '::a' } = $a->[1]; | |||||||||||||||||||||||||||||||
| 697 | 5 | local ${ $pkg . '::b' } = $b->[1]; | |||||||||||||||||||||||||||||||
| 698 | 5 | return $comporator->(); | |||||||||||||||||||||||||||||||
| 699 | 7 | }; | |||||||||||||||||||||||||||||||
| 700 | 7 | @iterators = sort $comp grep { defined $_ } @iterators; | |||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||
| 702 | ### ### | ||||||||||||||||||||||||||||||||
| 703 | 7 | ▶ | if (@iterators) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 704 | 6 | my $item = pop @{ $iterators[0] }; | |||||||||||||||||||||||||||||||
| 705 | 6 | $return_cb->($item); | |||||||||||||||||||||||||||||||
| 706 | } else { | ||||||||||||||||||||||||||||||||
| 707 | 1 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||
| 710 | 8 | }); | |||||||||||||||||||||||||||||||
| 711 | } | ||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||
| 713 | 1 | }; | |||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||
| 715 | 1 | $self->_set_head($generator); | |||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||
| 717 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 718 | } | ||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||
| 720 | =head2 $branch_stream branch_out($predicat); | ||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||
| 722 | Method makes new branch of current stream by predicate. | ||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||
| 724 | my $error_stream = $stream->branch_out(sub{$_->{headers}{status} != 200}); | ||||||||||||||||||||||||||||||||
| 725 | =cut | ||||||||||||||||||||||||||||||||
| 726 | 2 | ▶ | sub branch_out { | ||||||||||||||||||||||||||||||
|
Subroutine
branch_out: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 727 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 728 | 2 | my $predicat = shift; | |||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||
| 730 | 2 | ▶ | if (ref $predicat ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 731 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 732 | } | ||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||
| 734 | 1 | my @branch_items; | |||||||||||||||||||||||||||||||
| 735 | my @self_items; | ||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||
| 737 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||
| 739 | 1 | ▶ | my $generator; $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||||||
| 740 | 11 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 741 | 11 | my $is_for_branch = shift; | |||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||
| 743 | 11 | ▶ | if ($is_for_branch && @branch_items) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$is_for_branch and @branch_items
MC/DC0%0/2
$is_for_branch and @branch_items
$is_for_branch
@branch_items
Truth table66%2/3
A and B
A= $is_for_branchB= @branch_items
| |||||||||||||||||||||||||||||||||
| 744 | 0 | return $return_cb->(shift @branch_items); | |||||||||||||||||||||||||||||||
| 745 | ▶ | } elsif (!$is_for_branch && @self_items) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
not $is_for_branch and @self_items
MC/DC100%2/2
not $is_for_branch and @self_items
not $is_for_branch
@self_items
Truth table100%3/3
not(A) and B
A= not $is_for_branchB= @self_items
| |||||||||||||||||||||||||||||||||
| 746 | 3 | return $return_cb->(shift @self_items); | |||||||||||||||||||||||||||||||
| 747 | } | ||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||
| 749 | 8 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.8
| |||||||||||||||||||||||||||||||||
| 750 | 8 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 751 | 6 | my $is_branch_item; | |||||||||||||||||||||||||||||||
| 752 | 6 | $is_branch_item = $predicat->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||
| 754 | 6 | ▶ | if ($is_for_branch && !$is_branch_item) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$is_for_branch and not $is_branch_item
MC/DC50%1/2
$is_for_branch and not $is_branch_item
$is_for_branch
not $is_branch_item
Truth table66%2/3
A and not(B)
A= $is_for_branchB= not $is_branch_item
| |||||||||||||||||||||||||||||||||
| 755 | 3 | push @self_items, $_[0]; | |||||||||||||||||||||||||||||||
| 756 | 3 | return $generator->($return_cb,$is_for_branch); | |||||||||||||||||||||||||||||||
| 757 | ▶ | } elsif (!$is_for_branch && $is_branch_item) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
not $is_for_branch and $is_branch_item
MC/DC0%0/2
not $is_for_branch and $is_branch_item
not $is_for_branch
$is_branch_item
Truth table33%1/3
not(A) and B
A= not $is_for_branchB= $is_branch_item
| |||||||||||||||||||||||||||||||||
| 758 | 0 | push @branch_items, $_[0]; | |||||||||||||||||||||||||||||||
| 759 | 0 | return $generator->($return_cb,$is_for_branch); | |||||||||||||||||||||||||||||||
| 760 | } else { | ||||||||||||||||||||||||||||||||
| 761 | 3 | return $return_cb->($_[0]); | |||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||
| 763 | } else { | ||||||||||||||||||||||||||||||||
| 764 | 2 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||
| 766 | 8 | }); | |||||||||||||||||||||||||||||||
| 767 | 1 | }; | |||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||
| 769 | 1 | ▶ | $self->_set_head(sub { $generator->($_[0], 0) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 770 | 1 | ▶ | return Async::Stream->new(sub { $generator->($_[0], 1) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 771 | } | ||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||
| 773 | =head2 distinct($key_generator) | ||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||
| 775 | Method discards duplicate items from stream. | ||||||||||||||||||||||||||||||||
| 776 | By default uniqueness of items will be determined by textual representation of item. | ||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||
| 778 | $stream->distinct(sub {$_->{name}})->to_arrayref(sub {print @{$_[0]}}); | ||||||||||||||||||||||||||||||||
| 779 | =cut | ||||||||||||||||||||||||||||||||
| 780 | 3 | ▶ | sub distinct { | ||||||||||||||||||||||||||||||
|
Subroutine
distinct: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 781 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 782 | 3 | my $to_key = shift; | |||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||
| 784 | 3 | ▶ | if (ref $to_key ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 785 | 1 | ▶ | $to_key = sub { "$_" }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 786 | } | ||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||
| 788 | 3 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||
| 790 | 3 | my %index_of; | |||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||
| 792 | 24 | ▶ | my $generator; $generator = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 793 | 24 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 794 | 24 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 795 | 24 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 796 | 21 | my $key; | |||||||||||||||||||||||||||||||
| 797 | 21 | $key = $to_key->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||
| 799 | 21 | ▶ | if (exists $index_of{$key}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 800 | 14 | $generator->($return_cb); | |||||||||||||||||||||||||||||||
| 801 | } else { | ||||||||||||||||||||||||||||||||
| 802 | 7 | $index_of{$key} = undef; | |||||||||||||||||||||||||||||||
| 803 | 7 | $return_cb->($_[0]); | |||||||||||||||||||||||||||||||
| 804 | } | ||||||||||||||||||||||||||||||||
| 805 | } else { | ||||||||||||||||||||||||||||||||
| 806 | 3 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 807 | } | ||||||||||||||||||||||||||||||||
| 808 | 24 | }); | |||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||
| 810 | 3 | }; | |||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||
| 812 | 3 | $self->_set_head($generator); | |||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||
| 814 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 815 | } | ||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||
| 817 | =head1 TERMINAL METHODS | ||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||
| 819 | =head2 to_arrayref($returing_cb) | ||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||
| 821 | Method returns stream's iterator. | ||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||
| 823 | $stream->to_arrayref(sub { | ||||||||||||||||||||||||||||||||
| 824 | $array_ref = shift; | ||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||
| 826 | #... | ||||||||||||||||||||||||||||||||
| 827 | }); | ||||||||||||||||||||||||||||||||
| 828 | =cut | ||||||||||||||||||||||||||||||||
| 829 | 25 | ▶ | sub to_arrayref { | ||||||||||||||||||||||||||||||
|
Subroutine
to_arrayref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 830 | 25 | my $self = shift; | |||||||||||||||||||||||||||||||
| 831 | 25 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||
| 833 | 25 | ▶ | if (ref $return_cb ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 834 | 0 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 835 | } | ||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||
| 837 | 25 | my @result; | |||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||
| 839 | 25 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||
| 841 | 25 | ▶ | my $next_cb; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 842 | 104 | my $next_cb = $next_cb; | |||||||||||||||||||||||||||||||
| 843 | 104 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 844 | 104 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 845 | 79 | push @result, $_[0]; | |||||||||||||||||||||||||||||||
| 846 | 79 | $next_cb->(); | |||||||||||||||||||||||||||||||
| 847 | } else { | ||||||||||||||||||||||||||||||||
| 848 | 25 | $return_cb->(\@result); | |||||||||||||||||||||||||||||||
| 849 | } | ||||||||||||||||||||||||||||||||
| 850 | 104 | }); | |||||||||||||||||||||||||||||||
| 851 | 25 | };$next_cb->(); | |||||||||||||||||||||||||||||||
| 852 | 25 | weaken $next_cb; | |||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||
| 854 | 25 | return $self; | |||||||||||||||||||||||||||||||
| 855 | } | ||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||
| 857 | =head2 each($action) | ||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||
| 859 | Method execute action on each item in stream. | ||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||
| 861 | $stream->each(sub { | ||||||||||||||||||||||||||||||||
| 862 | print $_, "\n"; | ||||||||||||||||||||||||||||||||
| 863 | }); | ||||||||||||||||||||||||||||||||
| 864 | =cut | ||||||||||||||||||||||||||||||||
| 865 | 3 | ▶ | sub each { | ||||||||||||||||||||||||||||||
|
Subroutine
each: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 866 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 867 | 3 | my $action = shift; | |||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||
| 869 | 3 | ▶ | if (ref $action ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 870 | 2 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 871 | } | ||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||
| 873 | 1 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||
| 875 | 1 | ▶ | my $each; $each = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 876 | 4 | my $each = $each; | |||||||||||||||||||||||||||||||
| 877 | 4 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 878 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 879 | 3 | $action->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 880 | 3 | $each->() | |||||||||||||||||||||||||||||||
| 881 | } | ||||||||||||||||||||||||||||||||
| 882 | 4 | }); | |||||||||||||||||||||||||||||||
| 883 | 1 | }; $each->(); | |||||||||||||||||||||||||||||||
| 884 | 1 | weaken $each; | |||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||||
| 886 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 887 | } | ||||||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||
| 890 | =head2 shift_each($action) | ||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||
| 892 | Method acts like each,but after process item, it removes them from the stream | ||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||
| 894 | $stream->shift_each(sub { | ||||||||||||||||||||||||||||||||
| 895 | print $_, "\n"; | ||||||||||||||||||||||||||||||||
| 896 | }); | ||||||||||||||||||||||||||||||||
| 897 | =cut | ||||||||||||||||||||||||||||||||
| 898 | 2 | ▶ | sub shift_each { | ||||||||||||||||||||||||||||||
|
Subroutine
shift_each: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 899 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 900 | 2 | my $action = shift; | |||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||
| 902 | 2 | ▶ | if (ref $action ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 903 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||
| 907 | 1 | ▶ | my $each; $each = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 908 | 4 | my $each = $each; | |||||||||||||||||||||||||||||||
| 909 | 4 | ▶ | $self->shift(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 910 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 911 | 3 | $action->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 912 | 3 | $each->() | |||||||||||||||||||||||||||||||
| 913 | } | ||||||||||||||||||||||||||||||||
| 914 | 4 | }); | |||||||||||||||||||||||||||||||
| 915 | 1 | }; $each->(); | |||||||||||||||||||||||||||||||
| 916 | 1 | weaken $each; | |||||||||||||||||||||||||||||||
| 917 | |||||||||||||||||||||||||||||||||
| 918 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 919 | } | ||||||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||||||
| 921 | =head2 reduce($accumulator, $returing_cb) | ||||||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||||||
| 923 | Performs a reduction on the items of the stream. | ||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||
| 925 | $stream->reduce( | ||||||||||||||||||||||||||||||||
| 926 | sub{ $a + $b }, | ||||||||||||||||||||||||||||||||
| 927 | sub { | ||||||||||||||||||||||||||||||||
| 928 | my $sum_of_items = shift; | ||||||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||||||
| 930 | ... | ||||||||||||||||||||||||||||||||
| 931 | }); | ||||||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||||||
| 933 | =cut | ||||||||||||||||||||||||||||||||
| 934 | 9 | ▶ | sub reduce { | ||||||||||||||||||||||||||||||
|
Subroutine
reduce: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 935 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||
| 936 | 9 | my $code = shift; | |||||||||||||||||||||||||||||||
| 937 | 9 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||||||
| 939 | 9 | ▶ | if (ref $return_cb ne 'CODE' or ref $code ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $return_cb ne "CODE" or ref $code ne "CODE"
MC/DC50%1/2
ref $return_cb ne "CODE" or ref $code ne "CODE"
ref $return_cb ne "CODE"
ref $code ne "CODE"
Truth table66%2/3
A or B
A= ref $return_cb ne "CODE"B= ref $code ne "CODE"
| |||||||||||||||||||||||||||||||||
| 940 | 1 | croak 'First and Second arguments can be only subroutine references' | |||||||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||
| 943 | 8 | my $pkg = caller; | |||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||
| 945 | 8 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||||||
| 947 | 8 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 948 | 8 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 949 | 8 | my $prev = $_[0]; | |||||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||||||
| 951 | 8 | ▶ | my $reduce_cb; $reduce_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 952 | 24 | my $reduce_cb = $reduce_cb; | |||||||||||||||||||||||||||||||
| 953 | 24 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 954 | 24 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 955 | { | ||||||||||||||||||||||||||||||||
| 956 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 957 | 16 | local *{ $pkg . '::a' } = \$prev; | |||||||||||||||||||||||||||||||
| 958 | 16 | local *{ $pkg . '::b' } = \$_[0]; | |||||||||||||||||||||||||||||||
| 959 | 16 | $prev = $code->(); | |||||||||||||||||||||||||||||||
| 960 | } | ||||||||||||||||||||||||||||||||
| 961 | 16 | $reduce_cb->(); | |||||||||||||||||||||||||||||||
| 962 | } else { | ||||||||||||||||||||||||||||||||
| 963 | 8 | $return_cb->($prev); | |||||||||||||||||||||||||||||||
| 964 | } | ||||||||||||||||||||||||||||||||
| 965 | 24 | }); | |||||||||||||||||||||||||||||||
| 966 | 8 | };$reduce_cb->(); | |||||||||||||||||||||||||||||||
| 967 | 8 | weaken $reduce_cb; | |||||||||||||||||||||||||||||||
| 968 | } else { | ||||||||||||||||||||||||||||||||
| 969 | 0 | $return_cb->(); | |||||||||||||||||||||||||||||||
| 970 | } | ||||||||||||||||||||||||||||||||
| 971 | 8 | }); | |||||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||||||
| 973 | 8 | return $self; | |||||||||||||||||||||||||||||||
| 974 | } | ||||||||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||||||
| 976 | =head2 sum($returing_cb) | ||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||
| 978 | The method computes sum of all items in stream. | ||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||
| 980 | $stream->sum( | ||||||||||||||||||||||||||||||||
| 981 | sub { | ||||||||||||||||||||||||||||||||
| 982 | my $sum_of_items = shift; | ||||||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||||||
| 984 | ... | ||||||||||||||||||||||||||||||||
| 985 | }); | ||||||||||||||||||||||||||||||||
| 986 | =cut | ||||||||||||||||||||||||||||||||
| 987 | 2 | ▶ | sub sum { | ||||||||||||||||||||||||||||||
|
Subroutine
sum: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 988 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 989 | 2 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 990 | |||||||||||||||||||||||||||||||||
| 991 | 2 | ▶ | if (ref $return_cb ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 992 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 993 | } | ||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||
| 995 | 1 | ▶ | $self->reduce(sub{$a+$b}, $return_cb); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||||
| 997 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 998 | } | ||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||
| 1000 | =head2 min($returing_cb) | ||||||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||||||
| 1002 | The method finds out minimum item among all items in stream. | ||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||
| 1004 | $stream->min( | ||||||||||||||||||||||||||||||||
| 1005 | sub { | ||||||||||||||||||||||||||||||||
| 1006 | my $min_item = shift; | ||||||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||||||
| 1008 | ... | ||||||||||||||||||||||||||||||||
| 1009 | }); | ||||||||||||||||||||||||||||||||
| 1010 | =cut | ||||||||||||||||||||||||||||||||
| 1011 | 3 | ▶ | sub min { | ||||||||||||||||||||||||||||||
|
Subroutine
min: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1012 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 1013 | 3 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||||||
| 1015 | 3 | ▶ | if (ref $return_cb ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1016 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 1017 | } | ||||||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||||||
| 1019 | 2 | ▶ | $self->reduce(sub{$a < $b ? $a : $b}, $return_cb); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1020 | |||||||||||||||||||||||||||||||||
| 1021 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 1022 | } | ||||||||||||||||||||||||||||||||
| 1023 | |||||||||||||||||||||||||||||||||
| 1024 | =head2 max($returing_cb) | ||||||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||||||
| 1026 | The method finds out maximum item among all items in stream. | ||||||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||||||
| 1028 | $stream->max( | ||||||||||||||||||||||||||||||||
| 1029 | sub { | ||||||||||||||||||||||||||||||||
| 1030 | my $max_item = shift; | ||||||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||||||
| 1032 | ... | ||||||||||||||||||||||||||||||||
| 1033 | }); | ||||||||||||||||||||||||||||||||
| 1034 | =cut | ||||||||||||||||||||||||||||||||
| 1035 | 3 | ▶ | sub max { | ||||||||||||||||||||||||||||||
|
Subroutine
max: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1036 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 1037 | 3 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 1038 | |||||||||||||||||||||||||||||||||
| 1039 | 3 | ▶ | if (ref $return_cb ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1040 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 1041 | } | ||||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||||
| 1043 | 2 | ▶ | $self->reduce(sub{$a > $b ? $a : $b}, $return_cb); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||||||
| 1045 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 1046 | } | ||||||||||||||||||||||||||||||||
| 1047 | |||||||||||||||||||||||||||||||||
| 1048 | =head2 count($returing_cb) | ||||||||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||||||
| 1050 | The method counts number items in streams. | ||||||||||||||||||||||||||||||||
| 1051 | |||||||||||||||||||||||||||||||||
| 1052 | $stream->count(sub { | ||||||||||||||||||||||||||||||||
| 1053 | my $count = shift; | ||||||||||||||||||||||||||||||||
| 1054 | }); | ||||||||||||||||||||||||||||||||
| 1055 | =cut | ||||||||||||||||||||||||||||||||
| 1056 | 4 | ▶ | sub count { | ||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1057 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 1058 | 4 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||||||
| 1060 | 4 | ▶ | if (ref $return_cb ne 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1061 | 1 | croak 'First argument can be only subroutine reference' | |||||||||||||||||||||||||||||||
| 1062 | } | ||||||||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||||||
| 1064 | 3 | my $result = 0; | |||||||||||||||||||||||||||||||
| 1065 | 3 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||||||
| 1067 | 3 | ▶ | my $next_cb ; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 1068 | 6 | my $next_cb = $next_cb; | |||||||||||||||||||||||||||||||
| 1069 | 6 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 1070 | 6 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1071 | 3 | $result++; | |||||||||||||||||||||||||||||||
| 1072 | 3 | return $next_cb->(); | |||||||||||||||||||||||||||||||
| 1073 | } | ||||||||||||||||||||||||||||||||
| 1074 | 3 | $return_cb->($result) | |||||||||||||||||||||||||||||||
| 1075 | 6 | }); | |||||||||||||||||||||||||||||||
| 1076 | 3 | }; $next_cb->(); | |||||||||||||||||||||||||||||||
| 1077 | 3 | weaken $next_cb; | |||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||
| 1079 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 1080 | } | ||||||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||||||
| 1082 | =head2 any($predicat, $return_cb) | ||||||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||||||
| 1084 | Method look for any equivalent item in steam. if there is any then return that. | ||||||||||||||||||||||||||||||||
| 1085 | if there isn't then return nothing. | ||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||
| 1087 | $stream->any(sub {$_ % 2}, sub{ | ||||||||||||||||||||||||||||||||
| 1088 | my $odd_item = shift | ||||||||||||||||||||||||||||||||
| 1089 | |||||||||||||||||||||||||||||||||
| 1090 | ... | ||||||||||||||||||||||||||||||||
| 1091 | }); | ||||||||||||||||||||||||||||||||
| 1092 | =cut | ||||||||||||||||||||||||||||||||
| 1093 | 2 | ▶ | sub any { | ||||||||||||||||||||||||||||||
|
Subroutine
any: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1094 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 1095 | 2 | my $predicat = shift; | |||||||||||||||||||||||||||||||
| 1096 | 2 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||
| 1098 | 2 | my $iterator = $self->iterator; | |||||||||||||||||||||||||||||||
| 1099 | |||||||||||||||||||||||||||||||||
| 1100 | 2 | ▶ | my $next_cb; $next_cb = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 1101 | 6 | my $next_cb = $next_cb; | |||||||||||||||||||||||||||||||
| 1102 | 6 | ▶ | $iterator->next(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 1103 | 6 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1104 | 5 | my $is_valid; | |||||||||||||||||||||||||||||||
| 1105 | 5 | $is_valid = $predicat->() for ($_[0]); | |||||||||||||||||||||||||||||||
| 1106 | 5 | ▶ | if ($is_valid) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1107 | 1 | $return_cb->($_[0]); | |||||||||||||||||||||||||||||||
| 1108 | } else { | ||||||||||||||||||||||||||||||||
| 1109 | 4 | $next_cb->(); | |||||||||||||||||||||||||||||||
| 1110 | } | ||||||||||||||||||||||||||||||||
| 1111 | } else { | ||||||||||||||||||||||||||||||||
| 1112 | 1 | $return_cb->() | |||||||||||||||||||||||||||||||
| 1113 | } | ||||||||||||||||||||||||||||||||
| 1114 | 6 | }); | |||||||||||||||||||||||||||||||
| 1115 | 2 | }; $next_cb->(); | |||||||||||||||||||||||||||||||
| 1116 | 2 | weaken $next_cb; | |||||||||||||||||||||||||||||||
| 1117 | |||||||||||||||||||||||||||||||||
| 1118 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 1119 | } | ||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||
| 1121 | |||||||||||||||||||||||||||||||||
| 1122 | 64 | ▶ | sub _set_head { | ||||||||||||||||||||||||||||||
|
Subroutine
_set_head: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 1123 | 64 | my $self = shift; | |||||||||||||||||||||||||||||||
| 1124 | 64 | my $generator = shift; | |||||||||||||||||||||||||||||||
| 1125 | 64 | my %args = @_; | |||||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||||||
| 1127 | 64 | ▶ | my $prefetch = $args{prefetch} // $self->{_prefetch}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{"prefetch"} // $self->{"_prefetch"}
MC/DC0%0/2
$args{"prefetch"} // $self->{"_prefetch"}
$args{"prefetch"}
$self->{"_prefetch"}
Truth table66%2/3
A // B
A= $args{"prefetch"}B= $self->{"_prefetch"}
| |||||||||||||||||||||||||||||||||
| 1128 | |||||||||||||||||||||||||||||||||
| 1129 | 64 | my $new_generator = $generator; | |||||||||||||||||||||||||||||||
| 1130 | |||||||||||||||||||||||||||||||||
| 1131 | 64 | ▶ | if ($prefetch) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1132 | 2 | $new_generator = _get_prefetch_generator($generator, $self->{_prefetch}); | |||||||||||||||||||||||||||||||
| 1133 | } | ||||||||||||||||||||||||||||||||
| 1134 | |||||||||||||||||||||||||||||||||
| 1135 | 64 | $self->{_head} = Async::Stream::Item->new(undef, $new_generator); | |||||||||||||||||||||||||||||||
| 1136 | |||||||||||||||||||||||||||||||||
| 1137 | 64 | return $self; | |||||||||||||||||||||||||||||||
| 1138 | } | ||||||||||||||||||||||||||||||||
| 1139 | |||||||||||||||||||||||||||||||||
| 1140 | 2 | ▶ | sub _get_prefetch_generator { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_prefetch_generator: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||
| 1141 | 2 | my ($generator,$prefetch) = @_; | |||||||||||||||||||||||||||||||
| 1142 | |||||||||||||||||||||||||||||||||
| 1143 | 2 | my @responses_cache; | |||||||||||||||||||||||||||||||
| 1144 | my @requests_queue; | ||||||||||||||||||||||||||||||||
| 1145 | 2 | my $is_exhausted = 0; | |||||||||||||||||||||||||||||||
| 1146 | 2 | my $item_requested = 0; | |||||||||||||||||||||||||||||||
| 1147 | |||||||||||||||||||||||||||||||||
| 1148 | 10 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||||||
| 1149 | 10 | my $return_cb = shift; | |||||||||||||||||||||||||||||||
| 1150 | |||||||||||||||||||||||||||||||||
| 1151 | 10 | ▶ | if (@responses_cache) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1152 | 0 | $return_cb->(shift @responses_cache); | |||||||||||||||||||||||||||||||
| 1153 | } else { | ||||||||||||||||||||||||||||||||
| 1154 | 10 | push @requests_queue, $return_cb; | |||||||||||||||||||||||||||||||
| 1155 | } | ||||||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||||||
| 1157 | 10 | ▶ | if (!$is_exhausted) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 1158 | 6 | for (0 .. ($prefetch - $item_requested)) { | |||||||||||||||||||||||||||||||
| 1159 | 18 | $item_requested++; | |||||||||||||||||||||||||||||||
| 1160 | 18 | ▶ | $generator->(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.3
| |||||||||||||||||||||||||||||||||
| 1161 | 18 | $item_requested--; | |||||||||||||||||||||||||||||||
| 1162 | 18 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1163 | 8 | ▶ | if (@requests_queue) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1164 | 8 | shift(@requests_queue)->($_[0]); | |||||||||||||||||||||||||||||||
| 1165 | } else { | ||||||||||||||||||||||||||||||||
| 1166 | 0 | push @responses_cache, $_[0]; | |||||||||||||||||||||||||||||||
| 1167 | } | ||||||||||||||||||||||||||||||||
| 1168 | } else { | ||||||||||||||||||||||||||||||||
| 1169 | 10 | $is_exhausted = 1; | |||||||||||||||||||||||||||||||
| 1170 | 10 | ▶ | if (!$item_requested && @requests_queue) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $item_requested and @requests_queue
MC/DC0%0/2
not $item_requested and @requests_queue
not $item_requested
@requests_queue
Truth table66%2/3
not(A) and B
A= not $item_requestedB= @requests_queue
| |||||||||||||||||||||||||||||||||
| 1171 | 0 | shift(@requests_queue)->(); | |||||||||||||||||||||||||||||||
| 1172 | } | ||||||||||||||||||||||||||||||||
| 1173 | } | ||||||||||||||||||||||||||||||||
| 1174 | 18 | }); | |||||||||||||||||||||||||||||||
| 1175 | } | ||||||||||||||||||||||||||||||||
| 1176 | ▶ | } elsif (!$item_requested && @requests_queue) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
not $item_requested and @requests_queue
MC/DC50%1/2
not $item_requested and @requests_queue
not $item_requested
@requests_queue
Truth table66%2/3
not(A) and B
A= not $item_requestedB= @requests_queue
| |||||||||||||||||||||||||||||||||
| 1177 | 2 | shift(@requests_queue)->(); | |||||||||||||||||||||||||||||||
| 1178 | } | ||||||||||||||||||||||||||||||||
| 1179 | 2 | }; | |||||||||||||||||||||||||||||||
| 1180 | } | ||||||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||||||
| 1182 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||||||
| 1184 | Kirill Sysoev, C<< <k.sysoev at me.com> >> | ||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||
| 1186 | =head1 BUGS AND LIMITATIONS | ||||||||||||||||||||||||||||||||
| 1187 | |||||||||||||||||||||||||||||||||
| 1188 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||||||
| 1189 | L<https://github.com/pestkam/p5-Async-Stream/issues>. | ||||||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||||||
| 1191 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||||||
| 1193 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||||||
| 1195 | perldoc Async::Stream::Item | ||||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||||
| 1197 | |||||||||||||||||||||||||||||||||
| 1198 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||||||
| 1200 | Copyright 2017 Kirill Sysoev. | ||||||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||||||
| 1202 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 1203 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||||||
| 1204 | copy of the full license at: | ||||||||||||||||||||||||||||||||
| 1205 | |||||||||||||||||||||||||||||||||
| 1206 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||||||
| 1207 | =cut | ||||||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||||||
| 1209 | 5 | 1; # End of Async::Stream | |||||||||||||||||||||||||||||||