Acme-Parataxis

 view release on metacpan or  search on metacpan

lib/Acme/Parataxis/Generator.pod  view on Meta::CPAN


B<Safe lifecycle teardown>: Abandoning an unexhausted generator cleanly drains the fiber: the yield site raises an
internal drain sentinel, unwinding lexical scopes and executing object destructors without leaking fibers.

=item *

B<Context independence>: Generators do not enter the scheduler run queue. They can be instantiated and pulled from
top-level mainline scripts, within C<async> blocks, inside spawned fibers, or inside nurseries.

=back

=head1 CONSTRUCTOR

=head2 C<new( sub ($y) { ... } )>

    my $gen = Acme::Parataxis::Generator->new( sub ($y) {
        $y->($_) for 1 .. 3;
    } );

Constructs and returns a new generator.

The constructor takes exactly one argument: a code reference representing the producer body. That code reference
receives a single parameter: the yield closure (conventionally named C<$y>).

Calling C<< $y->($value) >> suspends the body and delivers C<$value> to the caller's C<next()>. C<$y> requires exactly
one argument; calling C<< $y->() >> or passing multiple arguments croaks.

=head1 METHODS

=head2 C<next( )>

    my $val = $gen->next;

Resumes the generator body and returns the next yielded value.

=over

=item * Once the body finishes (either by falling off the end or returning), C<next()> returns C<undef> and continues
returning C<undef> on subsequent calls.

=item * If the body explicitly yields C<undef> (e.g. C<< $y->(undef) >>), C<next()> returns C<undef>. Use
L</"is_done( )"> to differentiate a yielded C<undef> from sequence exhaustion.

=item * If the body throws an exception, that exception is rethrown directly on the caller's stack at the C<next()>
call site, and the generator is marked permanently finished. Exception objects cross the resumption boundary intact.

=back

=head2 C<is_done( )>

    while ( !$gen->is_done ) {
        my $v = $gen->next;
        process($v) if defined $v;
    }

Returns true if the generator has finished execution (whether it returned normally or threw an exception), and false if
it is still suspended awaiting the next pull.

=head2 C<fiber( )>

Returns the underlying private C<Acme::Parataxis> fiber object. Intended for debugging and ID inspection.

=head1 Recipes

=head2 Safely consuming sequences that contain C<undef>

The idiom C<< while ( defined(my $v = $gen->next) ) >> halts prematurely if the sequence legitimately yields C<undef>.
Test C<is_done()> instead:

    sub drain ($gen) {
        my @items;
        while (1) {
            my $val = $gen->next;
            last if $gen->is_done;
            push @items, $val;
        }
        return @items;
    }

    my $gen = Acme::Parataxis::Generator->new( sub ($y) {
        $y->('alpha');
        $y->(undef);
        $y->('omega');
    } );

    my @values = drain($gen);    # [ 'alpha', undef, 'omega' ]

=head2 Composing lazy generator pipelines

    my $cubes = Acme::Parataxis::Generator->new( sub ($y) {
        my $ints = Acme::Parataxis::Generator->new( sub ($z) {
            my $i = 1;
            while (1) { $z->($i++) }
        } );

        while ( defined( my $n = $ints->next ) ) {
            $y->( $n ** 3 );
        }
    } );

    say $cubes->next for 1 .. 4;    # 1, 8, 27, 64

=head2 Exception propagation preserving error objects

    use Acme::Parataxis::Error;

    my $gen = Acme::Parataxis::Generator->new( sub ($y) {
        $y->(1);
        Acme::Parataxis::Error::Timeout->throw( seconds => 5 );
    } );

    say $gen->next;    # 1
    eval { $gen->next };
    if ( my $err = $@ ) {
        say "Caught typed error: " . $err->kind;    # 'timeout'
    }

=head1 Behavior Notes

=over



( run in 1.205 second using v1.01-cache-2.11-cpan-c32d3c3b8f1 )