blib/lib/perl5i/2/ARRAY.pm untested

filter: Statementstmt 0.0% 0 / 205 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 23 Podpod n/a totaltotal 0.0% 0 / 336 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # vi: set ts=4 sw=4 ht=4 et :
2 package perl5i::2::ARRAY;
3 use 5.010;
4  
5 use strict;
6 use warnings;
7 no if $] >= 5.018000, warnings => 'experimental::smartmatch';
8  
9 # Don't accidentally turn carp/croak into methods.
10 require Carp::Fix::1_25;
11  
12 use perl5i::2::Signatures;
13 use perl5i::2::autobox;
14  
15 # A foreach which honors the number of parameters in the signature
16 method foreach($code) {
17 my $n = 1;
18 if( my $sig = $code->signature ) {
19 $n = $sig->num_positional_params;
20 Carp::Fix::1_25::croak("Function passed to foreach takes no arguments") unless $n;
21 }
22  
23 my $idx = 0;
24 while ( $idx <= $#{$self} ) {
25 $code->(@{$self}[$idx..($idx+$n-1)]);
26 $idx += $n;
27 }
28  
29 return;
30 }
31  
32 method first($filter) {
33 # Deep recursion and segfault (lines 90 and 91 in first.t) if we use
34 # the same elegant approach as in grep().
35 if ( ref $filter eq 'Regexp' ) {
36 return List::Util::first( sub { $_ ~~ $filter }, @$self );
37 }
38  
39 return List::Util::first( sub { $filter->() }, @$self );
40  
41 }
42  
43 method map( $code ) {
44 my @result = CORE::map { $code->($_) } @$self;
45  
46 return wantarray ? @result : \@result;
47 }
48  
49 method as_hash{
50 my %result = CORE::map { $_ => 1 } @$self;
51 return wantarray ? %result : \%result;
52 }
53  
54  
55 method pick ( $num ){
56 Carp::Fix::1_25::croak("pick() takes the number of elements to pick")
57 unless defined $num;
58 Carp::Fix::1_25::croak("pick() takes a positive integer or zero, not '$num'")
59 unless $num->is_integer && ($num->is_positive or $num == 0);
60
61 if($num >= @$self){
62 my @result = List::Util::shuffle(@$self);
63 return wantarray ? @result : \@result;
64 }
65
66 # for the first position in the array, generate a random number that gives
67 # that element an n/N chance of being picked (where n is the number of elements to pick and N is the total array size);
68 # repeat for the rest of the array, each time altering the probability of
69 # the element being picked to reflect the number of elements picked so far and the number left.
70 my $num_left = @$self;
71 my @result;
72 my $i=0;
73 while($num > 0){
74 my $rand = int(rand($num_left));
75 if($rand < $num){
76 push(@result, $self->[$i]);
77 $num--;
78 }
79 $num_left--;
80 $i++;
81 }
82  
83 # Don't return the picks in the same order as the original array
84 # Simulates what would happen if you shuffled first
85 @result = @result->shuffle;
86  
87 return wantarray ? @result : \@result;
88 }
89  
90  
91 method pick_one() {
92 return @$self[int rand @$self];
93 }
94  
95  
96 method grep($filter) {
97 my @result = CORE::grep { $_ ~~ $filter } @$self;
98  
99 return wantarray ? @result : \@result;
100 }
101  
102 method popn($times) {
103 Carp::Fix::1_25::croak("popn() takes the number of elements to pop")
104 unless defined $times;
105 Carp::Fix::1_25::croak("popn() takes a positive integer or zero, not '$times'")
106 unless $times->is_integer && ($times->is_positive or $times == 0);
107  
108 # splice() will choke if you walk off the array, so rein it in
109 $times = scalar(@$self) if ($times > scalar(@$self));
110  
111 my @result = splice(@$self, -$times, $times);
112 return wantarray ? @result : \@result;
113 }
114  
115 method shiftn($times) {
116 Carp::Fix::1_25::croak("shiftn() takes the number of elements to shift")
117 unless defined $times;
118 Carp::Fix::1_25::croak("shiftn() takes a positive integer or zero, not '$times'")
119 unless $times->is_integer && ($times->is_positive or $times == 0);
120  
121 # splice() will choke if you walk off the array, so rein it in
122 $times = scalar(@$self) if ($times > scalar(@$self));
123  
124 my @result = splice(@$self, 0, $times);
125 return wantarray ? @result : \@result;
126 }
127  
128 sub all {
129 require List::MoreUtils;
130 return &List::MoreUtils::all($_[1], @{$_[0]});
131 }
132  
133 sub any {
134 require List::MoreUtils;
135 return &List::MoreUtils::any($_[1], @{$_[0]});
136 }
137  
138 sub none {
139 require List::MoreUtils;
140 return &List::MoreUtils::none($_[1], @{$_[0]});
141 }
142  
143 sub true {
144 require List::MoreUtils;
145 return &List::MoreUtils::true($_[1], @{$_[0]});
146 }
147  
148 sub false {
149 require List::MoreUtils;
150 return &List::MoreUtils::false($_[1], @{$_[0]});
151 }
152  
153 sub uniq {
154 require List::MoreUtils;
155 my @uniq = List::MoreUtils::uniq(@{$_[0]});
156 return wantarray ? @uniq : \@uniq;
157 }
158  
159 sub minmax {
160 require List::MoreUtils;
161 my @minmax = List::MoreUtils::minmax(@{$_[0]});
162 return wantarray ? @minmax : \@minmax;
163 }
164  
165 sub mesh {
166 require List::MoreUtils;
167 my @mesh = &List::MoreUtils::zip(@_);
168 return wantarray ? @mesh : \@mesh;
169 }
170  
171 # Compare differences between two arrays.
172 my $diff_two_deeply = func($c, $d) {
173 my $diff = [];
174  
175 # For each element of $c, try to find if it is equal to any of the
176 # elements of $d. If not, it's unique, and has to be pushed into
177 # $diff.
178  
179 require perl5i::2::equal;
180 require List::MoreUtils;
181 foreach my $item (@$c) {
182 unless (
183 List::MoreUtils::any( sub { perl5i::2::equal::are_equal( $item, $_ ) }, @$d )
184 )
185 {
186 push @$diff, $item;
187 }
188 }
189  
190 return $diff;
191 };
192  
193 my $diff_two_simply = func($c, $d) {
194 no warnings 'uninitialized';
195 my %seen = map { $_ => 1 } @$d;
196  
197 my @diff = grep { not $seen{$_} } @$c;
198  
199 return \@diff;
200 };
201  
202 method diff(@rest) {
203 unless (@rest) {
204 return wantarray ? @$self : $self;
205 }
206  
207 require List::MoreUtils;
208  
209 my $has_refs = List::MoreUtils::any(sub { ref $_ }, @$self);
210  
211 my $diff_two = $has_refs ? $diff_two_deeply : $diff_two_simply;
212  
213 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
214 # ssasign ... "
215 die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
216  
217 foreach my $array (@rest) {
218 $self = $diff_two->($self, $array);
219 }
220  
221 return wantarray ? @$self : $self;
222 }
223  
224  
225 my $intersect_two_simply = func($c, $d) {
226 no warnings 'uninitialized';
227 my %seen = map { $_ => 1 } @$d;
228  
229 my @intersect = grep { $seen{$_} } @$c;
230  
231 return \@intersect;
232 };
233  
234 # Compare differences between two arrays.
235 my $intersect_two_deeply = func($c, $d) {
236 require perl5i::2::equal;
237  
238 my $intersect = [];
239  
240 # For each element of $c, try to find if it is equal to any of the
241 # elements of $d. If it is, it's shared, and has to be pushed into
242 # $intersect.
243  
244 require List::MoreUtils;
245 foreach my $item (@$c) {
246 if (
247 List::MoreUtils::any( sub { perl5i::2::equal::are_equal( $item, $_ ) }, @$d )
248 )
249 {
250 push @$intersect, $item;
251 }
252 }
253  
254 return $intersect;
255 };
256  
257 method intersect(@rest) {
258 unless (@rest) {
259 return wantarray ? @$self : $self;
260 }
261  
262 require List::MoreUtils;
263 my $has_refs = List::MoreUtils::any(sub { ref $_ }, @$self);
264  
265 my $intersect_two = $has_refs ? $intersect_two_deeply : $intersect_two_simply;
266  
267 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
268 # ssasign ... "
269 die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
270  
271 foreach my $array (@rest) {
272 $self = $intersect_two->($self, $array);
273 }
274  
275 return wantarray ? @$self : $self;
276 }
277  
278 method ltrim($charset) {
279 my @result = CORE::map { $_->ltrim($charset) } @$self;
280  
281 return wantarray ? @result : \@result;
282 }
283  
284 method rtrim($charset) {
285 my @result = CORE::map { $_->rtrim($charset) } @$self;
286  
287 return wantarray ? @result : \@result;
288 }
289  
290 method trim($charset) {
291 my @result = CORE::map { $_->trim($charset) } @$self;
292  
293 return wantarray ? @result : \@result;
294 }
295  
296  
297 1;