File Coverage

blib/lib/Util/Underscore/ListUtils.pm
Criterion Covered Total %
statement 86 122 70.4
branch 39 70 55.7
condition 5 12 41.6
subroutine 6 6 100.0
pod n/a
total 136 210 64.7


line stmt bran cond sub pod time code
1             package Util::Underscore::ListUtils;
2              
3             #ABSTRACT: Interface to List::Util and List::MoreUtils
4              
5 12     12   42 use strict;
  12         14  
  12         277  
6 12     12   38 use warnings;
  12         14  
  12         4399  
7              
8             ## no critic (ProhibitMultiplePackages)
9             package # hide from PAUSE
10             _;
11              
12             ## no critic (ProhibitSubroutinePrototypes)
13              
14              
15             # this function generates max_by, max_str_by, min_by, min_str_by
16             # It takes the proper comparison operators as arguments.
17             # For max_*: lt, gt
18             # For min_*: gt, lt
19             my $minmax_by = sub {
20             my ($lt, $gt) = @_;
21             ## no critic (ProhibitStringyEval)
22 1 50 33 1   519 return eval q~#line ~ . (__LINE__ + 1) . q~
  1 50 66     6  
  1 0 33     4  
  1 0 33     2  
  0 50       0  
  0 0       0  
  0 50       0  
  0 100       0  
  0 100       0  
  0 100       0  
  0 50       0  
  0 100       0  
  0 50       0  
  0 100       0  
  1 50       2  
  1 50       2  
  1 0       2  
  1 0       2  
  1 100       22  
  2 50       3  
  2 50       39  
  0 50       0  
  0 50       0  
  1 0       5  
  9 0       4167  
  9 100       39  
  6 50       17  
  4 50       8  
  2         2  
  2         5  
  2         4  
  2         40  
  2         4  
  6         8  
  6         146  
  2         5  
  2         3  
  2         9  
  2         2  
  2         3  
  2         4  
  2         4  
  2         41  
  5         6  
  5         88  
  2         5  
  2         3  
  2         7  
  1         546  
  1         10  
  1         3  
  1         4  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         1  
  1         2  
  1         1  
  1         2  
  1         21  
  2         4  
  2         36  
  1         3  
  1         1  
  1         4  
  1         513  
  1         10  
  1         3  
  1         2  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         1  
  1         2  
  1         3  
  1         25  
  2         2  
  2         40  
  1         3  
  1         2  
  1         4  
23             sub (&@) {
24             my $key_func = shift;
25             return if not @_ or not defined wantarray;
26             return $_[0] if not @_ > 1;
27             if (wantarray) {
28             my $max_key = do {
29             local *_ = \$_[0];
30             $key_func->();
31             };
32             my @max_elems = shift;
33             for (@_) {
34             my $key = $key_func->();
35             next if $key ~ . $lt . q~ $max_key;
36             $max_key = $key if $key ~ . $gt . q~ $max_key;
37             push @max_elems, $_;
38             }
39             return @max_elems;
40             }
41             else {
42             my $max_elem = \shift;
43             my $max_key = do {
44             local *_ = $max_elem;
45             $key_func->();
46             };
47             for (@_) {
48             my $key = $key_func->();
49             next if $key ~ . $lt . q~ $max_key;
50             $max_key = $key if $key ~ . $gt . q~ $max_key;
51             $max_elem = \$_;
52             }
53             return $$max_elem;
54             }
55             }
56             ~;
57             };
58              
59             *max_by = $minmax_by->(qw( < > ));
60             *max_str_by = $minmax_by->(qw( lt gt ));
61             *min_by = $minmax_by->(qw( > < ));
62             *min_str_by = $minmax_by->(qw( gt lt ));
63              
64              
65             sub uniq_by (&@) {
66 4     4   2327 my $key_func = shift;
67 4 100       12 return if not @_;
68 3 50       6 if (not defined wantarray) {
69 0         0 Carp::carp "Useless use of _::uniq_by in void context";
70 0         0 return;
71             }
72 3 100       6 if (@_ == 1) {
73 1 50       6 return (wantarray) ? @_ : 1;
74             }
75              
76             # caller context is propagated to grep, so this does the right thing.
77 2         3 my %seen;
78 2         3 grep { not $seen{ $key_func->() }++ } @_;
  12         33  
79             }
80              
81              
82             sub classify (&@) {
83 3     3   635 my $key_func = shift;
84 3 100       10 return if not @_;
85 2 50       4 if (not defined wantarray) {
86 0         0 Carp::carp "Useless use of _::classify in void context";
87 0         0 return;
88             }
89 2         3 my %categories;
90 2         5 push @{ $categories{ $key_func->() } }, $_ for @_;
  12         36  
91 2 100       24 (wantarray) ? %categories : \%categories;
92             }
93              
94              
95             ## no critic (ProtectPrivateVars)
96             $Util::Underscore::_ASSIGN_ALIASES->(
97             'List::Util',
98             reduce => 'reduce',
99             any => 'any',
100             all => 'all',
101             none => 'none',
102             max => 'max',
103             max_str => 'maxstr',
104             min => 'min',
105             min_str => 'minstr',
106             sum => 'sum',
107             product => 'product',
108             pairgrep => 'pairgrep',
109             pairfirst => 'pairfirst',
110             pairmap => 'pairmap',
111             shuffle => 'shuffle',
112             );
113              
114             ## no critic (ProtectPrivateVars)
115             $Util::Underscore::_ASSIGN_ALIASES->(
116             'List::MoreUtils',
117             first => 'first_value',
118             first_index => 'first_index',
119             last => 'last_value',
120             last_index => 'last_index',
121             natatime => 'natatime',
122             uniq => 'uniq',
123             part => 'part',
124             each_array => 'each_arrayref',
125             );
126              
127             sub zip {
128 1     1   8445 goto &List::MoreUtils::zip; # adios, prototypes!
129             }
130              
131             1;
132              
133             __END__