NAME

List::Enumerator - list construct library

SYNOPSIS

use List::Enumerator qw/E/;

my $fizzbuzz =
    E(1)->countup->zip(
        E("", "", "Fizz")->cycle,
        E("", "", "", "", "Buzz")->cycle
    )->map(sub {
        my ($n, $fizz, $buzz) = @$_;
        $fizz . $buzz || $n;
    });

$fizzbuzz->take(20)->each(sub {
    print $_, "\n";
});

DESCRIPTION

List::Enumerator is list library like Enumerator of Ruby.

List::Enumerator::E is interface wrapper for generating List::Enumerator::Array or List::Enumerator::Sub.

Most methods (except what returns always infinite list) consider caller context. ex:

E(1, 2, 3, 4, 5)->take(3);     #=> new List::Enumerator::Sub
[ E(1, 2, 3, 4, 5)->take(3) ]; #=> [1, 2, 3]
E(list), E([arrayref])

Returns List::Enumerator::Array.

E({ next => sub {}, rewind => sub {} })

Returns List::Enumerator::Sub. ex:

use List::Enumerator qw/E/;

sub fibonacci {
    my ($p, $i);
    E(0, 1)->chain(E({
        next => sub {
            my $ret = $p + $i;
            $p = $i;
            $i = $ret;
            $ret;
        },
        rewind => sub {
            ($p, $i) = (0, 1);
        }
    }))->rewind;
}

[ fibonacci->take(10) ];           #=> [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ];
[ fibonacci->drop(10)->take(10) ]; #=> [ 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181 ];

Concept

  • Lazy evaluation for infinite list (ex. cycle)

  • Method chain

  • Read the context

  • Applicable (implemented as Moose::Role)

DEVELOPMENT

This module is developing on github http://github.com/cho45/list-enumerator/tree/master.

AUTHOR

cho45 <cho45@lowreal.net>

SEE ALSO

List::RubyLike, http://coderepos.org/share/wiki/JSEnumerator

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.