Element-wise operations: Difference between revisions

→‎{{header|Perl}}: re-write as one runnable file, use modern Perl
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(→‎{{header|Perl}}: re-write as one runnable file, use modern Perl)
Line 2,873:
 
This example demonstrates Perl's operator overload ability and bulk list operations using <tt>map</tt>.
<syntaxhighlight lang="perl">use Elementwisev5.36;
 
Filepackage <tt>Elementwise.pm</tt>:;
<syntaxhighlight lang="perl">package Elementwise;
 
use Exporter 'import';
 
use overload
'=+' => sub ($a,$b,$) { $_[0]a->cloneadd($b) },
'+-' => sub ($a,$b,$) { $_[0]a->addsub($_[1]b) },
'-*' => sub ($a,$b,$) { $_[0]a->submul($_[1]b) },
'*/' => sub ($a,$b,$) { $_[0]a->muldiv($_[1]b) },
'/**' => sub ($a,$b,$) { $_[0]a->divexp($_[1]b) },;
'**' => sub { $_[0]->exp($_[1]) },
;
 
sub new ($class, $value) { bless $value, ref $class || $class }
sub new
{
my ($class, $v) = @_;
return bless $v, $class;
}
 
sub add { new Elementwise ref($_[1]) ? [map { $_[0][$_] + $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] + $_[1] } 0 .. $#{$_[0]} ] }
sub clone
sub sub { new Elementwise ref($_[1]) ? [map { $_[0][$_] - $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] - $_[1] } 0 .. $#{$_[0]} ] }
{
sub mul { new Elementwise ref($_[1]) ? [map { $_[0][$_] * $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] * $_[1] } 0 .. $#{$_[0]} ] }
my @ret = @{$_[0]};
sub div { new Elementwise ref($_[1]) ? [map { $_[0][$_] / $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] / $_[1] } 0 .. $#{$_[0]} ] }
return bless \@ret, ref($_[0]);
sub exp { new Elementwise ref($_[1]) ? [map { $_[0][$_] ** $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] ** $_[1] } 0 .. $#{$_[0]} ] }
}
 
package main;
sub add { new Elementwise ref($_[1]) ? [map { $_[0][$_] + $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] + $_[1] } 0 .. $#{$_[0]} ] }
sub sub { new Elementwise ref($_[1]) ? [map { $_[0][$_] - $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] - $_[1] } 0 .. $#{$_[0]} ] }
sub mul { new Elementwise ref($_[1]) ? [map { $_[0][$_] * $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] * $_[1] } 0 .. $#{$_[0]} ] }
sub div { new Elementwise ref($_[1]) ? [map { $_[0][$_] / $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] / $_[1] } 0 .. $#{$_[0]} ] }
sub exp { new Elementwise ref($_[1]) ? [map { $_[0][$_] ** $_[1][$_] } 0 .. $#{$_[0]} ] : [map { $_[0][$_] ** $_[1] } 0 .. $#{$_[0]} ] }
 
$a = Elementwise->new([<1 2 3 4 5 6 7 8 9>]);
1;</syntaxhighlight>
 
say <<"END";
File <tt>test.pl</tt>:
a @$a
<syntaxhighlight lang="perl">use Elementwise;
 
$a = new Elementwise [
1,2,3,
4,5,6,
7,8,9
];
 
print << "_E";
a @$a
a OP a
+ @{$a+$a}
Line 2,924 ⟶ 2,904:
* @{$a*$a}
/ @{$a/$a}
^ ** @{$a**$a}
 
a OP 5
+ @{$a+5}
Line 2,930 ⟶ 2,911:
* @{$a*5}
/ @{$a/5}
^ ** @{$a**5}
_EEND</syntaxhighlight>
 
{{out}}
<pre>a 1 2 3 4 5 6 7 8 9
 
a OP a
+ 2 4 6 8 10 12 14 16 18
Line 2,940 ⟶ 2,922:
* 1 4 9 16 25 36 49 64 81
/ 1 1 1 1 1 1 1 1 1
^ ** 1 4 27 256 3125 46656 823543 16777216 387420489
 
a OP 5
+ 6 7 8 9 10 11 12 13 14
Line 2,946 ⟶ 2,929:
* 5 10 15 20 25 30 35 40 45
/ 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8
^ ** 1 32 243 1024 3125 7776 16807 32768 59049</pre>
 
=={{header|Phix}}==
2,392

edits