Data::Monad::CondVar - The CondVar monad.
use Data::Monad::CondVar; # The sleep sort my @list = (3, 5, 2, 4, 9, 1, 8); my @result; AnyEvent::CondVar->all( map { cv_unit($_)->sleep($_ / 1000) ->map(sub { push @result, @_ }); } @list )->recv;
Data::Monad::CondVar adds monadic operations to AnyEvent::CondVar.
Since this module extends AnyEvent::CondVar directly, you can call monadic methods anywhere there are CondVars.
This module is marked EXPERIMENTAL. API could be changed without any notice.
A helper for rewriting functions using callbacks to ones returning CVs.
my $cv = as_cv { http_get "http://google.ne.jp", $_[0] }; my ($data, $headers) = $cv->recv;
These are shorthand of methods which has the same name.
Calls $f with current continuation, $cont. $f must return a CondVar object. If you call $cont in $f, results are sent to $cv directly and codes left in $f will be skipped.
$f
$cont
$cv
You can use call_cc to escape a deeply nested call structure.
call_cc
sub myname { my $uc = shift; return call_cc { my $cont = shift; cv_unit("hiratara")->flat_map(sub { return $cont->(@_) unless $uc; # escape from an inner block cv_unit @_; })->map(sub { uc $_[0] }); }; } print myname(0)->recv, "\n"; # hiratara print myname(1)->recv, "\n"; # HIRATARA
Overrides methods of Data::Monad::Base::Monad.
Overrides methods of Data::Monad::Base::MonadZero. It uses fail method internally.
fail
Creates the new CondVar object which represents a failed operation. You can use catch to handle failed operations.
catch
Takes the earliest value from $cv1, $cv2, ...
$cv1
$cv2
Takes all values from $cv1, $cv2, ...
This method works completely like <Data::Monad::Base::Monad-sequence>>, but you may want use this method for better cancellation.
<Data::Monad::Base::Monad-
Cancels computations for this CV. This method just calls the call back which is set in the canceler field.
canceler
<$cv-recv>> may never return from blocking after you call cancel.
<$cv-
cancel
The accessor of the method to cancel. You should set this field appropriately when you create the new CondVar object.
my $cv = AE::cv; my $t = AE::timer $sec, 0, sub { $cv->send(@any_results); $cv->canceler(undef); # Destroy cyclic refs }; $cv->canceler(sub { undef $t });
If $cv1 croaks, or returns the CondVar object which contains values of $cv2. Otherwise it returns $cv1's values.
or
or would be mplus on Haskell.
mplus
If $cv1 croaks, $cb is called and it returns the new CondVar object containing its result. Otherwise catch does nothing. $cb must return a CondVar object.
$cb
You can use this method to handle errors.
cv_unit(1, 0) ->map(sub { $_[0] / $_[1] }) ->catch(sub { my $exception = shift; $exception =~ /Illegal division/ ? cv_unit(0) # recover from errors : cv_fail($exception); # rethrow });
Sleeps $sec seconds, and just sends values of $cv1 to $cv.
$sec
If $cv1 doesn't compute any values within $sec seconds, $cv will be received undef and $cv1 will be canceled.
undef
Otherwise $cv will be received $cv1's results.
Continue to call flat_map($f) until $f returns a normal value which doesn't croak.
flat_map($f)
$max is maximum number of retries, $pace is how long it sleeps between each retry. The default value of $pace is 0.
$max
$pace
0
hiratara <hiratara {at} cpan.org>
Data::Monad::Base::Monad
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Data::Monad::CondVar, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Monad::CondVar
CPAN shell
perl -MCPAN -e shell install Data::Monad::CondVar
For more information on module installation, please visit the detailed CPAN module installation guide.