DR::Tarantool::Iterator - iterator/container class for DR::Tarantool
use DR::Tarantool::Iterator; my $iter = DR::Tarantool::Iterator->new([1, 2, 3]); my $item0 = $iter->item(0); my @all = $iter->all; my $all = $iter->all; while(my $item = $iter->next) { do_something_with_item( $item ); }
Constructor.
Array of items.
List of named arguments, that can be:
Name of class to bless/construct item. If the field is 'ARRAYREF' then the first element of the array is item_class, and the second element is item_constructor.
Name of constructor for item. If the value is undefined and item_class is defined, iterator will bless value instead construct.
If item_constructor is used, constructor method will be receive three arguments: item, item_index and iterator.
my $iter = DR::Tarantool::Iterator->new( [ [1], [2], [3] ], item_class => 'MyClass', item_constructor => 'new' ); my $iter = DR::Tarantool::Iterator->new( # the same [ [1], [2], [3] ], item_class => [ 'MyClass', 'new' ] ); my $item = $iter->item(0); my $item = MyClass->new( [1], 0, $iter ); # the same my $item = $iter->item(2); my $item = MyClass->new( [3], 2, $iter ); # the same
Any Your data You want to assign to iterator.
clone iterator object (doesn't clone items). It is usable if You want to have iterator that have the other item_class and (or) item_constructor.
If clone_items argument is true, the function will clone itemlist, too.
my $iter1 = $old_iter->clone(item_class => [ 'MyClass', 'new' ]); my $iter2 = $old_iter->clone(item_class => [ 'MyClass', 'new' ], clone_items => 1); $old_iter->sort(sub { $_[0]->name cmp $_[1]->name }); # $iter1 will be resorted, too, but $iter2 will not be
returns count of items that are contained in iterator
returns one item from iterator by its number (or croaks error for wrong numbers)
returns one raw item from iterator by its number (or croaks error for wrong numbers).
The function differ from item: it doesn't know about 'item_class'.
resorts iterator (changes current object). Compare function receives two raw objects:
$iter->raw_sort(sub { $_[0]->field cmp $_[1]->field });
resorts iterator (changes current object). Compare function receives two objects:
$iter->sort(sub { $_[0]->field <=> $_[1]->field });
greps iterator (returns new iterator).
my $admins = $users->grep(sub { $_[0]->is_admin });
greps iterator (returns new iterator). grep function receives raw item.
The same as item method.
Returns true if iterator contains element with noticed index.
my $item = $iter->exists(10) ? $iter->get(10) : somethig_else();
returns next element from iterator (or undef if eof).
while(my $item = $iter->next) { do_something_with( $item ); }
You can ask current element's number by function 'iter'.
returns current iterator index.
resets iterator index, returns previous index value.
returns all elements from iterator.
my @list = $iter->all; my $list_aref = $iter->all; my @abc_list = map { $_->abc } $iter->all; my @abc_list = $iter->all('abc'); # the same my @list = map { [ $_->abc, $_->cde ] } $iter->all; my @list = $iter->all('abc', 'cde'); # the same my @list = map { $_->abc + $_->cde } $iter->all; my @list = $iter->all(sub { $_[0]->abc + $_->cde }); # the same
set/returns item class. If the value isn't defined, iterator will bless fields into the class (or calls item_constructor in the class if item_constructor is defined
set/returns item constructor. The value can be used only if item_class is defined.
push item into iterator.
returns/set user's data assigned to the iterator
To install DR::Tarantool, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DR::Tarantool
CPAN shell
perl -MCPAN -e shell install DR::Tarantool
For more information on module installation, please visit the detailed CPAN module installation guide.