File Coverage

blib/lib/Bio/Gonzales/Util/Math.pm
Criterion Covered Total %
statement 28 35 80.0
branch 1 4 25.0
condition 0 2 0.0
subroutine 8 9 88.8
pod 1 2 50.0
total 38 52 73.0


line stmt bran cond sub pod time code
1             package Bio::Gonzales::Util::Math;
2              
3 1     1   114260 use warnings;
  1         12  
  1         35  
4 1     1   6 use strict;
  1         2  
  1         19  
5 1     1   4 use Carp;
  1         2  
  1         51  
6              
7 1     1   24 use 5.010;
  1         3  
8              
9 1     1   1370 use Math::Combinatorics;
  1         3442  
  1         75  
10 1     1   7 use List::Util;
  1         2  
  1         56  
11              
12 1     1   6 use base 'Exporter';
  1         4  
  1         439  
13             our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
14             our $VERSION = '0.083'; # VERSION
15              
16             @EXPORT = qw();
17             %EXPORT_TAGS = ();
18             @EXPORT_OK = qw(combine_alphabet shuffle);
19              
20             sub combine_alphabet {
21 0     0 1 0 my ($len) = @_;
22 0   0     0 $len //= 2;
23 0         0 my @n = ( 'a' .. 'z' );
24 0         0 my @c = combine( $len, @n );
25 0         0 return map { join "", @$_ } @c;
  0         0  
26             }
27              
28             sub shuffle {
29 100000     100000 0 1982975 my $d = shift;
30 100000 50       177236 if ( ref $d eq 'HASH' ) {
    0          
31              
32 100000         123060 my %shuffled;
33              
34 100000         222764 my @keys = keys %$d;
35              
36 100000         233476 my @key_idcs = List::Util::shuffle( 0 .. $#keys );
37 100000         203714 for ( my $i = 0; $i < @keys; $i++ ) {
38              
39 1000000         2058890 $shuffled{ $keys[ $key_idcs[$i] ] } = $d->{ $keys[$i] };
40             }
41 100000         263693 return \%shuffled;
42             } elsif ( ref $d eq 'ARRAY' ) {
43 0           return [ List::Util::shuffle @$d ];
44             }
45              
46             }
47              
48             1;
49              
50             __END__
51              
52             =head1 NAME
53              
54             Bio::Gonzales::Util::Math::Util
55              
56             =head1 SYNOPSIS
57              
58             use Bio::Gonzales::Util::Math::Util qw/combine_alphabet/;
59              
60             =head1 DESCRIPTION
61              
62             =head1 SUBROUTINES
63              
64             =over 4
65              
66             =item B<< @character_combinations = combine_alphabet($length) >>
67              
68             Combine alphabetic characters from a-z into a sequence of strings, e.g.
69              
70             @c = combine_alphabet(3);
71              
72             results in
73              
74             abc
75             aca
76             aac
77             ...
78             zzz
79              
80             =back
81              
82             =head1 SEE ALSO
83              
84             =head1 AUTHOR
85              
86             jw bargsten, C<< <joachim.bargsten at wur.nl> >>
87              
88             =cut