line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Array::Sample::Partition; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
53452
|
use 5.010001; |
|
1
|
|
|
|
|
10
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
5
|
use Exporter qw(import); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
183
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
10
|
|
|
|
|
|
|
our $DATE = '2022-05-20'; # DATE |
11
|
|
|
|
|
|
|
our $DIST = 'Array-Sample-Partition'; # DIST |
12
|
|
|
|
|
|
|
our $VERSION = '0.003'; # VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw(sample_partition); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub sample_partition { |
17
|
14
|
|
|
14
|
1
|
3086
|
my ($ary, $n, $opts) = @_; |
18
|
14
|
|
100
|
|
|
53
|
$opts //= {}; |
19
|
|
|
|
|
|
|
|
20
|
14
|
100
|
|
|
|
29
|
$n = @$ary if $n > @$ary; |
21
|
|
|
|
|
|
|
|
22
|
14
|
|
|
|
|
17
|
my @res; |
23
|
14
|
|
|
|
|
28
|
for my $i (1..$n) { |
24
|
19
|
|
|
|
|
35
|
my $idx = int($i*@$ary/($n+1)); |
25
|
19
|
100
|
|
|
|
45
|
push @res, $opts->{pos} ? $idx : $ary->[$idx]; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
14
|
|
|
|
|
59
|
@res; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
1; |
32
|
|
|
|
|
|
|
# ABSTRACT: Sample elements from an array by equal partitions |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
__END__ |