From: Vincent Pit Date: Fri, 8 Aug 2008 19:43:41 +0000 (+0200) Subject: Fix handling of partial returns in lists X-Git-Tag: v0.03~8 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FSub-Nary.git;a=commitdiff_plain;h=f73405816c83f0e2816e81421a8b6cf72d5e3151 Fix handling of partial returns in lists --- diff --git a/lib/Sub/Nary.pm b/lib/Sub/Nary.pm index 50ba378..b6ad798 100644 --- a/lib/Sub/Nary.pm +++ b/lib/Sub/Nary.pm @@ -311,18 +311,20 @@ sub inspect_kids { $op = $op->first; redo; } - diag "> $n ($c)" if $DEBUG; + diag "> $n" if $DEBUG; my ($rc, $lc) = $self->inspect($op); + $c = 1 - count $r; + diag Dumper [ $c, $r, \@l, $rc, $lc ] if $DEBUG; $r = add $r, scale $c, $rc if defined $rc; - if ($rc and not defined $lc) { + if (not defined $lc) { @l = (); last; } push @l, scale $c, $lc; - $c *= count $lc if defined $lc; } - my $l = combine @l; +# diag Dumper \@l if $DEBUG; + my $l = scale +(1 - count $r), normalize combine @l; return $r, $l; } diff --git a/t/20-return.t b/t/20-return.t index c48eeef..ccfd9e9 100644 --- a/t/20-return.t +++ b/t/20-return.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 76; +use Test::More tests => 79; use Sub::Nary; @@ -25,6 +25,10 @@ my @tests = ( [ sub { do { 1; return 2, 3 } }, 2 ], [ sub { do { 1; return 2, 3; 4 } }, 2 ], [ sub { do { 1; return 2, return 3 } }, 1 ], + [ sub { do { return if $x; 2, 3 }, 4 }, { 0 => 0.5, 3 => 0.5 } ], + [ sub { do { do { return if $x }, 3 }, 4 }, { 0 => 0.5, 3 => 0.5 } ], + [ sub { do { return if $x; 2, 3 }, do { return 1 if $y; 4, 5, 6 } }, + { 0 => 0.5, 1 => 0.25, 5 => 0.25 } ], [ sub { return $x }, 1 ], [ sub { return $x, $y }, 2 ],