| 1 | package Dallycot::AST::Modulus; | ||||||||||
| 2 | 29 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||
| 3 | |||||||||||
| 4 | # ABSTRACT: Calculate the modulus of a series of values | ||||||||||
| 5 | |||||||||||
| 6 | 29 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 29 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 29 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 29 | ▶ | use parent 'Dallycot::AST'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 29 | ▶ | use Promises qw(deferred); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | sub to_string { | ||||||||||
| 15 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 16 | 0 | return join( " mod ", map { $_->to_string } @$self ); | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | sub to_rdf { | ||||||||||
| 20 | 1 | ▶ | my($self, $model) = @_; | ||||||||
|
Subroutine
to_rdf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | return $model -> apply( | |||||||||
| 23 | $model -> meta_uri('loc:modulus'), | ||||||||||
| 24 | [ @$self ], | ||||||||||
| 25 | {} | ||||||||||
| 26 | ); | ||||||||||
| 27 | # my $bnode = $model->bnode; | ||||||||||
| 28 | # $model -> add_type($bnode, 'loc:Modulus'); | ||||||||||
| 29 | # | ||||||||||
| 30 | # $model -> add_list($bnode, 'loc:expressions', | ||||||||||
| 31 | # map { $_ -> to_rdf($model) } @$self | ||||||||||
| 32 | # ); | ||||||||||
| 33 | # return $bnode; | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub execute { | ||||||||||
| 37 | 34 | ▶ | my ( $self, $engine ) = @_; | ||||||||
|
Subroutine
execute: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 38 | |||||||||||
| 39 | 34 | my @expressions = @$self; | |||||||||
| 40 | return $engine->execute( ( shift @expressions ), ['Numeric'] )->then( | ||||||||||
| 41 | sub { | ||||||||||
| 42 | 34 | ▶ | my ($left_value) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | 34 | my $d = deferred; | |||||||||
| 45 | |||||||||||
| 46 | 34 | $self->process_loop( | |||||||||
| 47 | $engine, $d, | ||||||||||
| 48 | base => $left_value, | ||||||||||
| 49 | expressions => \@expressions | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | 34 | return $d; | |||||||||
| 53 | } | ||||||||||
| 54 | 34 | ); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | sub process_loop { | ||||||||||
| 58 | 34 | ▶ | my ( $self, $engine, $d, %state ) = @_; | ||||||||
|
Subroutine
process_loop: called CC=3 SCAR=15.0
Pod
undocumented
| |||||||||||
| 59 | my ( $left_value, $right_expr, @expressions ) | ||||||||||
| 60 | 34 | ▶ | = ( $state{base}, @{ $state{expressions} || [] } ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 34 | ▶ | if ( !@expressions ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | $engine->execute( $right_expr, ['Numeric'] )->done( | ||||||||||
| 64 | sub { | ||||||||||
| 65 | 34 | ▶ | my ($right_value) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 66 | 34 | $d->resolve( | |||||||||
| 67 | Dallycot::Value::Numeric->new( $left_value->value->copy->bmod( $right_value->value ) ) ); | ||||||||||
| 68 | }, | ||||||||||
| 69 | sub { | ||||||||||
| 70 | 0 | ▶ | $d->reject(@_); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 71 | } | ||||||||||
| 72 | 34 | ); | |||||||||
| 73 | } | ||||||||||
| 74 | else { | ||||||||||
| 75 | $engine->execute( $right_expr, ['Numeric'] )->done( | ||||||||||
| 76 | sub { | ||||||||||
| 77 | 0 | ▶ | my ($right_value) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 78 | 0 | $left_value = $left_value->copy->bmod( $right_value->value ); | |||||||||
| 79 | 0 | ▶ | if ( $left_value->is_zero ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | $d->resolve( Dallycot::Value::Numeric->new($left_value) ); | |||||||||
| 81 | } | ||||||||||
| 82 | else { | ||||||||||
| 83 | 0 | $self->process_loop( | |||||||||
| 84 | $engine, $d, | ||||||||||
| 85 | base => $left_value, | ||||||||||
| 86 | expressions => \@expressions | ||||||||||
| 87 | ); | ||||||||||
| 88 | } | ||||||||||
| 89 | }, | ||||||||||
| 90 | sub { | ||||||||||
| 91 | 0 | ▶ | $d->reject(@_); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 92 | } | ||||||||||
| 93 | 0 | ); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 34 | return; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 29 | 1; | |||||||||