The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Data::Object::Array

ABSTRACT

Data-Object Array Class

SYNOPSIS

use Data::Object::Array;

my $array = Data::Object::Array->new([1..9]);

DESCRIPTION

This package provides routines for operating on Perl 5 array references.

INHERITANCE

This package inherits behaviors from:

Data::Object::Array::Base

INTEGRATIONS

This package integrates behaviors from:

Data::Object::Role::Dumpable

Data::Object::Role::Functable

Data::Object::Role::Throwable

LIBRARIES

This package uses type constraints defined by:

Data::Object::Library

METHODS

This package implements the following methods.

all

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.

all example
# given [2..5]

$array->all(fun ($value, @args) {
  $value > 1; # 1, true
});

$array->all(fun ($value, @args) {
  $value > 3; # 0; false
});

any

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.

any example
# given [2..5]

$array->any(fun ($value) {
  $value > 5; # 0; false
});

$array->any(fun ($value) {
  $value > 3; # 1; true
});

clear

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.

clear example
# given ['a'..'g']

$array->clear; # []

count

count() : NumObject

The count method returns the number of elements within the array. This method returns a Data::Object::Number object.

count example
# given [1..5]

$array->count; # 5

defined

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.

defined example
# given [1,2,undef,4,5]

$array->defined(2); # 0; false
$array->defined(1); # 1; true

delete

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.

delete example
# given [1..5]

$array->delete(2); # 3

each

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.

each example
# given ['a'..'g']

$array->each(fun ($index, $value) {
    ...
});

each_key

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.

each_key example
# given ['a'..'g']

$array->each_key(fun ($index) {
    ...
});

each_n_values

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.

each_n_values example
# given ['a'..'g']

$array->each_n_values(4, fun (@values) {
    $values[1] # a
    $values[2] # b
    $values[3] # c
    $values[4] # d
    ...
});

each_value

each_key(CodeRef $arg1, Any @args) : Object

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.

each_value example
# given ['a'..'g']

$array->each_value(fun ($value, @args) {
    ...
});

empty

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.

empty example
# given ['a'..'g']

$array->empty; # []

eq

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.

eq example
# given $array

$array->eq; # exception thrown

exists

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.

exists example
# given [1,2,3,4,5]

$array->exists(5); # 0; false
$array->exists(0); # 1; true

first

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.

first example
# given [1..5]

$array->first; # 1

ge

