DR::Tarantool::Iterator(3pm) | User Contributed Perl Documentation | DR::Tarantool::Iterator(3pm) |
DR::Tarantool::Iterator - an iterator and a 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 ); }
A constructor.
Arguments
The constructor is invoked on with three arguments: item, item_index and iterator, for example:
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
Clone the iterator object, but do not clone the tuples. This method can be used to create an iterator that has a different item_class and (or) item_constructor.
If clone_items argument is true, the function clones the tuple list as well.
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 is sorted, too, but $iter2 is not
Return the number of tuples available through the iterator.
Return one tuple from the iterator by its index (or croak an error if the index is out of range).
Return one raw tuple from the iterator by its index (or croak error if the index is out of range).
In other words, this method ignores item_class and item_constructor.
Sort the contents referred to by the iterator (changes the current iterator object). The compare function receives two raw objects:
$iter->raw_sort(sub { $_[0]->field cmp $_[1]->field });
Sort the contents referred to by the iterator (changes the current object). The compare function receives two constructed objects:
$iter->sort(sub { $_[0]->field <=> $_[1]->field });
Find all objects in the set referred to by the iterator that match a given search criteria (linear search).
my $admins = $users->grep(sub { $_[0]->is_admin });
Same as grep, but works on raw objects.
my $admins = $users->raw_grep(sub { $_[0]->is_admin });
An alias for item method.
Return true if the iterator contains a tuple with the given index.
my $item = $iter->exists(10) ? $iter->get(10) : somethig_else();
Return the next tuple, or undef in case of eof.
while(my $item = $iter->next) { do_something_with( $item ); }
Index of the current tuple can be queried with function 'iter'.
Return index of the tuple at the current iterator position.
Reset iteration index, return the previous value of the index.
Return all tuples available through the 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/return the tuple class. If the value is defined, the iterator blesses tuples with it (and also calls item_constructor if it is set).
Set/return the tuple constructor. The value is used only if item_class is defined.
Push a tuple into the iterator.
Return/set an application-specific context maintained in the iterator object. This can be useful to pass additional state to item_constructor.
2018-11-02 | perl v5.28.0 |