Array-APX

 view release on metacpan or  search on metacpan

lib/Array/APX.pm  view on Meta::CPAN

table without the 1-column/row:

    my $f = sub { $_[0] * $_[1] }; # We need an outer product
    ... $x |$f| $x ...

The |-operator is used here as the generalized outer-'product'-operator 
(if applied to two APX data structures it would act as the bitwise binary or) 
- it expects a 
function reference like $f in the example above. Thus it is possible to
create any outer 'products' - not necessarily based on multiplication only.
Using the vector stored in $x and this two dimensional matrix, the 
in-method is used to derive a boolean vector that contains a 1 at every 
place corresponding to an element on the left hand operand that is contained
in the right hand operand. This boolean vector is then inverted using the
overloaded !-operator:

  !($x = iota(99) + 2)->in($x |$f| $x)

Using the select-method this boolean vector is used to select the elements
corresponding to places marked with 1 from the original vector $x thus 
yielding a vector of prime numbers between 2 and 100:

    print $x->select(!($x = iota(199) + 2)->in($x |$f| $x));

=cut

use strict;
use warnings;

require Exporter;
our @ISA         = qw(Exporter);
our @EXPORT_OK   = qw(dress iota);
our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] );

our $VERSION = 0.6;

use Data::Dumper;
#use Array::DeepUtils qw(:all);
use Array::DeepUtils;
use Carp;

# The following operators will be generated automatically:
my %binary_operators = (
    '+'  => 'add',
    '*'  => 'multiply',
    '-'  => 'subtract',
    '%'  => 'mod',
    '**' => 'power',
    '&'  => 'bitwise_and',
    '^'  => 'bitwise_xor',
);

# Overload everything defined in %binary_operators:
eval "use overload '$_' => '$binary_operators{$_}';" 
    for keys(%binary_operators);

# Binary operators with trick (0 instead of '' or undef) - these will be generated
# automatically, too:
my %special_binary_operators = (
    '==' => 'numeric_equal',
    '!=' => 'numeric_not_equal',
    '<'  => 'numeric_less_than',
    '<=' => 'numeric_less_or_equal',
    '>'  => 'numeric_greater_than',
    '>=' => 'numeric_greater_or_equal',
    'eq' => 'string_equal',
    'ne' => 'string_not_equal',
    'lt' => 'string_less_than',
    'le' => 'string_less_or_equal',
    'gt' => 'string_greater_than',
    'ge' => 'string_greater_or_equal',
);

# Overload everything defined in %special_binary_operatos:
eval "use overload '$_' => '$special_binary_operators{$_}';"
    for keys(%special_binary_operators);

# All other overloads are here:
use overload (
# Unary operators:
    '!' => 'not',
# Non-standard operators:
    '|'  => 'outer',      # This also implements the bitwise binary 'or'!
    '/'  => 'reduce',     # This also implements the binary division operator!
    'x'  => 'scan',
    '""' => '_stringify',
);

###############################################################################
# Overloading unary operators:
###############################################################################

=head1 Overloaded unary operators

Overloaded unary operators are automatically applied to all elements of
a (nested) APX data structure. The following operators are currently
available: !

=cut

sub not # Not, mapped to '!'.
{
    my $data = [@{$_[0]}];
    Array::DeepUtils::unary($data, sub { return 0+ !$_[0] });
    return bless $data;
}

###############################################################################
# Overloading binary operators:
###############################################################################

=head1 Overloaded binary operators

In general all overloaded binary operators are automatically applied in an
element wise fashion to all (corresponding) elements of APX data structures.

The following operators are currently available and do what one would
expect: 

=head2 +, -, *, /, %, **, |, &, ^, ==, !=, <, >, <=, >=, eq, ne, le, lt, ge, gt

These operators implement addition, subtraction, multiplication, division,
modulus, power, bitwise or / and /xor, numerical equal/not equal, numerical
less than, numerical greater than, numerical less or equal, numerical greater
or equal, string equal, string not equal, string less than, string less or 
equal, string greater than, string greater or equal



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