blib/lib/Dallycot/Library/LOC.pm

filter: Statementstmt 22.7% 52 / 229 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 11.4% 8 / 70 Podpod 0.0% 0 / 1 totaltotal 17.0% 60 / 353 CC 26 SCAR 15.4
CC
26
Cov
19%
__ANON__
20.0
__ANON__
12.0
__ANON__
6.0
CRAP 4.7
« blib/lib/Dallycot/Value/Duration.pm ↑ Summary blib/lib/Dallycot/Value/DateTime.pm »
1 package Dallycot::Library::LOC;
2 9 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Core library of useful functions
5  
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use Dallycot::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use List::Util qw(all any);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ns 'http://www.dallycot.net/ns/loc/1.0#';
19  
20 define 'all-true' => (
21 hold => 1,
22 arity => [0],
23 options => {},
24 ), sub {
25 0 ▶ my ( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
26  
27 0 ▶ return $engine->TRUE unless @things;
Branch: 1 branch
BranchTrueFalseTotal
unless @things 0 0 0
28  
29 0 my $d = deferred;
30  
31 0 my $process_loop;
32  
33 $process_loop = sub {
34 0 ▶ if ( !@things ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (not @things) { } 0 0 0
35 0 $d->resolve( $engine->TRUE );
36 }
37 else {
38 $engine->execute( shift @things, ['Boolean'] )->done(
39 sub {
40 0 ▶ if ( $_[0]->value ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->value) { } 0 0 0
41 0 $process_loop->();
42 }
43 else {
44 0 $d->resolve( $engine->FALSE );
45 }
46 },
47 sub {
48 0 ▶ $d->reject(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 }
50 0 );
51 };
52  
53 0 return;
54 0 };
55  
56 0 $process_loop->();
57  
58 0 return $d -> promise;
59 9 };
60  
61 define 'any-true' => (
62 hold => 1,
63 arity => [0],
64 options => {},
65 ), sub {
66 0 ▶ my ( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
67  
68 0 ▶ return $engine->FALSE unless @things;
Branch: 1 branch
BranchTrueFalseTotal
unless @things 0 0 0
69  
70 0 my $d = deferred;
71  
72 0 my $process_loop;
73  
74 $process_loop = sub {
75 0 ▶ if ( !@things ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (not @things) { } 0 0 0
76 0 $d->resolve( $engine->TRUE );
77 }
78 else {
79 $engine->execute( shift @things, ['Boolean'] )->done(
80 sub {
81 0 ▶ if ( $_[0]->value ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->value) { } 0 0 0
82 0 $d -> resolve( $engine->TRUE );
83 }
84 else {
85 0 $process_loop -> ();
86 }
87 },
88 sub {
89 0 ▶ $d->reject(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 }
91 0 );
92 }
93  
94 0 return;
95 0 };
96  
97 0 $process_loop->();
98  
99 0 return $d -> promise;
100 9 };
101  
102 9 define 'y-combinator' => '(function) :> function(function, ___)';
103  
104 9 define foldl => <<'EOD';
105 (
106 folder := y-combinator(
107 (self, pad, function, stream) :> (
108 (?stream) : (
109 next := function(pad, stream');
110 [ next, self(self, next, function, stream...) ]
111 )
112 ( ) : [ ]
113 )
114 );
115 (initial, function, stream) :> (
116 (?stream) : folder(initial, function, stream)
117 ( ) : [ initial ]
118 )
119 )
120 EOD
121  
122 9 define foldl1 => <<'EOD';
123 (function, stream) :> (
124 (?stream) : foldl(stream', function, stream...)
125 ( ) : [ ]
126 )
127 EOD
128  
129 9 define map => <<'EOD';
130 y-combinator(
131 (self, mapper, stream) :> (
132 (?stream) : [ mapper(stream'), self(self, mapper, stream...) ]
133 ( ) : [ ]
134 )
135 )
136 EOD
137  
138 9 define filter => <<'EOD';
139 y-combinator(
140 (self, selector, stream) :> (
141 (?stream) : (
142 (selector(stream')) : [ stream', self(self, selector, stream...) ]
143 ( ) : self(self, selector, stream...)
144 )
145 ( ) : [ ]
146 )
147 )
148 EOD
149  
150 define 'build-filter' => (
151 hold => 0,
152 arity => [0],
153 options => {},
154 ), sub {
155 0 ▶ my ( $engine, $options, @functions ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
156  
157 0 my $stream = pop @functions;
158 0 return collect( map { maybe_promise( $_->is_lambda ) } @functions )->then(
159 sub {
160 0 ▶ my @flags = map {@$_} @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
161 0 ▶ if ( any { !$_ } @flags ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_; } , @flags)) 0 0 0
162 0 croak "All but the last term in a filter must be lambdas.";
163 }
164 }
165 )->then(
166 sub {
167 0 return collect( map { maybe_promise( $_->min_arity ) } @functions )->then(
168 sub {
169 0 ▶ my (@arities) = map {@$_} @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
170 0 ▶ if ( any { 1 != $_ } @arities ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { 1 != $_; } , @arities)) 0 0 0
171 0 croak "All lambdas in a filter must have arity 1.";
172 }
173 }
174 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 }
176 )->then(
177 sub {
178 return maybe_promise( $stream->is_lambda )->then(
179 sub {
180 0 ▶ my ($flag) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
181 0 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 0 0 0
182 0 return $engine->make_filter( $engine->compose_filters( @functions, $stream ) );
183 }
184 else {
185 0 return $stream->apply_filter( $engine, $engine->compose_filters(@functions) );
186 }
187 }
188 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 }
190 0 );
191 9 };
192  
193 define 'build-list' => (
194 hold => 1,
195 arity => [0],
196 options => {},
197 ), sub {
198 0 ▶ my ( $engine, $options, @expressions ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
199  
200 0 given ( scalar(@expressions) ) {
201 0 when (0) {
202 0 return Dallycot::Value::EmptyStream->new;
203 }
204 0 when (1) {
205 return $engine->execute( $expressions[0] )->then(
206 sub {
207 0 ▶ my ($result) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 Dallycot::Value::Stream->new($result);
209 }
210 0 );
211 }
212 0 default {
213 0 my $last_expr = pop @expressions;
214 0 my $promise;
215 0 ▶ if ( $last_expr->isa('Dallycot::Value') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_expr->isa('Dallycot::Value')) { } 0 0 0
216 0 push @expressions, $last_expr;
217 }
218 else {
219 0 $promise = $engine->make_lambda($last_expr);
220 }
221 return $engine->collect(@expressions)->then(
222 sub {
223 0 ▶ my (@items) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
224 0 my $result = Dallycot::Value::Stream->new( ( pop @items ), undef, $promise );
225 0 ▶ while (@items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 0 0 0
226 0 $result = Dallycot::Value::Stream->new( ( pop @items ), $result );
227 }
228 0 $result;
229 }
230 0 );
231 }
232 }
233 9 };
234  
235 define 'build-map' => (
236 hold => 0,
237 arity => [0],
238 options => {}
239 ), sub {
240 0 ▶ my ( $engine, $options, @functions ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 0 my $stream = pop @functions;
242 0 return collect( map { maybe_promise( $_->is_lambda ) } @functions )->then(
243 sub {
244 0 ▶ my @flags = map {@$_} @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
245 0 ▶ if ( any { !$_ } @flags ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_; } , @flags)) 0 0 0
246 0 croak "All but the last term in a mapping must be lambdas.";
247 }
248 }
249 )->then(
250 sub {
251 0 return collect( map { maybe_promise( $_->min_arity ) } @functions )->then(
252 sub {
253 0 ▶ my (@arities) = map {@$_} @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
254 0 ▶ if ( any { 1 != $_ } @arities ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { 1 != $_; } , @arities)) 0 0 0
255 0 croak "All lambdas in a mapping must have arity 1.";
256 }
257 }
258 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 }
260 )->then(
261 sub {
262 return maybe_promise( $stream->is_lambda )->then(
263 sub {
264 0 ▶ my ($flag) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
265  
266 0 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 0 0 0
267 0 return $engine->make_map( $engine->compose_lambdas( @functions, $stream ) );
268 }
269 else {
270 0 my $transform = $engine->compose_lambdas(@functions);
271  
272 0 return $stream->apply_map( $engine, $transform );
273 }
274 }
275 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
276 }
277 0 );
278 9 };
279  
280 9 define upfrom => <<'EOD';
281 y-combinator( (self, n) :> [ n, self(self, n + 1) ] )
282 EOD
283  
284 9 define range => <<'EOD';
285 y-combinator(
286 (self, m, n) :> (
287 (m > n) : [ m, self(self, m - 1, n) ]
288 (m = n) : [ m ]
289 (m < n) : [ m, self(self, m + 1, n) ]
290 ( ) : [ ]
291 )
292 )
293 EOD
294  
295 define 'build-set' => (
296 hold => 0,
297 arity => [0],
298 options => {}
299 ), sub {
300 0 ▶ my( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
301  
302 0 return Dallycot::Value::Set->new(@things);
303 9 };
304  
305 define 'build-vector' => (
306 hold => 0,
307 arity => [0],
308 options => {}
309 ), sub {
310 0 ▶ my( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
311  
312 0 return Dallycot::Value::Vector->new(@things);
313 9 };
314  
315 define 'compose-functions' => (
316 hold => 0,
317 arity => [0],
318 options => {}
319 ), sub {
320 0 ▶ my ( $engine, $options, @functions ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
321  
322 0 return collect( map { maybe_promise( $_->is_lambda ) } @functions )->then(
323 sub {
324 0 ▶ my @flags = map {@$_} @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
325 0 ▶ if ( any { !$_ } @flags ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_; } , @flags)) 0 0 0
326 0 croak "All terms in a function composition must be lambdas";
327 }
328 }
329 )->then(
330 sub {
331 0 return collect( map { maybe_promise( $_->min_arity ) } @functions )->then(
332 sub {
333 0 ▶ my (@arities) = map {@$_} @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
334  
335 0 ▶ if ( any { 1 != $_ } @arities ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { 1 != $_; } , @arities)) 0 0 0
336 0 croak "All lambdas in a function composition must have arity 1";
337 }
338 }
339 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
340 }
341 )->then(
342 sub {
343 0 ▶ return $engine->compose_lambdas(@functions);
Subroutine __ANON__: not called CC=1 SCAR=6.9
344 }
345 0 );
346 9 };
347  
348 define consolidate => (
349 hold => 0,
350 arity => [1],
351 options => {},
352 ), sub {
353 0 ▶ my( $engine, $options, $root, @things) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
354  
355 0 ▶ return $root unless @things;
Branch: 1 branch
BranchTrueFalseTotal
unless @things 0 0 0
356  
357 0 return $root->prepend(@things);
358 9 };
359  
360 sub compare (&$@) {
361 0 ▶ my ( $comparator, $engine, @expressions ) = @_;
Subroutine compare: not called CC=1 SCAR=6.9
Pod undocumented
362  
363 0 my $d = deferred;
364  
365 0 my $process_loop;
366  
367 $process_loop = sub {
368 0 ▶ my( $left_value ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
369  
370 0 ▶ if ( !@expressions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @expressions) { } 0 0 0
371 0 $d -> resolve( $engine -> TRUE );
372 }
373 else {
374 $engine -> execute( shift @expressions ) -> then(
375 sub {
376 0 ▶ my ($right_value) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
377 $engine->coerce( $left_value, $right_value, [ $left_value->type, $right_value->type ] )->done(
378 sub {
379 0 ▶ my ( $cleft, $cright ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 $comparator->( $cleft, $cright )->done(
381 sub {
382 0 ▶ if ( $_[0] ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
383 0 $process_loop->( $right_value, @expressions );
384 }
385 else {
386 0 $d->resolve( $engine->FALSE );
387 }
388 },
389 sub {
390 0 ▶ $d->reject(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
391 }
392 0 );
393 },
394 sub {
395 0 ▶ $d -> reject(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
396 }
397 0 );
398 },
399 sub {
400 0 ▶ $d -> reject(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 }
402 0 );
403 }
404 0 };
405  
406 $engine->execute( shift @expressions )->done(
407 sub {
408 0 ▶ $process_loop->( $_[0] );
Subroutine __ANON__: not called CC=1 SCAR=6.9
409 },
410 sub {
411 0 ▶ $d->reject(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 }
413 0 );
414  
415 0 return $d->promise;
416 }
417  
418 define 'all-decreasing' => (
419 hold => 1,
420 arity => [1],
421 options => {}
422 ), sub {
423 0 ▶ my ( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
424  
425 compare {
426 0 ▶ my($a, $b) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 0 $a -> is_greater_or_equal( $engine, $b );
428 0 } @things;
429 9 };
430  
431 define 'all-increasing' => (
432 hold => 1,
433 arity => [1],
434 options => {}
435 ), sub {
436 0 ▶ my ( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
437  
438 compare {
439 0 ▶ my($a, $b) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
440 0 $a -> is_less_or_equal( $engine, $b );
441 0 } @things;
442 9 };
443  
444 define 'all-strictly-decreasing' => (
445 hold => 1,
446 arity => [1],
447 options => {}
448 ), sub {
449 0 ▶ my ( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
450  
451 compare {
452 0 ▶ my($a, $b) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
453 0 $a -> is_greater( $engine, $b );
454 0 } @things;
455 9 };
456  
457 define 'all-strictly-increasing' => (
458 hold => 1,
459 arity => [1],
460 options => {}
461 ), sub {
462 0 ▶ my ( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
463  
464 compare {
465 0 ▶ my($a, $b) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
466 0 $a -> is_less( $engine, $b );
467 0 } @things;
468 9 };
469  
470 define 'all-equal' => (
471 hold => 1,
472 arity => [1],
473 options => {}
474 ), sub {
475 0 ▶ my ( $engine, $options, @things ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
476  
477 compare {
478 0 ▶ my($a, $b) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
479 0 $a -> is_equal( $engine, $b );
480 0 } @things;
481 9 };
482  
483 define 'all-unique' => (
484 hold => 0,
485 arity => [1],
486 options => {}
487 ), sub {
488 0 ▶ my( $engine, $options, @values ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
489  
490 0 my @types = map { $_->type } @values;
491 return $engine->coerce( @values, \@types )->then(
492 sub {
493 0 ▶ my (@new_values) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
494  
495 # now make sure values are all different
496 0 my %seen;
497 0 ▶ if(all { !$seen{ $_->id }++ } @new_values) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { not $seen{$_->id}++; } , @new_values)) { } 0 0 0
498 0 return $engine->TRUE;
499 }
500 else {
501 0 return $engine->FALSE;
502 }
503 }
504 0 );
505 9 };
506  
507 define 'not-empty' => (
508 hold => 0,
509 arity => 1,
510 options => {}
511 ), sub {
512 0 ▶ my( $engine, $options, $result ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
513  
514 0 ▶ if ( blessed $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($result)) { } 0 0 0
515 0 ▶ return ( $result->is_defined && !$result->is_empty ? $engine->TRUE : $engine->FALSE );
Branch: 1 branch
BranchTrueFalseTotal
$result->is_defined && !$result->is_empty ? : 0 0 0
Condition0%0/3
$result->is_defined and not $result->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$result->is_defined and not $result->is_empty
$result->is_defined not $result->is_empty
Truth table0%0/3
A and not(B)
A= $result->is_definedB= not $result->is_empty
ABresult
0X0
100
111
516 }
517 else {
518 0 return ( $engine->FALSE );
519 }
520 9 };
521  
522 9 define 'execute-list' => <<'EOD';
523 (sequence) :> (
524 last(
525 foldl(
526 (),
527 { (#2)() }/2,
528 sequence
529 )
530 )
531 )
532 EOD
533  
534 define 'invert' => (
535 hold => 0,
536 arity => 1,
537 options => {}
538 ), sub {
539 0 ▶ my($engine, $options, $res) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
540  
541 0 ▶ if ( $res->isa('Dallycot::Value::Boolean') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->isa('Dallycot::Value::Boolean')) { } 0 0 0
elsif ($res->isa('Dallycot::Value::Lambda')) { } 0 0 0
542 0 return Dallycot::Value::Boolean->new( !$res->value );
543 }
544 elsif ( $res->isa('Dallycot::Value::Lambda') ) {
545 0 return $res -> invert;
546 # return Dallycot::Value::Lambda->new(
547 # expression => Dallycot::AST::Invert->new( $res->[0] ),
548 # bindings => $res->[1],
549 # bindings_with_defaults => $res->[2],
550 # options => $res->[3],
551 # closure_environment => $res->[4],
552 # closure_namespaces => $res->[5]
553 # );
554 }
555 else {
556 0 return Dallycot::Value::Boolean->new( !$res->is_defined );
557 }
558 9 };
559  
560 9 1;
« blib/lib/Dallycot/Value/Duration.pm ↑ Summary blib/lib/Dallycot/Value/DateTime.pm »