ge(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.

ge example
# given $array

$array->ge; # exception thrown

get

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.

get example
# given [1..5]

$array->get(0); # 1;

grep

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.

grep example
# given [1..5]

$array->grep(fun ($value) {
    $value >= 3
});

# [3,4,5]

gt

gt(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.

gt example
# given $array

$array->gt; # exception thrown

hash

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.

hash example
# given [1..5]

$array->hash; # {0=>1,1=>2,2=>3,3=>4,4=>5}

hashify

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.

hashify example
# 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.

head example
# given [9,8,7,6,5]

my $head = $array->head; # 9

invert

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.

invert example
# given [1..5]

$array->invert; # [5,4,3,2,1]

iterator

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.

iterator example
# given [1..5]

my $iterator = $array->iterator;
while (my $value = $iterator->next) {
    say $value; # 1
}

join

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.

join example
# given [1..5]

$array->join; # 12345
$array->join(', '); # 1, 2, 3, 4, 5

keyed

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.

keyed example
# given [1..5]

$array->keyed('a'..'d'); # {a=>1,b=>2,c=>3,d=>4}

keys

keys() : ArrayObject

The keys method returns an array reference consisting of the indicies of the array. This method returns a Data::Object::Array object.

keys example
# given ['a'..'d']

$array->keys; # [0,1,2,3]

last

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.

last example
# given [1..5]

$array->last; # 5

le

le(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.

le example
# given $array

$array->le; # exception thrown

length

length() : NumObject

The length method returns the number of elements in the array. This method returns a Data::Object::Number object.

length example
# given [1..5]

$array->length; # 5

list

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.

list example
# given $array

my $list = $array->list;

lt

lt(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.

lt example
# given $array

$array->lt; # exception thrown

map

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.

map example
# given [1..5]

$array->map(sub{
    shift + 1
});

# [2,3,4,5,6]

max

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.

max example
# given [8,9,1,2,3,4,5]

$array->max; # 9

min

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.

min example
# given [8,9,1,2,3,4,5]

$array->min; # 1

ne

ne(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.

ne example
# given $array

$array->ne; # exception thrown

none

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.

none example
# given [2..5]

$array->none(fun ($value) {
  $value <= 1; # 1; true
});

$array->none(fun ($value) {
  $value <= 2; # 0; false
});

nsort

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.

nsort example
# given [5,4,3,2,1]

$array->nsort; # [1,2,3,4,5]

one

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.

one example
# given [2..5]

$array->one(fun ($value) {
  $value == 5; # 1; true
});

$array->one(fun ($value) {
  $value == 6; # 0; false
});

pairs

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.

pairs example
# given [1..5]

$array->pairs; # [[0,1],[1,2],[2,3],[3,4],[4,5]]

pairs_array

pairs() : ArrayObject

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.

pairs_array example
# given [1..5]

$array->pairs_array; # [[0,1],[1,2],[2,3],[3,4],[4,5]]

pairs_hash

pairs() : ArrayObject

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.

pairs_hash example
# given [1..5]

$array->pairs_hash; # {0=>1,1=>2,2=>3,3=>4,4=>5}

part

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.

part example
# given [1..10]

$array->part(fun ($value) { $value > 5 }); # [[6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]

pop

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.

pop example
# given [1..5]

$array->pop; # 5

push

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.

push example
# given [1..5]

$array->push(6,7,8); # [1,2,3,4,5,6,7,8]

random

random() : NumObject

The random method returns a random element from the array. This method returns a data type object to be determined after execution.

random example
# given [1..5]

$array->random; # 4

reverse

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.

reverse example
# given [1..5]

$array->reverse; # [5,4,3,2,1]

rnsort

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.

rnsort example
# given [5,4,3,2,1]

$array->rnsort; # [5,4,3,2,1]

rotate

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.

rotate example
# 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

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.

rsort example
# given ['a'..'d']

$array->rsort; # ['d','c','b','a']

self

self() : Object

The self method returns the calling object (noop).

self example
my $self = $array->self();

set

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.

set example
# given [1..5]

$array->set(4,6); # [1,2,3,4,6]

shift

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.

shift example
# given [1..5]

$array->shift; # 1

size

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.

size example
# given [1..5]

$array->size; # 5

slice

kvslice(Any @args) : HashObject

The kvslice method returns a hash reference containing the elements in the array at the index(es) specified in the arguments. This method returns a Data::Object::Hash object.

slice example
# given [1..5]

$array->kvslice(2,4); # {2=>3, 4=>5}

slice

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.

slice example
# given [1..5]

$array->slice(2,4); # [3,5]

sort

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.

sort example
# given ['d','c','b','a']

$array->sort; # ['a','b','c','d']

sum

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.

sum example
# given [1..5]

$array->sum; # 15

tail

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.

tail example
# given [1..5]

$array->tail; # [2,3,4,5]

unique

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.

unique example
# given [1,1,1,1,2,3,1]

$array->unique; # [1,2,3]

unshift

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.

unshift example
# given [1..5]

$array->unshift(-2,-1,0); # [-2,-1,0,1,2,3,4,5]

values

values() : 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.

values example
# given [1..5]

$array->values; # [1,2,3,4,5]

CREDITS

Al Newkirk, +319

Anthony Brummett, +10

Adam Hopkins, +2

José Joaquín Atria, +1

AUTHOR

Al Newkirk, awncorp@cpan.org

LICENSE

Copyright (C) 2011-2019, Al Newkirk, et al.

This is free software; you can redistribute it and/or modify it under the terms of the The Apache License, Version 2.0, as elucidated here, https://github.com/iamalnewkirk/do/blob/master/LICENSE.

PROJECT

Wiki

Project

Initiatives

Milestones

Contributing

Issues

SEE ALSO

To get the most out of this distribution, consider reading the following:

Do

Data::Object

Data::Object::Class

Data::Object::ClassHas

Data::Object::Role

Data::Object::RoleHas

Data::Object::Library