Skip to content

Commit

Permalink
reducewith with triangle; implement combination of chaining and right…
Browse files Browse the repository at this point in the history
…-assoc
  • Loading branch information
moritz committed Apr 2, 2010
1 parent 0ccaf6c commit bcdee8e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/core/metaops.pm
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ our multi sub hyper(&op, $arg) {
hyper(&op, $arg.list)
}

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

Expand All @@ -109,13 +112,15 @@ our multi sub reducewith(&op, Iterable $an-iterable, :$chaining, :$right-assoc)
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
if !op($result, $next) {
my $i = $right-assoc ?? op($next, $result) !! op($result, $next);
unless $i {
return Bool::False;
}
$result = $next;
}
return Bool::True;
} else {
my @r = $result;
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
Expand All @@ -125,7 +130,9 @@ our multi sub reducewith(&op, Iterable $an-iterable, :$chaining, :$right-assoc)
else {
$result = &op($result, $next);
}
@r.push($result) if $triangle;
}
return @r if $triangle;
}
$result;
}
Expand Down

0 comments on commit bcdee8e

Please sign in to comment.