File Coverage

blib/lib/Array/Sample/Partition.pm
Criterion Covered Total %
statement 19 19 100.0
branch 4 4 100.0
condition 2 2 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
1             package Array::Sample::Partition;
2              
3             our $DATE = '2017-06-14'; # DATE
4             our $VERSION = '0.001'; # VERSION
5              
6 1     1   15803 use 5.010001;
  1         4  
7 1     1   6 use strict;
  1         2  
  1         18  
8 1     1   9 use warnings;
  1         2  
  1         27  
9              
10 1     1   5 use Exporter qw(import);
  1         2  
  1         123  
11             our @EXPORT_OK = qw(sample_partition);
12              
13             sub sample_partition {
14 14     14 1 2609 my ($ary, $n, $opts) = @_;
15 14   100     120 $opts //= {};
16              
17 14 100       34 $n = @$ary if $n > @$ary;
18              
19 14         24 my @res;
20 14         33 for my $i (1..$n) {
21 19         43 my $idx = int($i*@$ary/($n+1));
22 19 100       67 push @res, $opts->{pos} ? $idx : $ary->[$idx];
23             }
24              
25 14         71 @res;
26             }
27              
28             1;
29             # ABSTRACT: Sample elements from an array by equal partitions
30              
31             __END__