Added further clarification, and fixed the two "empty" cases of foreach => [] and generate => sub {} without an otherwise.
--
Paul Evans
=== modified file 'lib/Future/Utils.pm'
--- lib/Future/Utils.pm 2014-06-08 21:57:09 +0000
+++ lib/Future/Utils.pm 2014-07-17 10:11:39 +0000
@@ -206,8 +206,8 @@
Calls the C<CODE> block once for each value obtained from the array, passing
in the value as the first argument (before the previous trial future). When
there are no more items left in the array, the C<otherwise> code is invoked
-once and passed the last trial future, if there was one, otherwise C<undef> if
-the list was originally empty. The result of the eventual future will be the
+once and passed the last trial future, if there was one, or C<undef> if the
+list was originally empty. The result of the eventual future will be the
result of the future returned from C<otherwise>.
The referenced array may be modified by this operation.
@@ -216,7 +216,9 @@
$final_f = $otherwise->( $last_trial_f )
The C<otherwise> code is optional; if not supplied then the result of the
-eventual future will simply be that of the last trial.
+eventual future will simply be that of the last trial. If there was no trial,
+because the C<foreach> list was already empty, then an immediate successful
+future with an empty result is returned.
=head2 $future = repeat { CODE } foreach => ARRAY, while => CODE, ...
@@ -339,7 +341,7 @@
goto &$otherwise;
}
else {
- return $last_trial_f;
+ return $last_trial_f || Future->done;
}
};
=== modified file 't/34utils-repeat-foreach.t'
--- t/34utils-repeat-foreach.t 2014-03-26 15:09:41 +0000
+++ t/34utils-repeat-foreach.t 2014-07-17 10:11:39 +0000
@@ -56,6 +56,20 @@
is( scalar $future->failure, "Nothing to do\n", '$future returns otherwise failure for empty list' );
}
+# foreach on empty list
+{
+ my $future = repeat { die "Not invoked" } foreach => [];
+
+ ok( $future->is_ready, 'repeat {} on empty foreach without otherwise already ready' );
+ is_deeply( [ $future->get ], [], 'Result of empty future' );
+
+ $future = repeat { die "Not invoked" } foreach => [],
+ otherwise => sub { Future->done( 1, 2, 3 ) };
+
+ ok( $future->is_ready, 'repeat {} on empty foreach with otherwise already ready' );
+ is_deeply( [ $future->get ], [ 1, 2, 3 ], 'Result of otherwise future' );
+}
+
# foreach while
{
my $future = try_repeat {