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

Hydrogen::ArrayRef - a standard library for arrayrefs

VERSION

This documentation is for Hydrogen::ArrayRef 0.018, which is based on Sub::HandlesVia::HandlerLibrary::Array 0.046.

FUNCTIONS

Each function expects a reference to an array as its first argument.

accessor( $arrayref, $index, $value? )

Additional arguments: Int, Optional[Any].

Acts like get if given just an index, or set if given an index and value.

all( $arrayref )

All elements in the array, in list context.

all_true( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::all().

any( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::any().

apply( $arrayref, $coderef )

Additional arguments: CodeRef.

Executes the coderef (which should modify $_) against each element of the array; returns the resulting array in list context.

clear( $arrayref )

Empties the array.

count( $arrayref )

The number of elements in the referenced array.

delete( $arrayref, $index )

Additional arguments: Int.

Removes the indexed element from the array and returns it. Elements after it will be "moved up".

elements( $arrayref )

All elements in the array, in list context. (Essentially the same as all.)

first( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::first().

first_index( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::MoreUtils::first_index().

flatten( $arrayref )

All elements in the array, in list context. (Essentially the same as all.)

flatten_deep( $arrayref, $depth? )

Additional arguments: Optional[Int].

Flattens the arrayref into a list, including any nested arrayrefs. (Has the potential to loop infinitely.)

for_each( $arrayref, $coderef )

Additional arguments: CodeRef.

Function which executes the coderef on each element of the array. The coderef will be passed two values: the element and its index.

for_each_pair( $arrayref, $coderef )

Additional arguments: CodeRef.

Function which executes the coderef on each pair of elements in the array. The coderef will be passed the two elements.

get( $arrayref, $index )

Additional arguments: Int.

Returns a single element from the array by index.

grep( $arrayref, $coderef )

Additional arguments: CodeRef.

Like grep from perlfunc.

head( $arrayref, $count )

Additional arguments: Int.

Returns the first $count elements of the array in list context.

insert( $arrayref, $index, $value )

Additional arguments: Int, Any.

Inserts a value into the array with the given index. Elements after it will be "moved down".

is_empty( $arrayref )

Boolean indicating if the referenced array is empty.

join( $arrayref, $with? )

Additional arguments: Optional[Str].

Returns a string joining all the elements in the array; if $with is omitted, defaults to a comma.

map( $arrayref, $coderef )

Additional arguments: CodeRef.

Like map from perlfunc.

max( $arrayref )

Like List::Util::max().

maxstr( $arrayref )

Like List::Util::maxstr().

min( $arrayref )

Like List::Util::min().

minstr( $arrayref )

Like List::Util::minstr().

natatime( $arrayref, $n, $callback? )

Additional arguments: Int, Optional[CodeRef].

Given just a number, returns an iterator which reads that many elements from the array at a time. If also given a callback, calls the callback repeatedly with those values.

not_all_true( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::notall().

pairfirst( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::pairfirst().

pairgrep( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::pairgrep().

pairkeys( $arrayref )

Like List::Util::pairkeys().

pairmap( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::pairmap().

pairs( $arrayref )

Like List::Util::pairs().

pairvalues( $arrayref )

Like List::Util::pairvalues().

pick_random( $arrayref, $count )

Additional arguments: Optional[Int].

If no $count is given, returns one element of the array at random. If $count is given, creates a new array with that many random elements from the original array (or fewer if the original array is not long enough) and returns that as an arrayref or list depending on context

pop( $arrayref )

Removes the last element from the array and returns it.

print( $arrayref, $fh?, $with? )

Additional arguments: Optional[FileHandle], Optional[Str].

Prints a string joining all the elements in the array; if $fh is omitted, defaults to STDOUT; if $with is omitted, defaults to a comma.

product( $arrayref )

Like List::Util::product().

push( $arrayref, @values )

Adds elements to the end of the array.

reduce( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::reduce().

reductions( $arrayref, $coderef )

Additional arguments: CodeRef.

Like List::Util::reductions().

reset( $arrayref )

Resets the original value to its default value, or an empty arrayref if it has no default.

reverse( $arrayref )

Returns the reversed array in list context.

sample( $arrayref, $count )

Additional arguments: Int.

Like List::Util::sample().

set( $arrayref, $index, $value )

Additional arguments: Int, Any.

Sets the element with the given index to the supplied value.

shallow_clone( $arrayref )

Creates a new arrayref with the same elements as the original.

shift( $arrayref )

Removes an element from the start of the array and returns it.

shuffle( $arrayref )

Returns the array in a random order; can be called in list context or scalar context and will return an arrayref in the latter case.

shuffle_in_place( $arrayref )

Rearranges the array in a random order, and changes the original value to point to the new order.

sort( $arrayref, $coderef? )

Additional arguments: Optional[CodeRef].

Like sort from perlfunc.

sort_in_place( $arrayref, $coderef? )

Additional arguments: Optional[CodeRef].

Like sort from perlfunc, but changes the original value to point to the newly sorted array.

splice( $arrayref, $index, $length, @values )

Like splice from perlfunc.

sum( $arrayref )

Like List::Util::sum0().

tail( $arrayref, $count )

Additional arguments: Int.

Returns the last $count elements of the array in list context.

uniq( $arrayref )

Returns the array filtered to remove duplicates; can be called in list context or scalar context and will return an arrayref in the latter case.

uniq_in_place( $arrayref )

Filters the array to remove duplicates, and changes the original value to point to the filtered array.

uniqnum( $arrayref )

Returns the array filtered to remove duplicates numerically; can be called in list context or scalar context and will return an arrayref in the latter case.

uniqnum_in_place( $arrayref )

Filters the array to remove duplicates numerically, and changes the original value to point to the filtered array.

uniqstr( $arrayref )

Returns the array filtered to remove duplicates stringwise; can be called in list context or scalar context and will return an arrayref in the latter case.

uniqstr_in_place( $arrayref )

Filters the array to remove duplicates stringwise, and changes the original value to point to the filtered array.

unshift( $arrayref, @values )

Adds an element to the start of the array.

EXPORT

No functions are exported by this module by default. To import them all (this is usually a bad idea), use:

    use Hydrogen::ArrayRef -all;

To import a particular function, use:

    use Hydrogen::ArrayRef 'all_true';

To rename functions:

    use Hydrogen::ArrayRef 'all_true' => { -as => 'myfunc' };

On Perl 5.37.2+, you can import lexically:

    use Hydrogen::ArrayRef -lexical, 'all_true';

See Exporter::Tiny::Manual::Importing for more hints on importing.

BUGS

Please report any bugs to http://github.com/tobyink/p5-hydrogen/issues.

SEE ALSO

Hydrogen, Hydrogen::Array, Hydrogen::Curry::ArrayRef, Hydrogen::Topic::ArrayRef, Sub::HandlesVia::HandlerLibrary::Array, List::Util, List::MoreUtils.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.