Data::Object::Array
Data-Object Array Class
use Data::Object::Array; my $array = Data::Object::Array->new([1..9]);
Data::Object::Array provides routines for operating on Perl 5 array references. Array methods work on array references. Users of these methods should be aware of the methods that modify the array reference itself as opposed to returning a new array reference. Unless stated, it may be safe to assume that the following methods copy, modify and return new array references based on their function.
This package implements the following methods.
all(CodeRef $arg1, Any @args) : NumObject
The all method returns true if all of the elements in the array meet the criteria set by the operand and rvalue. This method returns a Data::Object::Number object.
# given [2..5] $array->all(fun ($value, @args) { $value > 1; # 1, true }); $array->all(fun ($value, @args) { $value > 3; # 0; false });
any(CodeRef $arg1, Any @args) : NumObject
The any method returns true if any of the elements in the array meet the criteria set by the operand and rvalue. This method returns a Data::Object::Number object.
# given [2..5] $array->any(fun ($value) { $value > 5; # 0; false }); $array->any(fun ($value) { $value > 3; # 1; true });
clear() : Object
The clear method is an alias to the empty method. This method returns a Data::Object::Undef object. This method is an alias to the empty method. Note: This method modifies the array.
# given ['a'..'g'] $array->clear; # []
count() : NumObject
The count method returns the number of elements within the array. This method returns a Data::Object::Number object.
# given [1..5] $array->count; # 5
defined() : NumObject
The defined method returns true if the element within the array at the index specified by the argument meets the criteria for being defined, otherwise it returns false. This method returns a Data::Object::Number object.
# given [1,2,undef,4,5] $array->defined(2); # 0; false $array->defined(1); # 1; true
delete(Int $arg1) : Any
The delete method returns the value of the element within the array at the index specified by the argument after removing it from the array. This method returns a data type object to be determined after execution. Note: This method modifies the array.
# given [1..5] $array->delete(2); # 3
each(CodeRef $arg1, Any @args) : Object
The each method iterates over each element in the array, executing the code reference supplied in the argument, passing the routine the index and value at the current position in the loop. This method returns a Data::Object::Array object.
# given ['a'..'g'] $array->each(fun ($index, $value) { ... });
each_key(CodeRef $arg1, Any @args) : Object
The each_key method iterates over each element in the array, executing the code reference supplied in the argument, passing the routine the index at the current position in the loop. This method returns a Data::Object::Array object.
# given ['a'..'g'] $array->each_key(fun ($index) { ... });
each_n_values(Num $arg1, CodeRef $arg2, Any @args) : Object
The each_n_values method iterates over each element in the array, executing the code reference supplied in the argument, passing the routine the next n values until all values have been seen. This method returns a Data::Object::Array object.
# given ['a'..'g'] $array->each_n_values(4, fun (@values) { $values[1] # a $values[2] # b $values[3] # c $values[4] # d ... });
The each_value method iterates over each element in the array, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop. This method returns a Data::Object::Array object.
# given ['a'..'g'] $array->each_value(fun ($value, @args) { ... });
empty() : Object
The empty method drops all elements from the array. This method returns a Data::Object::Array object. Note: This method modifies the array.
# given ['a'..'g'] $array->empty; # []
eq(Any $arg1) : NumObject
This method is a consumer requirement but has no function and is not implemented. This method will throw an exception if called.
# given $array $array->eq; # exception thrown
exists(Int $arg1) : NumObject
The exists method returns true if the element within the array at the index specified by the argument exists, otherwise it returns false. This method returns a Data::Object::Number object.
# given [1,2,3,4,5] $array->exists(5); # 0; false $array->exists(0); # 1; true
first() : Any
The first method returns the value of the first element in the array. This method returns a data type object to be determined after execution.
# given [1..5] $array->first; # 1
ge(Any $arg1) : NumObject
# given $array $array->ge; # exception thrown
get(Int $arg1) : Any
The get method returns the value of the element in the array at the index specified by the argument. This method returns a data type object to be determined after execution.
# given [1..5] $array->get(0); # 1;
grep(CodeRef $arg1, Any @args) : ArrayObject
The grep method iterates over each element in the array, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop and returning a new array reference containing the elements for which the argument evaluated true. This method returns a Data::Object::Array object.
# given [1..5] $array->grep(fun ($value) { $value >= 3 }); # [3,4,5]
gt(Any $arg1) : NumObject
# given $array $array->gt; # exception thrown
hash() : HashObject
The hash method returns a hash reference where each key and value pairs corresponds to the index and value of each element in the array. This method returns a Data::Object::Hash object.
# given [1..5] $array->hash; # {0=>1,1=>2,2=>3,3=>4,4=>5}
hashify(CodeRef $arg1, Any $arg2) : HashObject
The hashify method returns a hash reference where the elements of array become the hash keys and the corresponding values are assigned a value of 1. This method returns a Data::Object::Hash object.
# given [1..5] $array->hashify; # {1=>1,2=>1,3=>1,4=>1,5=>1} $array->hashify(fun ($value) { $value % 2 }); # {1=>1,2=>0,3=>1,4=>0,5=>1}
head() : Any
The head method returns the value of the first element in the array. This method returns a data type object to be determined after execution.
# given [9,8,7,6,5] my $head = $array->head; # 9
invert() : Any
The invert method returns an array reference containing the elements in the array in reverse order. This method returns a Data::Object::Array object.
# given [1..5] $array->invert; # [5,4,3,2,1]
iterator() : CodeObject
The iterator method returns a code reference which can be used to iterate over the array. Each time the iterator is executed it will return the next element in the array until all elements have been seen, at which point the iterator will return an undefined value. This method returns a Data::Object::Code object.
# given [1..5] my $iterator = $array->iterator; while (my $value = $iterator->next) { say $value; # 1 }
join(Str $arg1) : StrObject
The join method returns a string consisting of all the elements in the array joined by the join-string specified by the argument. Note: If the argument is omitted, an empty string will be used as the join-string. This method returns a Data::Object::String object.
# given [1..5] $array->join; # 12345 $array->join(', '); # 1, 2, 3, 4, 5
keyed(Str $arg1) : HashObject
The keyed method returns a hash reference where the arguments become the keys, and the elements of the array become the values. This method returns a Data::Object::Hash object.
# given [1..5] $array->keyed('a'..'d'); # {a=>1,b=>2,c=>3,d=>4}
keys() : ArrayObject
The keys method returns an array reference consisting of the indicies of the array. This method returns a Data::Object::Array object.
# given ['a'..'d'] $array->keys; # [0,1,2,3]
last() : Any
The last method returns the value of the last element in the array. This method returns a data type object to be determined after execution.
# given [1..5] $array->last; # 5
le(Any $arg1) : NumObject
# given $array $array->le; # exception thrown
length() : NumObject
The length method returns the number of elements in the array. This method returns a Data::Object::Number object.
# given [1..5] $array->length; # 5
list() : ArrayObject
The list method returns a shallow copy of the underlying array reference as an array reference. This method return a Data::Object::Array object.
# given $array my $list = $array->list;
lt(Any $arg1) : NumObject
# given $array $array->lt; # exception thrown
map(CodeRef $arg1, Any $arg2) : ArrayObject
The map method iterates over each element in the array, executing the code reference supplied in the argument, passing the routine the value at the current position in the loop and returning a new array reference containing the elements for which the argument returns a value or non-empty list. This method returns a Data::Object::Array object.
# given [1..5] $array->map(sub{ shift + 1 }); # [2,3,4,5,6]
max() : Any
The max method returns the element in the array with the highest numerical value. All non-numerical element are skipped during the evaluation process. This method returns a Data::Object::Number object.
# given [8,9,1,2,3,4,5] $array->max; # 9
min() : Any
The min method returns the element in the array with the lowest numerical value. All non-numerical element are skipped during the evaluation process. This method returns a Data::Object::Number object.
# given [8,9,1,2,3,4,5] $array->min; # 1
ne(Any $arg1) : NumObject
# given $array $array->ne; # exception thrown
none(CodeRef $arg1, Any $arg2) : NumObject
The none method returns true if none of the elements in the array meet the criteria set by the operand and rvalue. This method returns a Data::Object::Number object.
# given [2..5] $array->none(fun ($value) { $value <= 1; # 1; true }); $array->none(fun ($value) { $value <= 2; # 0; false });
nsort() : ArrayObject
The nsort method returns an array reference containing the values in the array sorted numerically. This method returns a Data::Object::Array object.
# given [5,4,3,2,1] $array->nsort; # [1,2,3,4,5]
one(CodeRef $arg1, Any $arg2) : NumObject
The one method returns true if only one of the elements in the array meet the criteria set by the operand and rvalue. This method returns a Data::Object::Number object.
# given [2..5] $array->one(fun ($value) { $value == 5; # 1; true }); $array->one(fun ($value) { $value == 6; # 0; false });
pairs() : ArrayObject
The pairs method is an alias to the pairs_array method. This method returns a Data::Object::Array object. This method is an alias to the pairs_array method.
# given [1..5] $array->pairs; # [[0,1],[1,2],[2,3],[3,4],[4,5]]
The pairs_array method returns an array reference consisting of array references where each sub-array reference has two elements corresponding to the index and value of each element in the array. This method returns a Data::Object::Array object.
# given [1..5] $array->pairs_array; # [[0,1],[1,2],[2,3],[3,4],[4,5]]
The pairs_hash method returns a hash reference where each key and value pairs corresponds to the index and value of each element in the array. This method returns a Data::Object::Hash object.
# given [1..5] $array->pairs_hash; # {0=>1,1=>2,2=>3,3=>4,4=>5}
part(CodeRef $arg1, Any $arg2) : Tuple[ArrayRef, ArrayRef]
The part method iterates over each element in the array, executing the code reference supplied in the argument, using the result of the code reference to partition to array into two distinct array references. This method returns an array reference containing exactly two array references. This method returns a Data::Object::Array object.
# given [1..10] $array->part(fun ($value) { $value > 5 }); # [[6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]
pop() : Any
The pop method returns the last element of the array shortening it by one. Note, this method modifies the array. This method returns a data type object to be determined after execution. Note: This method modifies the array.
# given [1..5] $array->pop; # 5
push(Any $arg1) : Any
The push method appends the array by pushing the agruments onto it and returns itself. This method returns a data type object to be determined after execution. Note: This method modifies the array.
# given [1..5] $array->push(6,7,8); # [1,2,3,4,5,6,7,8]
random() : NumObject
The random method returns a random element from the array. This method returns a data type object to be determined after execution.
# given [1..5] $array->random; # 4
reverse() : ArrayObject
The reverse method returns an array reference containing the elements in the array in reverse order. This method returns a Data::Object::Array object.
# given [1..5] $array->reverse; # [5,4,3,2,1]
rnsort() : ArrayObject
The rnsort method returns an array reference containing the values in the array sorted numerically in reverse. This method returns a Data::Object::Array object.
# given [5,4,3,2,1] $array->rnsort; # [5,4,3,2,1]
roles() : ArrayRef
The roles method returns the list of roles attached to object. This method returns a Data::Object::Array object.
# given $array $array->roles;
rotate() : ArrayObject
The rotate method rotates the elements in the array such that first elements becomes the last element and the second element becomes the first element each time this method is called. This method returns a Data::Object::Array object. Note: This method modifies the array.
# given [1..5] $array->rotate; # [2,3,4,5,1] $array->rotate; # [3,4,5,1,2] $array->rotate; # [4,5,1,2,3]
rsort() : ArrayObject
The rsort method returns an array reference containing the values in the array sorted alphanumerically in reverse. This method returns a Data::Object::Array object.
# given ['a'..'d'] $array->rsort; # ['d','c','b','a']
rules() : ArrayRef
The rules method returns consumed rules.
my $rules = $array->rules();
self() : Object
The self method returns the calling object (noop).
my $self = $array->self();
set(Str $arg1, Any $arg2) : Any
The set method returns the value of the element in the array at the index specified by the argument after updating it to the value of the second argument. This method returns a data type object to be determined after execution. Note: This method modifies the array.
# given [1..5] $array->set(4,6); # [1,2,3,4,6]
shift() : Any
The shift method returns the first element of the array shortening it by one. This method returns a data type object to be determined after execution. Note: This method modifies the array.
# given [1..5] $array->shift; # 1
size() : NumObject
The size method is an alias to the length method. This method returns a Data::Object::Number object. This method is an alias to the length method.
# given [1..5] $array->size; # 5
slice(Any $arg1) : Any
The slice method returns an array reference containing the elements in the array at the index(es) specified in the arguments. This method returns a Data::Object::Array object.
# given [1..5] $array->slice(2,4); # [3,5]
sort() : ArrayObject
The sort method returns an array reference containing the values in the array sorted alphanumerically. This method returns a Data::Object::Array object.
# given ['d','c','b','a'] $array->sort; # ['a','b','c','d']
sum() : NumObject
The sum method returns the sum of all values for all numerical elements in the array. All non-numerical element are skipped during the evaluation process. This method returns a Data::Object::Number object.
# given [1..5] $array->sum; # 15
tail() : Any
The tail method returns an array reference containing the second through the last elements in the array omitting the first. This method returns a Data::Object::Array object.
# given [1..5] $array->tail; # [2,3,4,5]
unique() : ArrayObject
The unique method returns an array reference consisting of the unique elements in the array. This method returns a Data::Object::Array object.
# given [1,1,1,1,2,3,1] $array->unique; # [1,2,3]
unshift() : Any
The unshift method prepends the array by pushing the agruments onto it and returns itself. This method returns a data type object to be determined after execution. Note: This method modifies the array.
# given [1..5] $array->unshift(-2,-1,0); # [-2,-1,0,1,2,3,4,5]
values(Str $arg1) : ArrayObject
The values method returns an array reference consisting of the elements in the array. This method essentially copies the content of the array into a new container. This method returns a Data::Object::Array object.
# given [1..5] $array->values; # [1,2,3,4,5]
This package inherits all behavior from the folowing role(s):
Data::Object::Role::Detract
Data::Object::Role::Dumper
Data::Object::Role::Output
Data::Object::Role::Throwable
This package adheres to the requirements in the folowing rule(s):
Data::Object::Rule::Collection
Data::Object::Rule::Comparison
Data::Object::Rule::Defined
Data::Object::Rule::List
To install Data::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Object
CPAN shell
perl -MCPAN -e shell install Data::Object
For more information on module installation, please visit the detailed CPAN module installation guide.