blib/lib/Dallycot/Library/Core/Math.pm

filter: Statementstmt 62.0% 157 / 253 Branchbran 42.1% 27 / 64 Conditioncond 33.3% 9 / 27 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 68.9% 20 / 29 Podpod n/a totaltotal 55.1% 215 / 390 CC 42 SCAR 23.9
CC
42
Cov
56%
__ANON__
22.7
__ANON__
20.0
__ANON__
20.0
CRAP 10.9
« blib/lib/Dallycot/Library/Core/Strings.pm ↑ Summary blib/lib/Dallycot/Value/TripleStore.pm »
1 package Dallycot::Library::Core::Math;
2 9 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Core library of useful math 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 Dallycot::Library::Core ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use Promises qw(deferred collect);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Math::BigInt::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 ns 'http://www.dallycot.net/ns/math/1.0#';
23  
24 9 uses 'http://www.dallycot.net/ns/core/1.0#', 'http://www.dallycot.net/ns/loc/1.0#';
25  
26 define
27 'divisible-by?' => (
28 hold => 0,
29 arity => 2,
30 options => {},
31 ),
32 sub {
33 67 ▶ my ( $engine, $options, $x, $n ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.8
34  
35 67 my $d = deferred;
36  
37 67 ▶ if ( !$x->isa('Dallycot::Value::Numeric')
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric') && $n->isa('Dallycot::Value::Numeric')) { } 0 67 67
Condition33%1/3
$x->isa('Dallycot::Value::Numeric') && $n->isa('Dallycot::Value::Numeric')
!ll&&!rl&&r
0067
MC/DC0%0/2
$x->isa('Dallycot::Value::Numeric') && $n->isa('Dallycot::Value::Numeric')
$x->isa('Dallycot::Value::Numeric') $n->isa('Dallycot::Value::Numeric')
Truth table33%1/3
A && B
A= $x->isa('Dallycot::Value::Numeric')B= $n->isa('Dallycot::Value::Numeric')
ABresult
0X0
100
111
38 || !$n->isa('Dallycot::Value::Numeric') )
39 {
40 0 $d->reject("divisible-by? expects numeric arguments");
41 }
42 else {
43 67 my $xcopy = $x->value->copy();
44 67 $xcopy->bmod( $n->value );
45 67 $d->resolve( Dallycot::Value::Boolean->new( $xcopy->is_zero ) );
46 }
47  
48 67 return $d->promise;
49 9 };
50  
51 define
52 'even?' => (
53 hold => 0,
54 arity => 1,
55 options => {}
56 ),
57 sub {
58 2 ▶ my ( $engine, $options, $x ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
59  
60 2 my $d = deferred;
61  
62 2 ▶ if ( !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric')) { } 0 2 2
63 0 $d->reject("even? expects a numeric argument");
64 }
65 else {
66 2 $d->resolve( Dallycot::Value::Boolean->new( $x->[0]->is_even ) );
67 }
68  
69 2 return $d;
70 9 };
71  
72 define
73 'odd?' => (
74 hold => 0,
75 arity => 1,
76 options => {}
77 ),
78 sub {
79 2 ▶ my ( $engine, $options, $x ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
80  
81 2 my $d = deferred;
82  
83 2 ▶ if ( !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric')) { } 0 2 2
84 0 $d->reject("odd? expects a numeric argument");
85 }
86 else {
87 2 $d->resolve( Dallycot::Value::Boolean->new( $x->[0]->is_odd ) );
88 }
89  
90 2 return $d->promise;
91 9 };
92  
93 define
94 factorial => (
95 hold => 0,
96 arity => 1,
97 options => {}
98 ),
99 sub {
100 10 ▶ my ( $engine, $options, $x ) = @_;
Subroutine __ANON__: called CC=3 SCAR=12.3
101  
102 10 my $d = deferred;
103  
104 10 ▶ if ( !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric')) { } 0 10 10
elsif ($x->value->is_int) { } 10 0 10
105 0 $d->reject("factorial expects a numeric argument");
106 }
107 elsif ( $x->value->is_int ) {
108 10 $d->resolve( Dallycot::Value::Numeric->new( $x->value->copy()->bfac() ) );
109 }
110 else {
111 # TODO: handle non-integer arguments to gamma function
112 0 $d->resolve( $engine->UNDEFINED );
113 }
114  
115 10 return $d->promise;
116 9 };
117  
118 define
119 ceil => (
120 hold => 0,
121 arity => 1,
122 options => {}
123 ),
124 sub {
125 0 ▶ my ( $engine, $options, $x ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
126  
127 0 my $d = deferred;
128  
129 0 ▶ if ( !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric')) { } 0 0 0
130 0 $d->reject("ceiling expects a numeric argument");
131 }
132 else {
133 0 $d->resolve( Dallycot::Value::Numeric->new( $x->value->copy->bceil ) );
134 }
135  
136 0 return $d->promise;
137 9 };
138  
139 define
140 floor => (
141 hold => 0,
142 arity => 1,
143 options => {}
144 ),
145 sub {
146 0 ▶ my ( $engine, $options, $x ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
147  
148 0 my $d = deferred;
149  
150 0 ▶ if ( !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric')) { } 0 0 0
151 0 $d->reject("floor expects a numeric argument");
152 }
153 else {
154 0 $d->resolve( Dallycot::Value::Numeric->new( $x->value->copy->bfloor ) );
155 }
156  
157 0 return $d->promise;
158 9 };
159  
160 define
161 abs => (
162 hold => 0,
163 arity => 1,
164 options => {}
165 ),
166 sub {
167 0 ▶ my ( $engine, $options, $x ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
168  
169 0 my $d = deferred;
170  
171 0 ▶ if ( !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric')) { } 0 0 0
172 0 $d->reject("abs expects a numeric argument");
173 }
174 else {
175 0 $d->resolve( Dallycot::Value::Numeric->new( $x->value->copy->babs ) );
176 }
177  
178 0 return $d->promise;
179 9 };
180  
181 define
182 binomial => (
183 hold => 0,
184 arity => 2,
185 options => {}
186 ),
187 sub {
188 0 ▶ my ( $engine, $options, $x, $y ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
189  
190 0 my $d = deferred;
191  
192 0 ▶ if ( !$x->isa('Dallycot::Value::Numeric')
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric') && $y->isa('Dallycot::Value::Numeric')) { } 0 0 0
Condition0%0/3
$x->isa('Dallycot::Value::Numeric') && $y->isa('Dallycot::Value::Numeric')
!ll&&!rl&&r
000
MC/DC0%0/2
$x->isa('Dallycot::Value::Numeric') && $y->isa('Dallycot::Value::Numeric')
$x->isa('Dallycot::Value::Numeric') $y->isa('Dallycot::Value::Numeric')
Truth table0%0/3
A && B
A= $x->isa('Dallycot::Value::Numeric')B= $y->isa('Dallycot::Value::Numeric')
ABresult
0X0
100
111
193 || !$y->isa('Dallycot::Value::Numeric') )
194 {
195 0 $d->reject("binomial-coefficient expects numeric arguments");
196 }
197 else {
198 0 $d->resolve( Dallycot::Value::Numeric->new( $x->value->copy->bnok( $y->value ) ) );
199 }
200  
201 0 return $d->promise;
202 9 };
203  
204 define
205 pi => (
206 hold => 0,
207 arity => [ 0, 1 ],
208 options => {}
209 ),
210 sub {
211 0 ▶ my ( $engine, $options, $accuracy ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
212  
213 0 my $d = deferred;
214  
215 0 ▶ if ( defined($accuracy) && !$accuracy->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $accuracy and not $accuracy->isa('Dallycot::Value::Numeric')) { } 0 0 0
Condition0%0/3
defined $accuracy and not $accuracy->isa("Dallycot::Value::Numeric")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $accuracy and not $accuracy->isa("Dallycot::Value::Numeric")
defined $accuracy not $accuracy->isa("Dallycot::Value::Numeric")
Truth table0%0/3
A and not(B)
A= defined $accuracyB= not $accuracy->isa("Dallycot::Value::Numeric")
ABresult
0X0
100
111
216 0 $d->reject("pi requires a numeric argument");
217 }
218 else {
219 0 ▶ $accuracy = defined($accuracy) ? $accuracy->value->as_int : 40;
Branch: 1 branch
BranchTrueFalseTotal
defined $accuracy ? : 0 0 0
220 0 my $pi = Math::BigFloat->bpi($accuracy);
221 0 $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->new($pi) ) );
222 }
223 0 return $d->promise;
224 9 };
225  
226 define
227 'golden-ratio' => (
228 hold => 0,
229 arity => [ 0, 1 ],
230 options => {}
231 ),
232 sub {
233 0 ▶ my ( $engine, $options, $accuracy ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
234  
235 0 my $d = deferred;
236  
237 0 ▶ if ( defined($accuracy) && !$accuracy->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $accuracy and not $accuracy->isa('Dallycot::Value::Numeric')) { } 0 0 0
Condition0%0/3
defined $accuracy and not $accuracy->isa("Dallycot::Value::Numeric")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $accuracy and not $accuracy->isa("Dallycot::Value::Numeric")
defined $accuracy not $accuracy->isa("Dallycot::Value::Numeric")
Truth table0%0/3
A and not(B)
A= defined $accuracyB= not $accuracy->isa("Dallycot::Value::Numeric")
ABresult
0X0
100
111
238 0 $d->reject("pi requires a numeric argument");
239 }
240 else {
241 0 ▶ $accuracy = defined($accuracy) ? $accuracy->value->as_int : 40;
Branch: 1 branch
BranchTrueFalseTotal
defined $accuracy ? : 0 0 0
242 }
243  
244 0 $d->resolve(
245 Dallycot::Value::Numeric->new(
246 Math::BigRat->new(
247 ( ( 1 + Math::BigFloat->new(5)->bsqrt( $accuracy + 1 ) ) / 2 )->bround($accuracy)
248 )
249 )
250 );
251  
252 0 return $d->promise;
253 9 };
254  
255 sub _simple_trig {
256 4 ▶ my ( $name, $method, $engine, $options, $arg ) = @_;
Subroutine _simple_trig: called CC=4 SCAR=16.4
257  
258 4 my $units;
259 4 my $d = deferred;
260  
261 4 ▶ if ( $options->{'units'}->isa('Dallycot::Value::String') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"units"}->isa("Dallycot::Value::String")) 4 0 4
262 4 $units = $options->{'units'}->value;
263 }
264 4 ▶ if ( !$options->{'accuracy'}->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"accuracy"}->isa("Dallycot::Value::Numeric")) 0 4 4
265 0 $d->reject("accuracy must be a numeric value");
266 0 return $d->promise;
267 }
268 4 ▶ if ( !$arg->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg->isa("Dallycot::Value::Numeric")) 0 4 4
269 0 $d->reject("$name requires a numeric argument");
270 0 return $d->promise;
271 }
272 4 my $accuracy = $options->{'accuracy'}->value->as_int;
273 4 my $angle = $arg->value->as_float( $accuracy + 10 );
274 4 given ($units) {
275 4 when ('degrees') {
276 4 $angle = $angle * Math::BigFloat->bpi( $accuracy + 10 ) / 180;
277 }
278 0 when ('radians') { }
279 0 when ('gradians') {
280 0 $angle = $angle * Math::BigFloat->bpi( $accuracy + 10 ) / 200;
281 }
282 0 default {
283 0 $d->reject("units must be 'degrees', 'radians', or 'gradians'");
284 0 return $d;
285 }
286 }
287 4 my $answer = $angle->$method($accuracy);
288 4 $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->new($answer) ) );
289 4 return $d;
290 }
291  
292 define
293 sin => (
294 hold => 0,
295 arity => 1,
296 options => {
297 units => Dallycot::Value::String->new('degrees'),
298 accuracy => Dallycot::Value::Numeric->new( Math::BigRat->new(40) ),
299 }
300 ),
301 sub {
302 2 ▶ my ( $engine, $options, $arg ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
303  
304 2 return _simple_trig( 'sin', 'bsin', $engine, $options, $arg );
305 9 };
306  
307 define
308 cos => (
309 hold => 0,
310 arity => 1,
311 options => {
312 units => Dallycot::Value::String->new('degrees'),
313 accuracy => Dallycot::Value::Numeric->new( Math::BigRat->new(40) ),
314 }
315 ),
316 sub {
317 2 ▶ my ( $engine, $options, $arg ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
318  
319 2 return _simple_trig( 'cos', 'bcos', $engine, $options, $arg );
320 9 };
321  
322 9 define tan => <<'EOD';
323 (angle, units -> "degrees", accuracy -> 40) :> (
324 sin(angle, units -> units, accuracy -> accuracy) div
325 cos(angle, units -> units, accuracy -> accuracy)
326 )
327 EOD
328  
329 define
330 'arc-tan' => (
331 hold => 0,
332 arity => [ 1, 2 ],
333 options => {
334 units => Dallycot::Value::String->new('degrees'),
335 accuracy => Dallycot::Value::Numeric->new( Math::BigRat->new(40) ),
336 }
337 ),
338 sub {
339 3 ▶ my ( $engine, $options, $y, $x ) = @_;
Subroutine __ANON__: called CC=14 SCAR=31.2
340  
341 3 my $units;
342 3 my $d = deferred;
343  
344 3 ▶ if ( $options->{'units'}->isa('Dallycot::Value::String') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"units"}->isa("Dallycot::Value::String")) 3 0 3
345 3 $units = $options->{'units'}->value;
346 }
347 3 ▶ if ( !$options->{'accuracy'}->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"accuracy"}->isa("Dallycot::Value::Numeric")) 0 3 3
348 0 $d->reject("accuracy must be a numeric value");
349 0 return $d->promise;
350 }
351 3 ▶ if ( !$y->isa('Dallycot::Value::Numeric') || defined($x) && !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $y->isa("Dallycot::Value::Numeric") or defined $x and not $x->isa("Dallycot::Value::Numeric")) 0 3 3
Condition50%3/6
not $y->isa("Dallycot::Value::Numeric") or defined $x and not $x->isa("Dallycot::Value::Numeric")
l!l&&r!l&&!r
003
defined $x and not $x->isa("Dallycot::Value::Numeric")
!ll&&!rl&&r
120
MC/DC0%0/3
not $y->isa("Dallycot::Value::Numeric") or defined $x and not $x->isa("Dallycot::Value::Numeric")
not $y->isa("Dallycot::Value::Numeric") defined $x not $x->isa("Dallycot::Value::Numeric")
Truth table25%1/4
not(A) or B and not(C)
A= not $y->isa("Dallycot::Value::Numeric")B= defined $xC= not $x->isa("Dallycot::Value::Numeric")
ABCresult
1XX1
0111
00X0
0100
352 0 $d->reject("arc-tan requires numeric arguments");
353 0 return $d->promise;
354 }
355 3 my $accuracy = $options->{'accuracy'}->value->as_int;
356  
357 3 $y = $y->value->as_float( $accuracy + 10 );
358 3 ▶ if ( defined $x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x) 2 1 3
359 2 $x = $x->value->as_float( $accuracy + 10 );
360 }
361  
362 3 my $angle;
363 3 ▶ if ( defined($x) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x) { } 2 1 3
364 2 my $key = 0;
365 2 ▶ $key |= 0x04 if $x->is_pos && !$x->is_zero;
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_pos and not $x->is_zero 1 1 2
Condition66%2/3
$x->is_pos and not $x->is_zero
!ll&&!rl&&r
101
MC/DC50%1/2
$x->is_pos and not $x->is_zero
$x->is_pos not $x->is_zero
Truth table66%2/3
A and not(B)
A= $x->is_posB= not $x->is_zero
ABresult
0X0
100
111
366 2 ▶ $key |= 0x0c if $x->is_neg;
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 1 1 2
367 2 ▶ $key |= 0x01 if $y->is_pos && !$y->is_zero;
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_pos and not $y->is_zero 1 1 2
Condition66%2/3
$y->is_pos and not $y->is_zero
!ll&&!rl&&r
101
MC/DC50%1/2
$y->is_pos and not $y->is_zero
$y->is_pos not $y->is_zero
Truth table66%2/3
A and not(B)
A= $y->is_posB= not $y->is_zero
ABresult
0X0
100
111
368 2 ▶ $key |= 0x03 if $y->is_neg;
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 1 1 2
369  
370 2 given ($key) {
371 2 when ( [ 0x04, 0x05, 0x07 ] ) { # $x > 0, $y anything
372 1 $angle = $y->batan2( $x, $accuracy );
373 }
374 1 when (0x0d) { # $x < 0, $y > 0
375 0 $angle = $y->batan2( $x, $accuracy + 1 ) + Math::BigFloat->bpi( $accuracy + 1 );
376 0 $angle->bround($accuracy);
377 }
378 1 when (0x0f) { # $x < 0, $y < 0
379 1 $angle = $y->batan2( $x, $accuracy + 1 ) - Math::BigFloat->bpi( $accuracy + 1 );
380 1 $angle->bround($accuracy);
381 }
382 0 when (0x01) { # $x = 0, $y > 0
383 0 $angle = Math::BigFloat->bpi( $accuracy + 1 ) / 2;
384 0 $angle->bround($accuracy);
385 }
386 0 when (0x03) { # $x = 0, $y < 0
387 0 $angle = -Math::BigFloat->bpi( $accuracy + 1 ) / 2;
388 0 $angle->bround($accuracy);
389 }
390 0 default {
391 0 $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->nan ) );
392 0 return $d->promise;
393 }
394 }
395 }
396 else {
397 1 $angle = $y->batan($accuracy);
398 }
399  
400 3 given ($units) {
401 3 when ('degrees') {
402 3 $angle = $angle * 180 / Math::BigFloat->bpi($accuracy);
403 }
404 0 when ('radians') { }
405 0 when ('gradians') {
406 0 $angle = $angle * 200 / Math::BigFloat->bpi($accuracy);
407 }
408 0 default {
409 0 $d->reject("units must be 'degrees', 'radians', or 'gradians'");
410 0 return $d;
411 }
412 }
413 3 $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->new($angle) ) );
414 3 return $d;
415 9 };
416  
417 define
418 gcd => (
419 hold => 0,
420 arity => [2],
421 options => {}
422 ),
423 sub {
424 3 ▶ my ( $engine, $options, @values ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
425  
426 3 my $d = deferred;
427  
428 3 ▶ if ( all { $_->isa('Dallycot::Value::Numeric') } @values ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { $_->isa('Dallycot::Value::Numeric'); } , @values)) { } 3 0 3
429 3 $d->resolve( Dallycot::Value::Numeric->new( Math::BigInt::bgcd( map { $_->value->as_int } @values ) ) );
430 }
431 else {
432 0 $d->reject("gcd requires numeric values");
433 }
434 3 return $d->promise;
435 9 };
436  
437 define
438 lcm => (
439 hold => 0,
440 arity => [2],
441 options => {}
442 ),
443 sub {
444 0 ▶ my ( $engine, $options, @values ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
445  
446 0 my $d = deferred;
447  
448 0 ▶ if ( all { $_->isa('Dallycot::Value::Numeric') } @values ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { $_->isa('Dallycot::Value::Numeric'); } , @values)) { } 0 0 0
449 0 $d->resolve( Dallycot::Value::Numeric->new( Math::BigInt::blcm( map { $_->value->as_int } @values ) ) );
450 }
451 else {
452 0 $d->reject("gcd requires numeric values");
453 }
454 0 return $d->promise;
455 9 };
456  
457 define
458 random => (
459 hold => 0,
460 arity => 1,
461 options => { 'use-internet' => Dallycot::Value::Boolean->new(0), }
462 ),
463 sub {
464 1 ▶ my ( $engine, $options, $spec ) = @_;
Subroutine __ANON__: called CC=7 SCAR=29.2
465  
466 1 my $d = deferred;
467 1 my %bounds;
468  
469 1 ▶ if ( $options->{'use-internet'}->isa('Dallycot::Value::Boolean')
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"use-internet"}->isa("Dallycot::Value::Boolean") and $options->{"use-internet"}->value) 0 1 1
Condition33%1/3
$options->{"use-internet"}->isa("Dallycot::Value::Boolean") and $options->{"use-internet"}->value
!ll&&!rl&&r
010
MC/DC0%0/2
$options->{"use-internet"}->isa("Dallycot::Value::Boolean") and $options->{"use-internet"}->value
$options->{"use-internet"}->isa("Dallycot::Value::Boolean") $options->{"use-internet"}->value
Truth table33%1/3
A and B
A= $options->{"use-internet"}->isa("Dallycot::Value::Boolean")B= $options->{"use-internet"}->value
ABresult
0X0
100
111
470 && $options->{'use-internet'}->value )
471 {
472 0 $bounds{use_internet} = 1;
473 }
474  
475 1 ▶ if ( $spec->isa('Dallycot::Value::Vector') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->isa('Dallycot::Value::Vector')) { } 0 1 1
elsif ($spec->isa('Dallycot::Value::Numeric')) { } 1 0 1
476 0 ▶ if ( @$spec != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$spec != 2) 0 0 0
477 0 $d->reject("random requires min and max bounds with a vector argument");
478 0 return $d->promise;
479 }
480 0 ▶ if ( all { $_->isa("Dallycot::Value::Numeric") } @$spec ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { $_->isa('Dallycot::Value::Numeric'); } , @$spec)) { } 0 0 0
481 0 $bounds{min} = $spec->[0]->value->as_int->bstr;
482 0 $bounds{max} = $spec->[1]->value->as_int->bstr;
483 }
484 else {
485 0 $d->reject("random requires numeric min/max bounds");
486 0 return $d->promise;
487 }
488 }
489 elsif ( $spec->isa('Dallycot::Value::Numeric') ) {
490 1 $bounds{max} = $spec->value->as_int->bstr;
491 }
492 else {
493 0 $d->reject("random requires a numeric bound");
494 0 return $d->promise;
495 }
496  
497 1 my $random = Math::BigInt::Random::random_bigint(%bounds);
498  
499 1 $d->resolve( Dallycot::Value::Numeric->new($random) );
500 1 return $d->promise;
501 9 };
502  
503 9 define sum => 'foldl(0, { #1 + #2 }/2, _)';
504  
505 9 define product => 'foldl(1, { #1 * #2 }/2, _)';
506  
507 9 define min => <<'EOD';
508 foldl1({(
509 (#1 < #2) : #1
510 ( ) : #2
511 )}/2, _)
512 EOD
513  
514 9 define max => <<'EOD';
515 foldl1({(
516 (#1 > #2) : #1
517 ( ) : #2
518 )}/2, _)
519 EOD
520  
521 9 define 'weighted-count-and-sum' => <<'EOD';
522 foldl( <0,0>, (
523 (pad, element) :>
524 <pad[1] + element[1], pad[2] + element[1] * element[2]>
525 ), _)
526 EOD
527  
528 9 define 'count-and-sum' => <<'EOD';
529 foldl( <0,0>, (
530 (pad, element) :>
531 < pad[1] + 1, pad[2] + element >
532 ), _)
533 EOD
534  
535 9 define mean => <<'EOD';
536 (s) :> ({ #[2] div #[1] } @ count-and-sum(s))
537 EOD
538  
539 9 define differences => <<'EOD';
540 (stream, d = 1) :> (
541 nest( y-combinator(
542 (self, s) :> (
543 (?(s') and ?(s...)) : [ s...' - s', self(self, s...) ]
544 (?(s') ) : [ -s' ]
545 ( ) : [ ]
546 )
547 ), d)(stream)
548 )
549 EOD
550  
551 9 define 'make-evens' => '() :> ({ # * 2 } @ 1..)';
552  
553 9 define 'make-odds' => '() :> ({ # * 2 + 1 } @ 0..)';
554  
555 9 define evens => 'make-evens()';
556  
557 9 define odds => 'make-odds()';
558  
559 9 define primes => <<'EOD';
560 sieve := y-combinator( (self, s) :> [ s', self(self, ~divisible-by?(_, s') % s...) ] );
561 [ 1, 2, sieve(make-odds()...) ]
562 EOD
563  
564 9 define 'prime-pairs' => 'primes Z primes...';
565  
566 9 define 'twin-primes' => '{ #[2] - #[1] = 2 } % prime-pairs';
567  
568 9 define factorials => 'factorial @ 1..';
569  
570 9 define 'fibonacci-sequence' => <<'EOD';
571 [ 1, 1, y-combinator((self, a, b) :> [ a + b, self(self, b, a+b) ])(1, 1) ]
572 EOD
573  
574 9 define 'leonardo-sequence' => <<'EOD';
575 [ 1, 1, y-combinator((self, a, b) :> [ a + b + 1, self(self, b, a + b + 1) ])(1, 1) ]
576 EOD
577  
578 9 define prime => '(n) :> primes[n]';
579 9 define fibonacci => '(n) :> fibonacci-sequence[n]';
580 9 define leonardo => '(n) :> leonardo-sequence[n]';
581  
582 9 1;
« blib/lib/Dallycot/Library/Core/Strings.pm ↑ Summary blib/lib/Dallycot/Value/TripleStore.pm »