Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/Future/Utils.pm view on Meta::CPAN
my $eventual_f = repeat {
my $item = shift;
...
return $trial_f;
} foreach => \@items;
my $eventual_f = try_repeat {
my $trial_f = ...
return $trial_f;
} while => sub { ... };
my $eventual_f = try_repeat_until_success {
...
return $trial_f;
};
my $eventual_f = try_repeat_until_success {
my $item = shift;
...
return $trial_f;
} foreach => \@items;
Z<>
use Future::Utils qw( fmap_concat fmap_scalar fmap_void );
my $result_f = fmap_concat {
my $item = shift;
...
return $item_f;
} foreach => \@items, concurrent => 4;
my $result_f = fmap_scalar {
my $item = shift;
...
return $item_f;
} foreach => \@items, concurrent => 8;
my $done_f = fmap_void {
my $item = shift;
...
return $item_f;
} foreach => \@items, concurrent => 10;
Unless otherwise noted, the following functions require at least version
I<0.08>.
=cut
=head1 INVOKING A BLOCK OF CODE
=head2 call
$f = call { CODE }
I<Since version 0.22.>
The C<call> function invokes a block of code that returns a future, and simply
returns the future it returned. The code is wrapped in an C<eval {}> block, so
that if it throws an exception this is turned into an immediate failed
C<Future>. If the code does not return a C<Future>, then an immediate failed
C<Future> instead.
(This is equivalent to using C<< Future->call >>, but is duplicated here for
completeness).
=cut
sub call(&)
{
my ( $code ) = @_;
return Future->call( $code );
}
=head2 call_with_escape
$f = call_with_escape { CODE }
I<Since version 0.22.>
The C<call_with_escape> function invokes a block of code that returns a
future, and passes in a separate future (called here an "escape future").
Normally this is equivalent to the simple C<call> function. However, if the
code captures this future and completes it by calling C<done> or C<fail> on
it, the future returned by C<call_with_escape> immediately completes with this
result, and the future returned by the code itself is cancelled.
This can be used to implement short-circuit return from an iterating loop or
complex sequence of code, or immediate fail that bypasses failure handling
logic in the code itself, or several other code patterns.
$f = $code->( $escape_f )
(This can be considered similar to C<call-with-escape-continuation> as found
in some Scheme implementations).
=cut
sub call_with_escape(&)
{
my ( $code ) = @_;
my $escape_f = Future->new;
return Future->wait_any(
Future->call( $code, $escape_f ),
$escape_f,
);
}
=head1 REPEATING A BLOCK OF CODE
The C<repeat> function provides a way to repeatedly call a block of code that
returns a L<Future> (called here a "trial future") until some ending condition
is satisfied. The C<repeat> function itself returns a C<Future> to represent
running the repeating loop until that end condition (called here the "eventual
future"). The first time the code block is called, it is passed no arguments,
and each subsequent invocation is passed the previous trial future.
The result of the eventual future is the result of the last trial future.
If the eventual future is cancelled, the latest trial future will be
cancelled.
If some specific subclass or instance of C<Future> is required as the return
value, it can be passed as the C<return> argument. Otherwise the return value
will be constructed by cloning the first non-immediate trial C<Future>.
=head2 repeat+while
$future = repeat { CODE } while => CODE
Repeatedly calls the C<CODE> block while the C<while> condition returns a true
value. Each time the trial future completes, the C<while> condition is passed
the trial future.
$trial_f = $code->( $previous_trial_f )
$again = $while->( $trial_f )
If the C<$code> block dies entirely and throws an exception, this will be
caught and considered as an immediately-failed C<Future> with the exception as
the future's failure. The exception will not be propagated to the caller.
=head2 repeat+until
$future = repeat { CODE } until => CODE
Repeatedly calls the C<CODE> block until the C<until> condition returns a true
value. Each time the trial future completes, the C<until> condition is passed
the trial future.
$trial_f = $code->( $previous_trial_f )
$accept = $until->( $trial_f )
=head2 repeat+foreach
$future = repeat { CODE } foreach => ARRAY, otherwise => CODE
I<Since version 0.13.>
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, 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.
$trial_f = $code->( $item, $previous_trial_f )
$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. 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 repeat+foreach+while
$future = repeat { CODE } foreach => ARRAY, while => CODE, ...
I<Since version 0.13.>
=head2 repeat+foreach+until
$future = repeat { CODE } foreach => ARRAY, until => CODE, ...
I<Since version 0.13.>
Combines the effects of C<foreach> with C<while> or C<until>. Calls the
C<CODE> block once for each value obtained from the array, until the array is
exhausted or the given ending condition is satisfied.
If a C<while> or C<until> condition is combined with C<otherwise>, the
C<otherwise> code will only be run if the array was entirely exhausted. If the
operation is terminated early due to the C<while> or C<until> condition being
satisfied, the eventual result will simply be that of the last trial that was
executed.
=head2 repeat+generate
( run in 2.039 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )