| 1 | package Dallycot::Value::Stream; | ||||||||||||||||||||
| 2 | 30 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | # ABSTRACT: A linked list of values with a possible generator | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 30 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 30 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # RDF List | ||||||||||||||||||||
| 10 | 30 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 30 | ▶ | use Readonly; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 30 | Readonly my $HEAD => 0; | |||||||||||||||||||
| 14 | 30 | Readonly my $TAIL => 1; | |||||||||||||||||||
| 15 | 30 | Readonly my $TAIL_PROMISE => 2; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 30 | ▶ | use parent 'Dallycot::Value::Collection'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 30 | ▶ | use experimental qw(switch); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 30 | ▶ | use Promises qw(deferred); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | sub new { | ||||||||||||||||||||
| 24 | 383 | ▶ | my ( $class, $head, $tail, $promise ) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 25 | 383 | ▶ | $class = ref $class || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 26 | 383 | return bless [ $head, $tail, $promise ] => $class; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 175 | ▶ | sub is_defined { return 1 } | ||||||||||||||||||
|
Subroutine
is_defined: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 175 | ▶ | sub is_empty {return} | ||||||||||||||||||
|
Subroutine
is_empty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | sub to_rdf { | ||||||||||||||||||||
| 34 | 0 | ▶ | my($self, $model) = @_; | ||||||||||||||||||
|
Subroutine
to_rdf: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | my @things; | |||||||||||||||||||
| 37 | 0 | my $root = $self; | |||||||||||||||||||
| 38 | 0 | push @things, $root -> [0]->to_rdf($model); | |||||||||||||||||||
| 39 | 0 | ▶ | while($root -> [1]) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 0 | $root = $root->[1]; | |||||||||||||||||||
| 41 | 0 | push @things, $root->[0]->to_rdf($model); | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | 0 | ▶ | if($root -> [2]) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 0 | return $model -> list_with_promise(@things, $root->[2]); | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||
| 47 | 0 | return $model -> list(@things); | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | sub prepend { | ||||||||||||||||||||
| 52 | 0 | ▶ | my ( $self, @things ) = @_; | ||||||||||||||||||
|
Subroutine
prepend: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | my $stream = $self; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | foreach my $thing (@things) { | |||||||||||||||||||
| 57 | 0 | $stream = __PACKAGE__->new( $thing, $stream ); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 0 | return $stream; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub as_text { | ||||||||||||||||||||
| 63 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
as_text: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | my $text = "[ "; | |||||||||||||||||||
| 66 | 0 | my $point = $self; | |||||||||||||||||||
| 67 | 0 | $text .= $point->[$HEAD]->as_text; | |||||||||||||||||||
| 68 | 0 | ▶ | while ( defined $point->[$TAIL] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | $point = $point->[$TAIL]; | |||||||||||||||||||
| 70 | 0 | ▶ | if ( defined $point->[$HEAD] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | $text .= ", "; | |||||||||||||||||||
| 72 | 0 | $text .= $point->[$HEAD]->as_text; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 0 | ▶ | if ( defined $point->[$TAIL_PROMISE] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | $text .= ", ..."; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | 0 | return $text . " ]"; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | sub calculate_length { | ||||||||||||||||||||
| 82 | 2 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
calculate_length: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 2 | my $d = deferred; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | my $ptr = $self; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 2 | my $count = 1; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 2 | ▶ | while ( $ptr->[$TAIL] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 2 | $count++; | |||||||||||||||||||
| 92 | 2 | $ptr = $ptr->[$TAIL]; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 2 | ▶ | if ( $ptr->[$TAIL_PROMISE] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 1 | $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->binf() ) ); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||
| 99 | 1 | $d->resolve( Dallycot::Value::Numeric->new($count) ); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 2 | return $d->promise; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | sub _resolve_tail_promise { | ||||||||||||||||||||
| 106 | 240 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
_resolve_tail_promise: called CC=1 SCAR=0.6
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | return $self->[$TAIL_PROMISE]->apply( $engine, {} )->then( | ||||||||||||||||||||
| 109 | sub { | ||||||||||||||||||||
| 110 | 233 | ▶ | my ($list_tail) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.5
| |||||||||||||||||||||
| 111 | 233 | given ( ref $list_tail ) { | |||||||||||||||||||
| 112 | 233 | when (__PACKAGE__) { | |||||||||||||||||||
| 113 | 224 | $self->[$TAIL] = $list_tail; | |||||||||||||||||||
| 114 | 224 | $self->[$TAIL_PROMISE] = undef; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | 9 | when ('Dallycot::Value::Vector') { | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | # convert finite vector into linked list | ||||||||||||||||||||
| 119 | 0 | my @values = @$list_tail; | |||||||||||||||||||
| 120 | 0 | my $point = $self; | |||||||||||||||||||
| 121 | 0 | ▶ | while (@values) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 0 | $point->[$TAIL] = $self->new( shift @values ); | |||||||||||||||||||
| 123 | 0 | $point = $point->[$TAIL]; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | 9 | default { | |||||||||||||||||||
| 127 | 9 | $self->[$TAIL] = $list_tail; | |||||||||||||||||||
| 128 | 9 | $self->[$TAIL_PROMISE] = undef; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 240 | ); | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | sub apply_map { | ||||||||||||||||||||
| 136 | 5 | ▶ | my ( $self, $engine, $transform ) = @_; | ||||||||||||||||||
|
Subroutine
apply_map: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | return $engine->make_map($transform)->then( | ||||||||||||||||||||
| 139 | sub { | ||||||||||||||||||||
| 140 | 5 | ▶ | my ($map_t) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 5 | $map_t->apply( $engine, {}, $self ); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | 5 | ); | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | sub apply_filter { | ||||||||||||||||||||
| 148 | 18 | ▶ | my ( $self, $engine, $filter ) = @_; | ||||||||||||||||||
|
Subroutine
apply_filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | return $engine->make_filter($filter)->then( | ||||||||||||||||||||
| 151 | sub { | ||||||||||||||||||||
| 152 | 18 | ▶ | my ($filter_t) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 18 | $filter_t->apply( $engine, {}, $self ); | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | 18 | ); | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | sub drop { | ||||||||||||||||||||
| 160 | 0 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
drop: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | return; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | sub value_at { | ||||||||||||||||||||
| 166 | 10 | ▶ | my ( $self, $engine, $index ) = @_; | ||||||||||||||||||
|
Subroutine
value_at: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 10 | ▶ | if ( $index == 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | 2 | return $self->head($engine); | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 8 | my $d = deferred; | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 8 | ▶ | if ( $index < 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | 0 | $d->resolve( $engine->UNDEFINED ); | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||
| 178 | # we want to keep resolving tails until we get somewhere | ||||||||||||||||||||
| 179 | 8 | $self->_walk_tail( $engine, $d, $index - 1 ); | |||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 8 | return $d->promise; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | sub _walk_tail { | ||||||||||||||||||||
| 186 | 36 | ▶ | my ( $self, $engine, $d, $count ) = @_; | ||||||||||||||||||
|
Subroutine
_walk_tail: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 36 | ▶ | if ( $count > 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | $self->tail($engine)->done( | ||||||||||||||||||||
| 190 | sub { | ||||||||||||||||||||
| 191 | 29 | ▶ | my ($tail) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 192 | 29 | $tail->_walk_tail( $engine, $d, $count - 1 ); | |||||||||||||||||||
| 193 | }, | ||||||||||||||||||||
| 194 | sub { | ||||||||||||||||||||
| 195 | 0 | ▶ | $d->reject(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | 29 | ); | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | else { | ||||||||||||||||||||
| 200 | $self->head($engine)->done( | ||||||||||||||||||||
| 201 | sub { | ||||||||||||||||||||
| 202 | 7 | ▶ | $d -> resolve(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 203 | }, | ||||||||||||||||||||
| 204 | sub { | ||||||||||||||||||||
| 205 | 0 | ▶ | $d -> reject(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | 7 | ); | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | sub head { | ||||||||||||||||||||
| 212 | 366 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
head: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 366 | my $p = deferred; | |||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 366 | ▶ | if ( defined $self->[$HEAD] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | 366 | $p->resolve( $self->[0] ); | |||||||||||||||||||
| 218 | } | ||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||
| 220 | 0 | $p->resolve( bless [] => 'Dallycot::Value::Undefined' ); | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | 366 | return $p->promise; | |||||||||||||||||||
| 224 | } | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | sub tail { | ||||||||||||||||||||
| 227 | 377 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
tail: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | 377 | my $p = deferred; | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 377 | ▶ | if ( defined $self->[$TAIL] ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 232 | 132 | $p->resolve( $self->[$TAIL] ); | |||||||||||||||||||
| 233 | } | ||||||||||||||||||||
| 234 | elsif ( defined $self->[$TAIL_PROMISE] ) { | ||||||||||||||||||||
| 235 | $self->_resolve_tail_promise($engine)->done( | ||||||||||||||||||||
| 236 | sub { | ||||||||||||||||||||
| 237 | 233 | ▶ | if ( defined $self->[$TAIL] ) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
Branch: 1 branch
| |||||||||||||||||||||
| 238 | 233 | $p->resolve( $self->[$TAIL] ); | |||||||||||||||||||
| 239 | } | ||||||||||||||||||||
| 240 | else { | ||||||||||||||||||||
| 241 | 0 | $p->reject('The tail operator expects a stream-like object.'); | |||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | }, | ||||||||||||||||||||
| 244 | sub { | ||||||||||||||||||||
| 245 | 0 | ▶ | $p->reject(@_); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | 240 | ); | |||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | else { | ||||||||||||||||||||
| 250 | 5 | $p->resolve( bless [] => 'Dallycot::Value::EmptyStream' ); | |||||||||||||||||||
| 251 | } | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | 377 | return $p->promise; | |||||||||||||||||||
| 254 | } | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 30 | 1; | |||||||||||||||||||