Skip to content

Commit

Permalink
Refactor the reducewith code a tad (could still use more) and extend …
Browse files Browse the repository at this point in the history
…triangle mode to chaining mode.
  • Loading branch information
colomon committed Apr 3, 2010
1 parent 16273e1 commit e50ff8c
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/core/metaops.pm
Expand Up @@ -109,27 +109,23 @@ our multi sub reducewith(&op, Iterable $an-iterable,
}

if $chaining {
my $bool = Bool::True;
my @r = $bool;
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
my $i = $right-assoc ?? op($next, $result) !! op($result, $next);
unless $i {
return Bool::False;
}
$bool = $bool && ($right-assoc ?? &op($next, $result) !! &op($result, $next));
@r.push($bool) if $triangle;
$result = $next;
}
return Bool::True;
return @r if $triangle;
return $bool;
} else {
my @r = $result;
loop {
my $next = $ai.get;
last if $next ~~ EMPTY;
if ($right-assoc) {
$result = &op($next, $result);
}
else {
$result = &op($result, $next);
}
$result = $right-assoc ?? &op($next, $result) !! &op($result, $next);

This comment has been minimized.

Copy link
@nsp

nsp Apr 25, 2010

So '?? !!' is the ternary operator in Perl 6? Does '?:' still work?

This comment has been minimized.

Copy link
@colomon

colomon Apr 25, 2010

Author Contributor

?? !! is indeed the ternary operator in Perl 6. ? : is gone. I believe it was decided that these two common one-character symbols were too valuable to "waste" on ternary.

This comment has been minimized.

Copy link
@jnthn

jnthn Apr 25, 2010

Member

That and single characters are a bit too "light" to indicate control flow, and also because !! is more obviously to do with booleans.

This comment has been minimized.

Copy link
@nsp

nsp Apr 26, 2010

Yea. I like how it also brings more attention to what the code is doing, because the ternary operator is definitely something you want noticed.

This comment has been minimized.

Copy link
@pmichaud

pmichaud Apr 26, 2010

Contributor

There are several reasons why ternary is now ?? !!:

  • Throughout Perl 6, the ? and ! characters are used to ask "is true" and "is false" (e.g., ?$var and !$var)
  • Control-flow operators are typically doubled -- i.e., ?? !! matches && and ||
  • These memes extend to regexes as well -- <?subrule> and <!subrule>
@r.push($result) if $triangle;
}
return @r if $triangle;
Expand Down

0 comments on commit e50ff8c

Please sign in to comment.