Data-Monad-CondVar

 view release on metacpan or  search on metacpan

lib/Data/Monad/CondVar.pm  view on Meta::CPAN

    $self->flat_map(sub {
        my @args = @_;

        $class->unit($retry, undef, undef)->while(
            sub {
                my ($retry, $ok, $fail) = @_;
                ! $ok and $retry > 0;
            }, sub {
                my ($retry, $ok, $fail) = @_;
                $f->(@args)->flat_map(sub {
                    $class->unit($retry - 1, [@_], undef);
                })->catch(sub {
                    $class->unit($retry - 1, undef, [@_])
                          ->sleep($pace);
                });
            }
        )->flat_map(sub {
            my ($retry, $ok, $fail) = @_;
            $fail ? $class->fail(@$fail) : $class->unit(@$ok);
        });
    });
}

1;

__END__

=head1 NAME

Data::Monad::CondVar - The CondVar monad.

=head1 SYNOPSIS

  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;

=head1 DESCRIPTION

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 B<EXPERIMENTAL>. API could be changed without any notice.

=head1 METHODS

=over 4

=item $cv = as_cv($cb->($cv))

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;

=item $cv = cv_unit(@vs)

=item $cv = cv_zero()

=item $cv = cv_fail($v)

=item $f = cv_flat_map_multi(\&f, $cv1, $cv2, ...)

=item $f = cv_map_multi(\&f, $cv1, $cv2, ...)

=item $cv = cv_sequence($cv1, $cv2, ...)

These are shorthand of methods which has the same name.

=item $cv = call_cc($f->($cont))

Calls C<$f> with current continuation, C<$cont>.
C<$f> must return a CondVar object.
If you call C<$cont> in C<$f>, results are sent to C<$cv> directly and
codes left in C<$f> will be skipped.

You can use C<call_cc> to escape a deeply nested call structure.

  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

=item unit

=item flat_map

Overrides methods of L<Data::Monad::Base::Monad>.

=item zero

Overrides methods of L<Data::Monad::Base::MonadZero>.
It uses C<fail> method internally.

=item $cv = AnyEvent::CondVar->fail($msg)

Creates the new CondVar object which represents a failed operation.
You can use C<catch> to handle failed operations.

=item $cv = AnyEvent::CondVar->any($cv1, $cv2, ...)



( run in 1.369 second using v1.01-cache-2.11-cpan-39bf76dae61 )