List::Rubyish - Array iterator like the Ruby
my $array_ref = [ {name => 'jkondo'}, {name => 'cinnamon'} ]; my $list = List::Rubyish->new($array_ref); $list->size; #=> 2 my $first = $list->shift; #=> {name => 'jkondo'} $list->push($first); #=> [{name => 'cinnamon'}, {name => 'jkondo'}]; # List::Rubyish provides much more useful methods. For more # details, see the sections below.
DBIx::MoCo::List is very useful, However installation is complex.
List::Rubyish was made in order to enable use of DBIx::MoCo::List independently.
Dump the content of $self using Data::Dumper.
$self
Sets the argument into $self, a refernce to an array blessed by List::Rubyish, like the same name functions provided by Perl core, then returns a List::Rubyish object.
my $list = List::Rubyish->new([qw(1 2 3)]); $list->push(4, 5); #=> [1, 2, 3, 4, 5] $list->unshift(0); #=> [0, 1, 2, 3, 4, 5]
They're almost the same as push()/unshift() described above except that the argument shoud be a reference to an array.
push()
unshift()
my $list = List::Rubyish->new([1, 2, 3]); $list->concat([4, 5]); #=> [1, 2, 3, 4, 5] $list->prepend([0]); #=> [0, 1, 2, 3, 4, 5]
Pulls out the first/last element from $self, a refernce to an array blessed by List::Rubyish, then returns it like the same name functions in Perl core.
$list = List::Rubyish->new([1, 2, 3]); $list->shift; #=> 1 $list->pop; #=> 3 $list->dump #=> [2]
Returns the first/last element of $self, a refernce to an array blessed by List::Rubyish. These methods aren't destructive contrary to shift()/pop() method.
shift()
pop()
$list = List::Rubyish->new([1, 2, 3]); $list->first; #=> 1 $list->last; #=> 3 $list->dump #=> [1, 2, 3]
Returns the elements whose indexes are between $start and $end as a List::Rubyish object.
$start
$end
$list = List::Rubyish->new([qw(1 2 3 4)]); $list->slice(1, 2) #=> [2, 3]
Bundles up the elements in each arguments into an array or a List::Rubyish object along with the context.
my $list = List::Rubyish->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 = List::Rubyish->new([1, 2, 3]); $list->zip([4, 5], [7, 8, 9]); #=> [[1, 4, 7], [2, 5, 8], [3, undef, 9]] my $list = List::Rubyish->new([1, 2]); $list->zip([4, 5], [7, 8, 9]); #=> [[1, 4, 7], [2, 5, 8]]
Deletes the same values as $value in $self, a refernce to an array blessed by List::Rubyish, 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.
$value
$code
$list = List::Rubyish->new([1, 2, 3, 2, 1]); $list->delete(2); #=> 2 $list->dump #=> [1, 3, 1]
Deletes the element at $pos and returns it.
$pos
$list = List::Rubyish->new([1, 2, 3, 2, 1]); $list->delete_at(3); #=> 2 $list->dump #=> [1, 2, 3, 1]
Deletes the elements if $code returns true value with each element as an argument.
$list = List::Rubyish->new([1, 2, 3, 4]); $list->delete_if(sub { ($_ % 2) == 0) }); $list->dump #=> [1, 3]
Executes folding calculation using $code through each element and returns the result.
$list = List::Rubyish->new([1, 2, 3, 4]); $list->inject(0, sub { $_[0] + $_[1] }); #=> 10
Joins all the elements by $delimiter.
$delimiter
$list = List::Rubyish->new([0 1 2 3]); $list->join(', ') #=> '0, 1, 2, 3'
Executes $code with each index of $self, a refernce to an array blessed by List::Rubyish.
$list = List::Rubyish->new([1, 2, 3]); $list->each_index(sub { do_something($_) });
Executes $code with each value of $self, a refernce to an array blessed by List::Rubyish.
$list = List::Rubyish->new([1, 2, 3]); $list->each(sub { do_something($_) });
Executes $code with each element of $self, a refernce to an array blessed by List::Rubyish using CORE::map() and returns the results as a list or List::Rubyish object along with the context.
$list = List::Rubyish->new([1, 2, 3]); $list->map(sub { $_ * 2 }); #=> [2, 4, 6]
An alias of collect() method described above.
collect()
Executes $code with each element of $self, a refernce to an array blessed by List::Rubyish using CORE::grep() and returns the results as a list or List::Rubyish object along with the context.
$list = List::Rubyish->new([qw(1 2 3 4)]); $list->grep(sub { ($_ % 2) == 0 }); #=> [2, 4]
Returns the first value found in $self, a refernce to an array blessed by List::Rubyish, as a result of $code..
$list = List::Rubyish->new([1, 2, 3, 4]); $list->find(sub { ($_ % 2) == 0 }); #=> 2
Returns the values found in $self, a refernce to an array blessed by List::Rubyish, as a result of $code..
$list = List::Rubyish->new([1, 2, 3, 4]); $list->select(sub { ($_ % 2) == 0 }); #=> 2, 4
Returns index of given target or given code returns true.
$list = List::Rubyish->new([qw(foo bar baz)]); $list->index_of('bar'); #=> 1 $list->index_of(sub { shift eq 'bar' }); #=> 1
Sorts out each element and returns the result as a list or List::Rubyish object along with the context.
$list = List::Rubyish->new([qw(3 2 4 1]); $list->sort; #=> [1, 2, 3, 4] $list->sort(sub { $_[1] <=> $_[0] }); #=> [4, 3, 2, 1]
Sorts out each element with Schwartzian transform returns the result as a list or List::Rubyish object along with the context.
$list = List::Rubyish->new([ [3], [2], [4], [1]]); $list->sort_by(sub { $_->[0] }); #=> [[1], [2], [3], [4]] $list->sort_by(sub { $_->[0] }, sub { $_[1} <=> $_[0] } ); #=> [[4], [3], [2], [1]]
Eliminates undefined values in $self, a refernce to an array blessed by List::Rubyish.
$list = List::Rubyish->new([1, 2, undef, 3, undef, 4]); $list->compact; #=> [1, 2, 3, 4]
Returns the length of $self, a refernce to an array blessed by List::Rubyish.
$list = List::Rubyish->new([qw(1 2 3 4)]); $list->length; #=> 4
An alias of length() method described above.
length()
Returns a list or List::Rubyish object which is recursively flattened out.
$list = List::Rubyish->new([1, [2, 3, [4], 5]]); $list->flattern; #=> [1, 2, 3, 4, 5]
Returns true if $self, a refernce to an array blessed by List::Rubyish, is empty.
Uniquifies the elements in $self, a refernce to an array blessed by List::Rubyish, and returns the result.
$list = List::Rubyish->new([1, 2, 2, 3, 3, 4]) $list->uniq; #=> [1, 2, 3, 4]
Reduces the list by $code.
# finds the maximum value $list = List::Rubyish->new([4, 1, 3, 2]) $list->reduce(sub { $_[0] > $_[1] ? $_[0] : $_[1] }); #=> 4
See List::Util to get to know about details of reduce().
reduce()
Returns an reversely ordered $self, a refernce to an array blessed by List::Rubyish.
$list = List::Rubyish->new([4, 1, 3, 2]) $list->reverse; #=> [2, 3, 1, 4]
Returns a duplicated $self, a refernce to an array blessed by List::Rubyish.
Returns the sum of each element in $self, a refernce to an array blessed by List::Rubyish.
$list = List::Rubyish->new([1, 2, 3, 4]); $list->sum; #=> 10
DBIx::MoCo::List, List::Util, List::MoreUtils, http://github.com/naoya/list-rubylike, http://d.hatena.ne.jp/naoya/20080419/1208579525, http://www.ruby-lang.org/ja/man/html/Enumerable.html
Junya Kondo, <jkondo@hatena.com>, Naoya Ito, <naoya@hatena.ne.jp>, Kentaro Kuribayashi, <kentarok@gmail.com>, Yuichi Tateno, <secondlife at hatena ne jp<gt>,
Kazuhiro Osawa <yappo <at> shibuya <döt> pl>
naoya, kentaro, tokuhirom, kan, lopnor
svn co http://svn.coderepos.org/share/lang/perl/List-Rubyish/trunk List-Rubyish
List::Rubyish is Subversion repository is hosted at http://coderepos.org/share/. patches and collaborators are welcome.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Copyright (C) Hatena Inc. All Rights Reserved.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in '<döt>'. Assuming UTF-8
To install List::Rubyish, copy and paste the appropriate command in to your terminal.
cpanm
cpanm List::Rubyish
CPAN shell
perl -MCPAN -e shell install List::Rubyish
For more information on module installation, please visit the detailed CPAN module installation guide.