Skip to content

Commit

Permalink
Added the Array.pam_operator() which is a reversed map.
Browse files Browse the repository at this point in the history
Example:
	# Calls the block with each element
	[1,2,3] «call« { |i|
		say i;		# prints: 1, 2, 3
	};

	# Divides 10 by 1,2,3
	[1,2,3] «/« 10;		# equivalent with: [10/1, 10/2, 10/3]
  • Loading branch information
trizen committed Aug 21, 2015
1 parent f6b0acc commit 04f8897
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
19 changes: 13 additions & 6 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package Sidef::Parser {
},
special_ops => {
mop => [1, 'map_operator'],
pop => [1, 'pam_operator'],
uop => [1, 'unroll_operator'],
rop => [0, 'reduce_operator'],
},
Expand Down Expand Up @@ -291,12 +292,18 @@ package Sidef::Parser {
)
)
»(?<uop>[_\pL][_\pL\pN]*|(?&ops))« # unroll method + op (e.g.: »add« or »+«)
| >>(?<uop>[_\pL][_\pL\pN]*|(?&ops))<< # unroll method + op (e.g.: >>add<< or >>+<<)
| »(?<mop>[_\pL][_\pL\pN]*|(?&ops))» # mapping operator (e.g.: »add» or »+»)
| >>(?<mop>[_\pL][_\pL\pN]*|(?&ops))>> # mapping operator (e.g.: >>add>> or >>+>>)
| <<(?<rop>[_\pL][_\pL\pN]*|(?&ops))>> # reduce method (e.g.: <<add>> or <<+>>)
| «(?<rop>[_\pL][_\pL\pN]*|(?&ops))» # reduce method + op (e.g.: «add» or «+»)
»(?<uop>[_\pL][_\pL\pN]*|(?&ops))« # unroll operator (e.g.: »add« or »+«)
| >>(?<uop>[_\pL][_\pL\pN]*|(?&ops))<< # unroll operator (e.g.: >>add<< or >>+<<)
| »(?<mop>[_\pL][_\pL\pN]*|(?&ops))» # mapping operator (e.g.: »add» or »+»)
| >>(?<mop>[_\pL][_\pL\pN]*|(?&ops))>> # mapping operator (e.g.: >>add>> or >>+>>)
| «(?<pop>[_\pL][_\pL\pN]*|(?&ops))« # reverse mapping operator (e.g.: «add« or «+«)
| <<(?<pop>[_\pL][_\pL\pN]*|(?&ops))<< # reverse mapping operator (e.g.: <<add<< or <<+<<)
| <<(?<rop>[_\pL][_\pL\pN]*|(?&ops))>> # reduce operator (e.g.: <<add>> or <<+>>)
| «(?<rop>[_\pL][_\pL\pN]*|(?&ops))» # reduce operator (e.g.: «add» or «+»)
| \h*\^(?<op>[_\pL][_\pL\pN]*[!:?]?)\^\h* # method-like operator (e.g.: ^add^)
| (?<op>(?&ops)) # primitive operator (e.g.: +, -, *, /)
}x;
Expand Down
15 changes: 15 additions & 0 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ package Sidef::Types::Array::Array {
$self->new(@array);
}

sub pam_operator {
my ($self, $operator, $arg) = @_;

if (ref $operator) {
$operator = $operator->get_value;
}

my @array;
foreach my $i (0 .. $#{$self}) {
push @array, $arg->$operator($self->[$i]->get_value);
}

$self->new(@array);
}

sub reduce_operator {
my ($self, $operator) = @_;

Expand Down
4 changes: 2 additions & 2 deletions scripts/Expensive/chebyshev_coefficients.sf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func chebft (callback, a, b, n) {
var bpa = (0.5 * b+a);

var pi_n = ((0..(n-1) »+» 0.5) »*» (Math::PI / n));
var f = (pi_n »cos»() »*» bma »+» bpa -> map {callback(_)});
var sums = (0..(n-1) -> map {|i| f »*« ((pi_n »*» i) »cos»()) «+» });
var f = (pi_n »cos»() »*» bma »+» bpa «call« callback);
var sums = (0..(n-1) «run« {|i| f »*« ((pi_n »*» i) »cos»()) «+» });

sums »*» (2/n);
}
Expand Down

0 comments on commit 04f8897

Please sign in to comment.