Math-Symbolic
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Math/Symbolic/Operator.pm view on Meta::CPAN
asinh => hyperbolic area sine (1)
acosh => hyperbolic area cosine (1)
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
if ( @_ and not( ref( $_[0] ) eq 'HASH' ) ) {
my $symbol = shift;
my $type = $Op_Symbols{$symbol};
croak "Invalid operator type specified ($symbol)."
unless defined $type;
my $operands = [ @_[ 0 .. $Op_Types[$type]{arity} - 1 ] ];
croak "Undefined operands not supported by "
. "Math::Symbolic::Operator objects."
if grep +( not defined($_) ), @$operands;
@$operands =
map {
ref($_) =~ /^Math::Symbolic/
? $_
: Math::Symbolic::parse_from_string($_)
} @$operands;
return bless {
type => $type,
operands => $operands,
} => $class;
}
my %args;
%args = %{ $_[0] } if @_;
# and ref( $_[0] ) eq 'HASH';
# above condition isn't necessary since that'd otherwise have been
# the above branch.
my $operands = [];
if ( ref $proto ) {
foreach ( @{ $proto->{operands} } ) {
push @$operands, $_->new();
}
}
my $self = {
type => undef,
( ref($proto) ? %$proto : () ),
operands => $operands,
%args,
};
@{ $self->{operands} } =
map {
ref($_) =~ /^Math::Symbolic/
? $_
: Math::Symbolic::parse_from_string($_)
} @{ $self->{operands} };
bless $self => $class;
}
=head2 Method arity
Returns the operator's arity as an integer.
=cut
sub arity {
my $self = shift;
return $Op_Types[ $self->{type} ]{arity};
}
=head2 Method type
Optional integer argument that sets the operator's type.
Returns the operator's type as an integer.
=cut
sub type {
my $self = shift;
$self->{type} = shift if @_;
return $self->{type};
}
=head2 Method to_string
Returns a string representation of the operator and its operands.
Optional argument: 'prefix' or 'infix'. Defaults to 'infix'.
=cut
sub to_string {
my $self = shift;
my $string_type = shift;
$string_type = 'infix'
unless defined $string_type
and $string_type eq 'prefix';
no warnings 'recursion';
my $string = '';
if ( $string_type eq 'prefix' ) {
$string .= $self->_to_string_prefix();
}
else {
$string .= $self->_to_string_infix();
}
return $string;
}
sub _to_string_infix {
my $self = shift;
my $op = $Op_Types[ $self->{type} ];
my $op_str = $op->{infix_string};
my $string;
if ( $op->{arity} == 2 ) {
my $op1 = $self->{operands}[0]->term_type() == T_OPERATOR;
my $op2 = $self->{operands}[1]->term_type() == T_OPERATOR;
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.668 second using v1.00-cache-2.02-grep-82fe00e-cpan-3b7f77b76a6c )