Permalink
Browse files

reducewith with triangle; implement combination of chaining and right…

…-assoc
  • Loading branch information...
1 parent 0ccaf6c commit bcdee8ef256fb2fe0af01466f6b1faff115dce84 @moritz moritz committed Apr 2, 2010
Showing with 9 additions and 2 deletions.
  1. +9 −2 src/core/metaops.pm
View
@@ -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;
@@ -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;
@@ -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;
}

0 comments on commit bcdee8e

Please sign in to comment.