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 ];
next

Returns next element of receiver.

rewind

Rewinds receiver.

select(sub {}), find_all(sub {})

Selects all elements which is evaluated true with block. find_all is just alias to select.

E(1..10)->select(sub {
    $_ % 2 == 0
})->to_a; #=> [2, 4, 6, 8, 10];
reject(sub {})

Selects all elements which is evaluated false with block. This is antonym of select.

reduce(sub {}), inject(sub {})

Reduces receiver to one value using block.

E(1..3)->reduce(sub { $a + $b }); #=> 6
slice($start, $end)

Slices receiver with $start and $end.

E(1..10)->slice(0);  #=> 1
E(1..10)->slice(-1); #=> 10

E(1..20)->slice(9, 11)->to_a; #=> [10, 11, 12]
find($target)

Finds $target. If the value is found returns it. If not so returns undef.

find_index($target), index_of($target)

Finds $target and returns its index.

first

Returns first element of receiver.

last

Returns last element of receiver.

max

Returns max value of receiver.

max_by(sub {})

Returns max value of receiver with block.

min

Returns min value of receiver.

min_by(sub {})

Returns min value of receiver with block.

minmax_by(sub {})

Returns min value and max value of receiver with block.

sort_by(sub {})

Returns sorted list with returned value from block. (Schwartzian transformed sort)

sort(sub {})

Returns sorted list with block.

sum

Sums receiver up and returns the value.

uniq

Returns new unique list.

grep(sub {})

Grep receiver and returns new list.

[ E(1..10)->grep(sub { $_ % 2 == 0 }) ]; #=> [2, 4, 6, 8, 10];
compact

Returns new list excludes undef.

reverse

Returns new reversed list of receiver.

flatten($level)

Expands nested array.

[ E([1, 2, [3, 4], 5])->flatten ];      #=> [1, 2, 3, 4, 5];
[ E([1, [2, [3, 4]], 5])->flatten ];    #=> [1, 2, 3, 4, 5];
[ E([1, [2, [3, 4]], 5])->flatten(1) ]; #=> [1, 2, [3, 4], 5];
length, size

Returns length of receiver. You should not call this method for infinite list.

is_empty

This is synonym of !$self->length;

chain(list...)

Chains with other lists.

[ E(1, 2, 3)->chain([4, 5, 6]) ]; #=> [1, 2, 3, 4, 5, 6];
take(sub {}), take(number), take_while(sub {})

Returns prefix of receiver of length number or elements satisfy block.

drop(sub {}), drop(number), drop_while(sub {})

Returns remaining of receiver.

every(sub {}), all(sub {})

Returns 1 if all elements in receiver satisfies the block.

some(sub {}), any(sub {})

Returns 1 if at least one element in receiver satisfies the block.

none(sub {})

Returns 1 if all elements in receiver not satisfies the block.

E(0, 0, 0, 0)->none; #=> 1
E(0, 0, 0, 1)->none; #=> 0
E(0, 0, 1, 1)->none; #=> 0
one(sub {})

Returns 1 if just one elements in receiver satisfies the block.

E(0, 0, 0, 0)->one; #=> 0
E(0, 0, 0, 1)->one; #=> 1
E(0, 0, 1, 1)->one; #=> 0
zip(list..)

Returns zipped list with arguments. The length of returned list is length of receiver.

[ E(1..3)->zip([qw/a b c/]) ]; #=> [ [1, "a"], [2, "b"], [3, "c"] ]
with_index

Returns zipped with count.

E("a", "b", "c")->with_index->each(sub {
	my ($item, $index) = @$_;
});
countup($lim), to($lim)

Returns count up list starts from first of receiver. If $lim is not supplied, this returns infinite list.

E(1)->countup; #=> List::Enumerator::Sub
[ E(1)->countup->take(3) ]; #=>  [1, 2, 3]

E(1)->to(100); #=> E(1..100)
cycle

Returns infinite list which cycles receiver.

[ E(1, 2, 3)->cycle->take(5) ]; #=> [1, 2, 3, 1, 2]
join($sep)

Returns string of receiver joined with $sep

group_by(subh{})

Returns a hash reference group by the block.

E([
	{ cat => 'a' }, { cat => 'a' },{ cat => 'a' },{ cat => 'a' },
	{ cat => 'b' }, { cat => 'b' },{ cat => 'b' },{ cat => 'b' },
	{ cat => 'c' }, { cat => 'c' },{ cat => 'c' },{ cat => 'c' },
])->group_by(sub {
	$_->{cat};
});

{
	'a' => [ { cat => 'a' }, { cat => 'a' },{ cat => 'a' },{ cat => 'a' } ],
	'b' => [ { cat => 'b' }, { cat => 'b' },{ cat => 'b' },{ cat => 'b' } ],
	'c' => [ { cat => 'c' }, { cat => 'c' },{ cat => 'c' },{ cat => 'c' } ],
};
partition(sub {})
my ($even, $odd) = E(1..10)->partition(sub { $_ % 2 == 0 });
include($target), is_include($target)

If receiver include $target this return true.

map(sub {}), collect(sub {})

map.

each(sub {})

Iterate items.

each_index

Iterate indexes with block.

each_slice($n, sub {})
E(1)->countup->each_slice(3)->take(3)->to_a;

[
	[1, 2, 3],
	[4, 5, 6],
	[7, 8, 9],
];
each_cons($n, sub {})
E(1)->countup->each_cons(3)->take(3)->to_a;

[
	[1, 2, 3],
	[2, 3, 4],
	[3, 4, 5]
];
choice, sample

Returns one item in receiver randomly.

shuffle

Returns randomized array of receiver.

transpose

Returns transposed array of receiver.

[ E([
	[1, 2],
	[3, 4],
	[5, 6],
])->transpose ]

[
	[1, 3, 5],
	[2, 4, 6],
];
to_list

Returns expanded array or array reference.

E(1)->countup->take(5)->to_list;     #=> [1, 2, 3, 4, 5]
[ E(1)->countup->take(5)->to_list ]; #=> [1, 2, 3, 4, 5]
to_a

Returns expanded array reference.

E(1)->countup->take(5)->to_a;     #=> [1, 2, 3, 4, 5]
[ E(1)->countup->take(5)->to_a ]; #=> [ [1, 2, 3, 4, 5] ]
expand

Returns new List::Enumerator::Array with expanded receiver.

dump

Dump receiver.

stop

Throw StopIteration exception.

my $list = E({
	next => sub {
		$_->stop;
	}
});

$list->to_a; #=> [];

Concept

  • Lazy evaluation for infinite list (ex. cycle)

  • Method chain

  • Read the context

  • Applicable

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.