The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::MoCo::List - Array iterator for DBIx::MoCo.

SYNOPSIS

  my $array_ref = [
    {name => 'jkondo'},
    {name => 'cinnamon'}
  ];
  my $list = DBIx::MoCo::List->new($array_ref);

  $list->size;              #=> 2
  my $first = $list->shift; #=> {name => 'jkondo'}
  $list->push($first);      #=> [{name => 'cinnamon'}, {name => 'jkondo'}];

  # DBIx::MoCo::List provides much more useful methods. For more
  # details, see the sections below.

METHODS

dump ()

Dump the content of $self using Data::Dumper.

push ( @array )
unshift ( @array )

Sets the argument into $self, a refernce to an array blessed by DBIx::MoCo::List, like the same name functions provided by Perl core, then returns a DBIx::MoCo::List object.

  my $list = DBIx::MoCo::List->new([qw(1 2 3)]);
  $list->push(4, 5); #=> [1, 2, 3, 4, 5]
  $list->unshift(0); #=> [0, 1, 2, 3, 4, 5]
concat ( \@array )
prepend ( \@array )

They're almost the same as push()/unshift() described above except that the argument shoud be a reference to an array.

  my $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->concat([4, 5]); #=> [1, 2, 3, 4, 5]
  $list->prepend([0]);   #=> [0, 1, 2, 3, 4, 5]
shift ()
pop ()

Pulls out the first/last element from $self, a refernce to an array blessed by DBIx::MoCo::List, then returns it like the same name functions in Perl core.

  $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->shift; #=> 1
  $list->pop;   #=> 3
  $list->dump   #=> [2]
first ()
last ()

Returns the first/last element of $self, a refernce to an array blessed by DBIx::MoCo::List. These methods aren't destructive contrary to shift()/pop() method.

  $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->first; #=> 1
  $list->last;  #=> 3
  $list->dump   #=> [1, 2, 3]
slice ( $start, $end )

Returns the elements whose indexes are between $start and $end as a DBIx::MoCo::List object.

  $list = DBIx::MoCo::List->new([qw(1 2 3 4)]);
  $list->slice(1, 2) #=> [2, 3]
zip ( \@array1, \@array2, ... )

Bundles up the elements in each arguments into an array or a DBIx::MoCo::List object along with the context.

  my $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->zip([4, 5, 6], [7, 8, 9]);
      #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

  # When the numbers of each list are different...
  $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->zip([4, 5], [7, 8, 9]);
      #=> [[1, 4, 7], [2, 5, 8], [3, undef, 9]]

  my $list   = DBIx::MoCo::List->new([1, 2]);
  $list->zip([4, 5], [7, 8, 9]);
      #=> [[1, 4, 7], [2, 5, 8]]
delete ( $value, $code )

Deletes the same values as $value in $self, a refernce to an array blessed by DBIx::MoCo::List, and returns the value if found. If the value is not found in $self and $code is passed in, the code is executed using the value as an argument to find the value to be deleted.

  $list = DBIx::MoCo::List->new([1, 2, 3, 2, 1]);
  $list->delete(2); #=> 2
  $list->dump       #=> [1, 3, 1]
delete_at ( $pos )

Deletes the element at $pos and returns it.

  $list = DBIx::MoCo::List->new([1, 2, 3, 2, 1]);
  $list->delete_at(3); #=> 2
  $list->dump          #=> [1, 2, 3, 1]
delete_if ( $code )

Deletes the elements if $code returns false value with each element as an argument.

  $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
  $list->delete_if(sub { ($_ % 2) == 0) });
  $list->dump #=> [2, 4]
inject ( $result, $code )

Executes folding calculation using $code through each element and returns the result.

  $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
  $list->inject(0, sub { $_[0] + $_[1] }); #=> 10
join ( $delimiter )

Joins all the elements by $delimiter.

  $list = DBIx::MoCo::List->new([0 1 2 3]);
  $list->join(', ') #=> '0, 1, 2, 3'
each_index ( $code )

Executes $code with each index of $self, a refernce to an array blessed by DBIx::MoCo::List.

  $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->each_index(sub { do_something($_) });
each ( $code )

Executes $code with each value of $self, a refernce to an array blessed by DBIx::MoCo::List.

  $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->each(sub { do_something($_) });
