Data-Frame

 view release on metacpan or  search on metacpan

lib/Data/Frame/Autobox.pm  view on Meta::CPAN

}

sub at {
    my ($array, $index) = @_;
    $array->[$index];
}
 
sub uniq { [ List::AllUtils::uniq(@{$_[0]}) ] }

sub set {
    my ($array, $index, $value) = @_;
    $array->[$index] = $value;
}

sub length { CORE::scalar @{$_[0]} }
sub elems { CORE::scalar @{$_[0]} }
 
sub flatten  { @{$_[0]} }
 
sub slice {
    my ($array, $indices) = @_;
    [ @{$array}[ @{$indices} ] ];
}


sub copy { [ @{$_[0]} ] }

sub repeat {
    my ($array, $n) = @_;
    return [ (@$array) x $n ];
}

sub repeat_to_length {
    my ($array, $l) = @_;
    return $array if @$array == 0;
    my $x = repeat($array, ceil($l / @$array));
    return [ @$x[0 .. $l-1] ];
}
 
sub intersect {
    my ($array, $other) = @_;
    my %hash = map { $_ => 1 } @$array;
    return [ grep { exists $hash{$_} } @$other ];
}

sub union {
    my ($array, $other) = @_;
    return [ List::AllUtils::uniq( @$array, @$other ) ];
}

sub setdiff {
    my ($array, $other) = @_;
    my %hash = map { $_ => 1 } @$other;
    return [ grep { not exists( $hash{$_} ) } @$array ];
}
 

package Data::Frame::Autobox::HASH;
$Data::Frame::Autobox::HASH::VERSION = '0.006005';
use Carp;
use Ref::Util;
use List::AllUtils qw(pairmap);


sub isempty { keys %{ $_[0] } == 0 } 

sub delete {
    my ($hash, $key) = @_;
    CORE::delete $hash->{$key};
}
 
sub merge {
    my ($left, $right) = @_;
    Carp::confess "You must pass a hashref as argument to merge"
        unless ref $right eq 'HASH';
    return { %$left, %$right };
}
 
sub hslice {
    my ($hash, $keys) = @_;
    return { map { $_ => $hash->{$_} } @$keys };
}

sub flatten { %{$_[0]} }

sub at {
    my ($hash, $index) = @_;
    $hash->{$index};
}
 
sub set {
    my ($hash, $index, $value) = @_;
    $hash->{$index} = $value;
}
 
sub exists {
    my ($hash, $key) = @_;
    CORE::exists $hash->{$key};
}
 
sub keys { [ CORE::keys %{$_[0]} ] }
sub names { [ CORE::keys %{$_[0]} ] }
 
sub values {
    my ($hash) = @_;
    [ CORE::values %$hash ];
}
 

sub copy { { %{ $_[0] } } }

sub rename {
    my ( $hash, $href_or_coderef ) = @_;

    my %new_hash;
    if ( Ref::Util::is_coderef($href_or_coderef) ) {
        %new_hash = pairmap { ( $href_or_coderef->($a) // $a ) => $b } %$hash;
    }
    else {
        %new_hash = pairmap { ( $href_or_coderef->{$a} // $a ) => $b } %$hash;
    }
    return \%new_hash;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Data::Frame::Autobox - Autobox arrays and hashes for Data::Frame

=head1 VERSION

version 0.006005

=head1 SYNOPSIS

    use Data::Frame::Autobox;

    [ 1 .. 5 ]->isempty;    # false

    { one => 1 }->names;    # [ 'one' ]
    { one => 1 }->isempty;  # false

=head1 DESCRIPTION

This package provides a set of methods for autoboxed arrays and hashes.

=head1 ARRAY METHODS

=head2 isempty

    my $isempty = $array->isempty;

Returns a boolean value for if the array ref is empty.

=head2 grep

    my $new_array = $array->grep($coderef);

=head2 map

    my $new_array = $array->map($coderef);

=head2 at

    my $value = $array->at($idx);

=head2 uniq

    my $uniq_array = $array->uniq;



( run in 0.947 second using v1.01-cache-2.11-cpan-39bf76dae61 )