Permalink
Browse files

Change reducewith triangle case to lazily return values.

  • Loading branch information...
1 parent d906f51 commit 020a6a30632a2aef35001a14e75b9e2e31629f8e @colomon colomon committed Apr 3, 2010
Showing with 48 additions and 24 deletions.
  1. +48 −24 src/core/metaops.pm
View
@@ -103,34 +103,58 @@ our multi sub reducewith(&op, Iterable $an-iterable,
my $ai = $an-iterable.iterator;
$ai = $ai.Seq.reverse.iterator if $right-assoc;
- my $result = $ai.get;
- if $result ~~ EMPTY {
- return &op();
- }
-
- if $chaining {
- my $bool = Bool::True;
- my @r = $bool;
- loop {
- my $next = $ai.get;
- last if $next ~~ EMPTY;
- $bool = $bool && ($right-assoc ?? &op($next, $result) !! &op($result, $next));
- @r.push($bool) if $triangle;
- $result = $next;
+ if $triangle {
+ gather {
+ my $result = $ai.get;
+ return if $result ~~ EMPTY;
+
+ if $chaining {
+ my $bool = Bool::True;
+ take Bool::True;
+ loop {
+ my $next = $ai.get;
+ last if $next ~~ EMPTY;
+ $bool = $bool && ($right-assoc ?? &op($next, $result) !! &op($result, $next));
+ my $temp = $bool;
+ take $temp;
+ $result = $next;
+ }
+ } else {
+ my $temp = $result;
+ take $temp;
+ loop {
+ my $next = $ai.get;
+ last if $next ~~ EMPTY;
+ $result = $right-assoc ?? &op($next, $result) !! &op($result, $next);
+ my $temp = $result;
+ take $temp;
+ }
+ }
}
- return @r if $triangle;
- return $bool;
} else {
- my @r = $result;
- loop {
- my $next = $ai.get;
- last if $next ~~ EMPTY;
- $result = $right-assoc ?? &op($next, $result) !! &op($result, $next);
- @r.push($result) if $triangle;
+ my $result = $ai.get;
+ if $result ~~ EMPTY {
+ return &op();
+ }
+
+ if $chaining {
+ my $bool = Bool::True;
+ loop {
+ my $next = $ai.get;
+ last if $next ~~ EMPTY;
+ $bool = $bool && ($right-assoc ?? &op($next, $result) !! &op($result, $next));
+ $result = $next;
+ }
+ return $bool;
+ } else {
+ loop {
+ my $next = $ai.get;
+ last if $next ~~ EMPTY;
+ $result = $right-assoc ?? &op($next, $result) !! &op($result, $next);
+ }
}
- return @r if $triangle;
+ $result;
}
- $result;
}
our multi sub reducewith(&op, $arg,

0 comments on commit 020a6a3

Please sign in to comment.