Skip to content

Commit

Permalink
Change reducewith triangle case to lazily return values.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Apr 3, 2010
1 parent d906f51 commit 020a6a3
Showing 1 changed file with 48 additions and 24 deletions.
72 changes: 48 additions & 24 deletions src/core/metaops.pm
Expand Up @@ -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,
Expand Down

0 comments on commit 020a6a3

Please sign in to comment.