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

filter: Statementstmt 61.9% 158 / 255 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% 216 / 392 CC 42 SCAR 23.9
CC
42
Cov
56%
__ANON__
22.7
__ANON__
20.0
__ANON__
20.0
CRAP 10.9
« blib/lib/Dallycot/Value/DateTime.pm ↑ Summary blib/lib/Dallycot/Tangle.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 67 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.8
33 67 my ( $engine, $options, $x, $n ) = @_;
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 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
58 2 my ( $engine, $options, $x ) = @_;
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 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
79 2 my ( $engine, $options, $x ) = @_;
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 9 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
100 9 my ( $engine, $options, $x ) = @_;
101  
102 9 my $d = deferred;
103  
104 9 ▶ if ( !$x->isa('Dallycot::Value::Numeric') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $x->isa('Dallycot::Value::Numeric')) { } 0 9 9
elsif ($x->value->is_int) { } 9 0 9
105 0 $d->reject("factorial expects a numeric argument");
106 }
107 elsif ( $x->value->is_int ) {
108 9 $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 9 return $d->promise;
116 9 };
117  
118 define
119 ceil => (
120 hold => 0,
121 arity => 1,
122 options => {}
123 ),
124 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
125 0 my ( $engine, $options, $x ) = @_;
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 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
146 0 my ( $engine, $options, $x ) = @_;
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 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
167 0 my ( $engine, $options, $x ) = @_;
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 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
188 0 my ( $engine, $options, $x, $y ) = @_;
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 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
211 0 my ( $engine, $options, $accuracy ) = @_;
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 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
233 0 my ( $engine, $options, $accuracy ) = @_;
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 4 ▶sub _simple_trig {
Subroutine _simple_trig: called CC=4 SCAR=16.5
256 4 my ( $name, $method, $engine, $options, $arg ) = @_;
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 $angle = $angle -> copy;
280 }
281 0 when ('gradians') {
282 0 $angle = $angle * Math::BigFloat->bpi( $accuracy + 10 ) / 200;
283 }
284 0 default {
285 0 $d->reject("units must be 'degrees', 'radians', or 'gradians'");
286 0 return $d;
287 }
288 }
289 # Clamp angle to -2pi .. 2pi
290 4 $angle->bmod(2 * Math::BigFloat -> bpi($accuracy));
291 4 my $answer = $angle->$method($accuracy);
292 4 $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->new($answer) ) );
293 4 return $d;
294 }
295  
296 define
297 sin => (
298 hold => 0,
299 arity => 1,
300 options => {
301 units => Dallycot::Value::String->new('degrees'),
302 accuracy => Dallycot::Value::Numeric->new( Math::BigRat->new(40) ),
303 }
304 ),
305 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
306 2 my ( $engine, $options, $arg ) = @_;
307  
308 2 return _simple_trig( 'sin', 'bsin', $engine, $options, $arg );
309 9 };
310  
311 define
312 cos => (
313 hold => 0,
314 arity => 1,
315 options => {
316 units => Dallycot::Value::String->new('degrees'),
317 accuracy => Dallycot::Value::Numeric->new( Math::BigRat->new(40) ),
318 }
319 ),
320 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 2 my ( $engine, $options, $arg ) = @_;
322  
323 2 return _simple_trig( 'cos', 'bcos', $engine, $options, $arg );
324 9 };
325  
326 9 define tan => <<'EOD';
327 (angle, units -> "degrees", accuracy -> 40) :> (
328 sin(angle, units -> units, accuracy -> accuracy) div
329 cos(angle, units -> units, accuracy -> accuracy)
330 )
331 EOD
332  
333 define
334 'arc-tan' => (
335 hold => 0,
336 arity => [ 1, 2 ],
337 options => {
338 units => Dallycot::Value::String->new('degrees'),
339 accuracy => Dallycot::Value::Numeric->new( Math::BigRat->new(40) ),
340 }
341 ),
342 3 ▶ sub {
Subroutine __ANON__: called CC=14 SCAR=31.2
343 3 my ( $engine, $options, $y, $x ) = @_;
344  
345 3 my $units;
346 3 my $d = deferred;
347  
348 3 ▶ if ( $options->{'units'}->isa('Dallycot::Value::String') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"units"}->isa("Dallycot::Value::String")) 3 0 3
349 3 $units = $options->{'units'}->value;
350 }
351 3 ▶ if ( !$options->{'accuracy'}->isa('Dallycot::Value::Numeric') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"accuracy"}->isa("Dallycot::Value::Numeric")) 0 3 3
352 0 $d->reject("accuracy must be a numeric value");
353 0 return $d->promise;
354 }
355 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 table50%2/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
356 0 $d->reject("arc-tan requires numeric arguments");
357 0 return $d->promise;
358 }
359 3 my $accuracy = $options->{'accuracy'}->value->as_int;
360  
361 3 $y = $y->value->as_float( $accuracy + 10 );
362 3 ▶ if ( defined $x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x) 2 1 3
363 2 $x = $x->value->as_float( $accuracy + 10 );
364 }
365  
366 3 my $angle;
367 3 ▶ if ( defined($x) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x) { } 2 1 3
368 2 my $key = 0;
369 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
370 2 ▶ $key |= 0x0c if $x->is_neg;
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 1 1 2
371 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
372 2 ▶ $key |= 0x03 if $y->is_neg;
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 1 1 2
373  
374 2 given ($key) {
375 2 when ( [ 0x04, 0x05, 0x07 ] ) { # $x > 0, $y anything
376 1 $angle = $y->batan2( $x, $accuracy );
377 }
378 1 when (0x0d) { # $x < 0, $y > 0
379 0 $angle = $y->batan2( $x, $accuracy + 1 ) + Math::BigFloat->bpi( $accuracy + 1 );
380 0 $angle->bround($accuracy);
381 }
382 1 when (0x0f) { # $x < 0, $y < 0
383 1 $angle = $y->batan2( $x, $accuracy + 1 ) - Math::BigFloat->bpi( $accuracy + 1 );
384 1 $angle->bround($accuracy);
385 }
386 0 when (0x01) { # $x = 0, $y > 0
387 0 $angle = Math::BigFloat->bpi( $accuracy + 1 ) / 2;
388 0 $angle->bround($accuracy);
389 }
390 0 when (0x03) { # $x = 0, $y < 0
391 0 $angle = -Math::BigFloat->bpi( $accuracy + 1 ) / 2;
392 0 $angle->bround($accuracy);
393 }
394 0 default {
395 0 $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->nan ) );
396 0 return $d->promise;
397 }
398 }
399 }
400 else {
401 1 $angle = $y->batan($accuracy);
402 }
403  
404 3 given ($units) {
405 3 when ('degrees') {
406 3 $angle = $angle * 180 / Math::BigFloat->bpi($accuracy);
407 }
408 0 when ('radians') { }
409 0 when ('gradians') {
410 0 $angle = $angle * 200 / Math::BigFloat->bpi($accuracy);
411 }
412 0 default {
413 0 $d->reject("units must be 'degrees', 'radians', or 'gradians'");
414 0 return $d;
415 }
416 }
417 3 $d->resolve( Dallycot::Value::Numeric->new( Math::BigRat->new($angle) ) );
418 3 return $d;
419 9 };
420  
421 define
422 gcd => (
423 hold => 0,
424 arity => [2],
425 options => {}
426 ),
427 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
428 3 my ( $engine, $options, @values ) = @_;
429  
430 3 my $d = deferred;
431  
432 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
433 3 $d->resolve( Dallycot::Value::Numeric->new( Math::BigInt::bgcd( map { $_->value->as_int } @values ) ) );
434 }
435 else {
436 0 $d->reject("gcd requires numeric values");
437 }
438 3 return $d->promise;
439 9 };
440  
441 define
442 lcm => (
443 hold => 0,
444 arity => [2],
445 options => {}
446 ),
447 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
448 0 my ( $engine, $options, @values ) = @_;
449  
450 0 my $d = deferred;
451  
452 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
453 0 $d->resolve( Dallycot::Value::Numeric->new( Math::BigInt::blcm( map { $_->value->as_int } @values ) ) );
454 }
455 else {
456 0 $d->reject("gcd requires numeric values");
457 }
458 0 return $d->promise;
459 9 };
460  
461 define
462 random => (
463 hold => 0,
464 arity => 1,
465 options => { 'use-internet' => Dallycot::Value::Boolean->new(0), }
466 ),
467 1 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=29.2
468 1 my ( $engine, $options, $spec ) = @_;
469  
470 1 my $d = deferred;
471 1 my %bounds;
472  
473 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
474 && $options->{'use-internet'}->value )
475 {
476 0 $bounds{use_internet} = 1;
477 }
478  
479 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
480 0 ▶ if ( @$spec != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$spec != 2) 0 0 0
481 0 $d->reject("random requires min and max bounds with a vector argument");
482 0 return $d->promise;
483 }
484 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
485 0 $bounds{min} = $spec->[0]->value->as_int->bstr;
486 0 $bounds{max} = $spec->[1]->value->as_int->bstr;
487 }
488 else {
489 0 $d->reject("random requires numeric min/max bounds");
490 0 return $d->promise;
491 }
492 }
493 elsif ( $spec->isa('Dallycot::Value::Numeric') ) {
494 1 $bounds{max} = $spec->value->as_int->bstr;
495 }
496 else {
497 0 $d->reject("random requires a numeric bound");
498 0 return $d->promise;
499 }
500  
501 1 my $random = Math::BigInt::Random::random_bigint(%bounds);
502  
503 1 $d->resolve( Dallycot::Value::Numeric->new($random) );
504 1 return $d->promise;
505 9 };
506  
507 9 define sum => 'foldl(0, { #1 + #2 }/2, _)';
508  
509 9 define product => 'foldl(1, { #1 * #2 }/2, _)';
510  
511 9 define min => <<'EOD';
512 foldl1({(
513 (#1 < #2) : #1
514 ( ) : #2
515 )}/2, _)
516 EOD
517  
518 9 define max => <<'EOD';
519 foldl1({(
520 (#1 > #2) : #1
521 ( ) : #2
522 )}/2, _)
523 EOD
524  
525 9 define 'weighted-count-and-sum' => <<'EOD';
526 foldl( <0,0>, (
527 (pad, element) :>
528 <pad[1] + element[1], pad[2] + element[1] * element[2]>
529 ), _)
530 EOD
531  
532 9 define 'count-and-sum' => <<'EOD';
533 foldl( <0,0>, (
534 (pad, element) :>
535 < pad[1] + 1, pad[2] + element >
536 ), _)
537 EOD
538  
539 9 define mean => <<'EOD';
540 (s) :> ({ #[2] div #[1] } @ count-and-sum(s))
541 EOD
542  
543 9 define differences => <<'EOD';
544 (stream, d = 1) :> (
545 nest( y-combinator(
546 (self, s) :> (
547 (?(s') and ?(s...)) : [ s...' - s', self(self, s...) ]
548 (?(s') ) : [ -s' ]
549 ( ) : [ ]
550 )
551 ), d)(stream)
552 )
553 EOD
554  
555 9 define 'make-evens' => '() :> ({ # * 2 } @ 1..)';
556  
557 9 define 'make-odds' => '() :> ({ # * 2 + 1 } @ 0..)';
558  
559 9 define evens => 'make-evens()';
560  
561 9 define odds => 'make-odds()';
562  
563 9 define primes => <<'EOD';
564 sieve := y-combinator( (self, s) :> [ s', self(self, ~divisible-by?(_, s') % s...) ] );
565 [ 1, 2, sieve(make-odds()...) ]
566 EOD
567  
568 9 define 'prime-pairs' => 'primes Z primes...';
569  
570 9 define 'twin-primes' => '{ #[2] - #[1] = 2 } % prime-pairs';
571  
572 9 define factorials => 'factorial @ 1..';
573  
574 9 define 'fibonacci-sequence' => <<'EOD';
575 [ 1, 1, y-combinator((self, a, b) :> [ a + b, self(self, b, a+b) ])(1, 1) ]
576 EOD
577  
578 9 define 'leonardo-sequence' => <<'EOD';
579 [ 1, 1, y-combinator((self, a, b) :> [ a + b + 1, self(self, b, a + b + 1) ])(1, 1) ]
580 EOD
581  
582 9 define prime => '(n) :> primes[n]';
583 9 define fibonacci => '(n) :> fibonacci-sequence[n]';
584 9 define leonardo => '(n) :> leonardo-sequence[n]';
585  
586 9 1;
« blib/lib/Dallycot/Value/DateTime.pm ↑ Summary blib/lib/Dallycot/Tangle.pm »