Perl6-GatherTake

 view release on metacpan or  search on metacpan

lib/Perl6/GatherTake.pm  view on Meta::CPAN

exhausted). This means that iterating over C<for (@$list)> will result in an 
undefined element at the end if the block returns only a finite number of
elements.

=item * 

This module consumes much more resources than desirable: for each
gather-take-block it (currently) maintains a tied array (which is implemented
as a blessed hash) which holds all the computed values so far, a C<Coro> and
a C<Coro::Channel> object.

=item * 

C<take> doesn't default to C<$_>.

=item * 

More advanced array operations (like slices, C<splice> etc.) aren't tested yet.

=back

=head1 LICENSE

This package is free software, you can use it under the same terms as Perl
itself.

All example and test code in this distribution is "Public Domain" (*), i.e.
you may use it in any way you want.

(*) German copyright laws always grant the original author some rights, so
I can't really place things in the "Public Domain". But don't let that bother
you.

=head1 AUTHOR

Moritz Lenz, L<http://perlgeek.de/>, L<http://perl-6.de/>.
E-Mail E<lt>moritz@faui2k3.orgE<gt>.

=head1 DEVELOPMENT

You can obtain the latest development version via subversion:

    svn co https://faui2k3.org/svn/moritz/cpan/Perl6-GatherTake

Patches and comments are welcome.

=cut

use strict;
use warnings;

use Data::Dumper;
use base 'Exporter';
use Perl6::GatherTake::LazyList;
use Coro;
use Coro::Channel;
use Carp qw(confess);
use Scalar::Util qw(refaddr);
our @EXPORT = qw(gather take);

our %_coro_to_queue;

sub gather(&@) {
    my $code = shift;
    # cheat prototype by prepending '&' to method call:
    my $coro = &async($code, @_);
    my @result = ();
    my $queue = Coro::Channel->new(1);
#    print "Initialized coro $coro\n";
    $_coro_to_queue{refaddr($coro)} = $queue;
    tie @result, 'Perl6::GatherTake::LazyList', $coro, $queue;
    return \@result;
}

sub take {
    my $c = Coro::current;
#    print "Take: $c\n";
    for (@_){
        $_coro_to_queue{refaddr($c)}->put($_);
    }
}

1;



( run in 1.170 second using v1.01-cache-2.11-cpan-2398b32b56e )