| 1 | package JQ::Lite::Builtin::Aggregate; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 182 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 182 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 182 | ▶ | use List::Util qw(sum min max); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 182 | ▶ | use JQ::Lite::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 136 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 136 | my ($class, $register) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | $register->(['add', 'sum'], sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 13 | 2 | my ($owner, $inputs) = @_; | |||||||||||||||||||||||||||
| 14 | 2 | ▶ | return [ map { ref($_) eq 'ARRAY' ? sum(map { 0 + $_ } @{$_}) : $_ } @{$inputs} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 136 | }); | |||||||||||||||||||||||||||
| 16 | 1 | ▶ | $register->('product', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 17 | 1 | my ($owner, $inputs) = @_; | |||||||||||||||||||||||||||
| 18 | return [ map { | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | if (ref($_) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | my ($product, $seen) = (1, 0); | |||||||||||||||||||||||||||
| 21 | 1 | for my $value (@{$_}) { | |||||||||||||||||||||||||||
| 22 | 3 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 3 | $product *= 0 + $value; | |||||||||||||||||||||||||||
| 24 | 3 | $seen = 1; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | $seen ? $product : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 0 | else { $_ } | |||||||||||||||||||||||||||
| 29 | 1 | } @{$inputs} ]; | |||||||||||||||||||||||||||
| 30 | 136 | }); | |||||||||||||||||||||||||||
| 31 | 136 | $register->('min', _extreme(\&min)); | |||||||||||||||||||||||||||
| 32 | 136 | $register->('max', _extreme(\&max)); | |||||||||||||||||||||||||||
| 33 | 1 | ▶ | $register->('avg', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 34 | 1 | my ($owner, $inputs) = @_; | |||||||||||||||||||||||||||
| 35 | 1 | return [ map { ref($_) eq 'ARRAY' && @{$_} | |||||||||||||||||||||||||||
| 36 | 1 | ▶ | ? sum(map { 0 + $_ } @{$_}) / scalar(@{$_}) : 0 } @{$inputs} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ eq "ARRAY" and @{$_;}
MC/DC0%0/2
ref $_ eq "ARRAY" and @{$_;}
ref $_ eq "ARRAY"
@{$_;}
Truth table33%1/3
A and B
A= ref $_ eq "ARRAY"B= @{$_;}
| |||||||||||||||||||||||||||||
| 37 | 136 | }); | |||||||||||||||||||||||||||
| 38 | 5 | ▶ | $register->('median', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 39 | 5 | my ($owner, $inputs) = @_; | |||||||||||||||||||||||||||
| 40 | return [ map { | ||||||||||||||||||||||||||||
| 41 | 5 | ▶ | if (ref($_) eq 'ARRAY' && @{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ eq "ARRAY" and @{$_;}
MC/DC0%0/2
ref $_ eq "ARRAY" and @{$_;}
ref $_ eq "ARRAY"
@{$_;}
Truth table33%1/3
A and B
A= ref $_ eq "ARRAY"B= @{$_;}
| |||||||||||||||||||||||||||||
| 42 | 5 | my @numbers = sort { $a <=> $b } | |||||||||||||||||||||||||||
| 43 | JQ::Lite::Util::_extract_numeric_values($_); | ||||||||||||||||||||||||||||
| 44 | 5 | ▶ | if (@numbers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 4 | my $middle = int(@numbers / 2); | |||||||||||||||||||||||||||
| 46 | 4 | ▶ | @numbers % 2 ? $numbers[$middle] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | : ($numbers[$middle - 1] + $numbers[$middle]) / 2; | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 1 | else { undef } | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | else { $_ } | |||||||||||||||||||||||||||
| 52 | 5 | } @{$inputs} ]; | |||||||||||||||||||||||||||
| 53 | 136 | }); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 272 | ▶ | sub _extreme { | ||||||||||||||||||||||||||
|
Subroutine
_extreme: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 272 | my ($function) = @_; | |||||||||||||||||||||||||||
| 58 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 59 | 6 | my ($owner, $inputs) = @_; | |||||||||||||||||||||||||||
| 60 | return [ map { | ||||||||||||||||||||||||||||
| 61 | 6 | ▶ | ref($_) eq 'ARRAY' ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 6 | my @numbers = JQ::Lite::Util::_extract_numeric_values($_); | |||||||||||||||||||||||||||
| 63 | 6 | ▶ | @numbers ? $function->(@numbers) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } : $_ | ||||||||||||||||||||||||||||
| 65 | 6 | } @{$inputs} ]; | |||||||||||||||||||||||||||
| 66 | 272 | }; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 182 | 1; | |||||||||||||||||||||||||||