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