forked from EntropyOrg/p5-Data-Frame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enumerable.pm
55 lines (45 loc) · 1.09 KB
/
Enumerable.pm
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
package PDL::Role::Enumerable;
use strict;
use warnings;
use Hash::Ordered;
use Hash::Ordered::Extension;
use Moo::Role;
use Try::Tiny;
use List::AllUtils ();
with qw(PDL::Role::Stringifiable);
has _levels => ( is => 'rw', default => sub { Hash::Ordered->new; } );
sub element_stringify_max_width {
my ($self, $element) = @_;
my @where_levels = @{ $self->{PDL}->uniq->unpdl };
my @which_levels = @{ $self->levels }[@where_levels];
my @lengths = map { length $_ } @which_levels;
List::AllUtils::max( @lengths );
}
sub element_stringify {
my ($self, $element) = @_;
( $self->_levels->keys )[ $element ];
}
sub number_of_levels {
my ($self) = @_;
scalar $self->_levels->keys;
}
sub levels {
my ($self, @levels) = @_;
if( @levels ) {
try {
$self->_levels->RenameKeys( @levels );
} catch {
die "incorrect number of levels" if /@{[ Hash::Ordered::ERROR_KEY_LENGTH_MISMATCH ]}/;
};
}
[ $self->_levels->keys ];
}
around qw(slice uniq dice) => sub {
my $orig = shift;
my ($self) = @_;
my $ret = $orig->(@_);
# TODO _levels needs to be copied
$ret->_levels( $self->_levels );
$ret;
};
1;