Dmitry E. Oboukhov

NAME

DR::Tarantool::Iterator - iterator/container class for DR::Tarantool

SYNOPSIS

    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 );
    }

METHODS

new

Constructor.

Arguments

  • Array of items.

  • List of named arguments, that can be:

    item_class

    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.

    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
    data

    Any Your data You want to assign to iterator.

clone(%opt)

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

count

returns count of items that are contained in iterator

item

returns one item from iterator by its number (or croaks error for wrong numbers)

raw_item

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'.

raw_sort(&)

resorts iterator (changes current object). Compare function receives two raw objects:

    $iter->raw_sort(sub { $_[0]->field cmp $_[1]->field });

sort(&)

resorts iterator (changes current object). Compare function receives two objects:

    $iter->sort(sub { $_[0]->field <=> $_[1]->field });

grep(&)

greps iterator (returns new iterator).

    my $admins = $users->grep(sub { $_[0]->is_admin });

raw_grep(&)

greps iterator (returns new iterator). grep function receives raw item.

    my $admins = $users->grep(sub { $_[0]->is_admin });

get

The same as item method.

exists

Returns true if iterator contains element with noticed index.

    my $item = $iter->exists(10) ? $iter->get(10) : somethig_else();

next

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'.

iter

returns current iterator index.

reset

resets iterator index, returns previous index value.

all

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

item_class

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

item_constructor

set/returns item constructor. The value can be used only if item_class is defined.

push

push item into iterator.

data

returns/set user's data assigned to the iterator