Permalink
Browse files

Refactor the reducewith code a tad (could still use more) and extend …

…triangle mode to chaining mode.
  • Loading branch information...
1 parent 16273e1 commit e50ff8cb3400da8872b241f479c09ce7a7c89871 @colomon colomon committed Apr 3, 2010
Showing with 7 additions and 11 deletions.
  1. +7 −11 src/core/metaops.pm
View
@@ -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);
@nsp
nsp Apr 25, 2010

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

@colomon
colomon Apr 25, 2010 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.

@jnthn
jnthn Apr 25, 2010 Contributor

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

@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.

@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;

0 comments on commit e50ff8c

Please sign in to comment.