Skip to content

Commit

Permalink
Add new reducewith sub intended to be the backend for [op] (though th…
Browse files Browse the repository at this point in the history
…at is not wired up yet).
  • Loading branch information
colomon committed Apr 2, 2010
1 parent dd2043b commit 8f35ce2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/metaops.pm
Expand Up @@ -96,6 +96,27 @@ our multi sub hyper(&op, $arg) {
hyper(&op, $arg.list)
}

our multi sub reducewith(&op, Iterable $an-iterable, :$chaining, :$right-assoc) {
my $ai = $an-iterable.iterator;
$ai = $ai.Seq.reverse.iterator if $right-assoc;

my $result = $ai.get;
if $result ~~ EMPTY {
return &op();
}

loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
if ($right-assoc) {
$result = &op($next, $result);
}
else {
$result = &op($result, $next);
}
}
$result;
}

# degenerate case of operators, to be used by reduce() for the 0-ary case
# this fails for operators defined in PIR, so some of them are commented out.
Expand Down

0 comments on commit 8f35ce2

Please sign in to comment.