collect ( $code )

Executes $code with each element of $self, a refernce to an array blessed by DBIx::MoCo::List using CORE::map() and returns the results as a list or DBIx::MoCo:List object along with the context.

  $list = DBIx::MoCo::List->new([1, 2, 3]);
  $list->map(sub { $_ * 2 }); #=> [2, 4, 6]
map ( $code )

An alias of collect() method described above.

grep ( $code )

Executes $code with each element of $self, a refernce to an array blessed by DBIx::MoCo::List using CORE::grep() and returns the results as a list or DBIx::MoCo:List object along with the context.

  $list = DBIx::MoCo::List->new([qw(1 2 3 4)]);
  $list->grep(sub { ($_ % 2) == 0 }); #=> [2, 4]
find ( $code )

Returns the first value found in $self, a refernce to an array blessed by DBIx::MoCo::List, as a result of $code..

  $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
  $list->find(sub { ($_ % 2) == 0 }); #=> 2
index_of ( $arg )

Returns index of given target or given code returns true.

  $list = DBIx::MoCo::List->new([qw(foo bar baz)]);
  $list->index_of('bar');                  #=> 1
  $list->index_of(sub { shift eq 'bar' }); #=> 1
sort ( $code )

Sorts out each element and returns the result as a list or DBIx::MoCo:List object along with the context.

  $list = DBIx::MoCo::List->new([qw(3 2 4 1]);
  $list->sort;                          #=> [1, 2, 3, 4]
  $list->sort(sub { $_[1] <=> $_[0] }); #=> [4, 3, 2, 1]
compact ()

Eliminates undefined values in $self, a refernce to an array blessed by DBIx::MoCo::List.

  $list = DBIx::MoCo::List->new([1, 2, undef, 3, undef, 4]);
  $list->compact; #=> [1, 2, 3, 4]
length ()

Returns the length of $self, a refernce to an array blessed by DBIx::MoCo::List.

  $list = DBIx::MoCo::List->new([qw(1 2 3 4)]);
  $list->length; #=> 4
size ()

An alias of length() method described above.

flatten ()

Returns a list or DBIx::MoCo::List object which is recursively flattened out.

  $list = DBIx::MoCo::List->new([1, [2, 3, [4], 5]]);
  $list->flattern; #=> [1, 2, 3, 4, 5]
is_empty ()

Returns true if $self, a refernce to an array blessed by DBIx::MoCo::List, is empty.

uniq ()

Uniquifies the elements in $self, a refernce to an array blessed by DBIx::MoCo::List, and returns the result.

  $list = DBIx::MoCo::List->new([1, 2, 2, 3, 3, 4])
  $list->uniq; #=> [1, 2, 3, 4]
reduce ( $code )

Reduces the list by $code.

  # finds the maximum value
  $list = DBIx::MoCo::List->new([4, 1, 3, 2])
  $list->reduce(sub { $_[0] > $_[1] ? $_[0] : $_[1] }); #=> 4

See List::Util to get to know about details of reduce().

reverse ()

Returns an reversely ordered $self, a refernce to an array blessed by DBIx::MoCo::List.

  $list = DBIx::MoCo::List->new([4, 1, 3, 2])
  $list->reverse; #=> [2, 3, 1, 4]
dup ()

Returns a duplicated $self, a refernce to an array blessed by DBIx::MoCo::List.

sum ()

Returns the sum of each element in $self, a refernce to an array blessed by DBIx::MoCo::List.

  $list = DBIx::MoCo::List->new([1, 2, 3, 4]);
  $list->sum; #=> 10

AUTO-GENERATED METHODS

map_XXX ()

Returns the results of XXX() method of each elements as a list or a DBIx::MoCo::List object along with the context.

  my $list  = Your::MoCo::Class->retrieve_all;
  my $names = $list->map_name;

In this case, $names is a list of the return values of name() method of each element in $list.

SEE ALSO

DBIx::MoCo, List::Util, List::MoreUtils

AUTHOR

Junya Kondo, <jkondo@hatena.com>, Naoya Ito, <naoya@hatena.ne.jp>, Kentaro Kuribayashi, <kentarok@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) Hatena Inc. All Rights Reserved.

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