Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix right-assoc meta reduce
  • Loading branch information
moritz committed Aug 20, 2015
1 parent 8afab2e commit 328aa98
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/core/metaops.pm
Expand Up @@ -120,29 +120,25 @@ multi sub METAOP_REDUCE_LEFT(\op, \triangle) {
}
}

sub REDUCE_LEFT_ITERATOR(\op, Iterator \iter) {
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;

my \second = iter.pull-one;
return op.(first) if second =:= IterationEnd;

my $result := op.(first, second);
until (my \value = iter.pull-one) =:= IterationEnd {
$result := op.($result, value);
}
$result;
}

multi sub METAOP_REDUCE_LEFT(\op) {
#?if jvm
my $ :=
#?endif
sub (\iterablish) {
my \source = nqp::istype(iterablish, Iterable)
my \iter = nqp::istype(iterablish, Iterable)
?? iterablish.iterator
!! iterablish.list.iterator;
REDUCE_LEFT_ITERATOR(op, source);
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;

my \second = iter.pull-one;
return op.(first) if second =:= IterationEnd;

my $result := op.(first, second);
until (my \value = iter.pull-one) =:= IterationEnd {
$result := op.($result, value);
}
$result;
}
}

Expand Down Expand Up @@ -170,7 +166,18 @@ multi sub METAOP_REDUCE_RIGHT(\op) {
my $ :=
#?endif
sub (*@values) {
REDUCE_LEFT_ITERATOR(op, @values.reverse.iterator);
my \iter = @values.reverse.iterator;
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;

my \second = iter.pull-one;
return op.(first) if second =:= IterationEnd;

my $result := op.(second, first);
until (my \value = iter.pull-one) =:= IterationEnd {
$result := op.(value, $result);
}
$result;
}
}

Expand Down

0 comments on commit 328aa98

Please sign in to comment.