| Filename | /Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Data/Perl/Role/Collection/Array.pm |
| Statements | Executed 35 statements in 1.68ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 44µs | 183µs | Data::Perl::Role::Collection::Array::BEGIN@5 |
| 17 | 1 | 1 | 31µs | 31µs | Data::Perl::Role::Collection::Array::count |
| 1 | 1 | 1 | 15µs | 51µs | Data::Perl::Role::Collection::Array::BEGIN@8 |
| 1 | 1 | 1 | 13µs | 246µs | Data::Perl::Role::Collection::Array::BEGIN@7 |
| 1 | 1 | 1 | 11µs | 356µs | Data::Perl::Role::Collection::Array::BEGIN@9 |
| 1 | 1 | 1 | 7µs | 30µs | Data::Perl::Role::Collection::Array::BEGIN@10 |
| 1 | 1 | 1 | 7µs | 20µs | Data::Perl::Role::Collection::Array::BEGIN@24 |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::__ANON__[:47] |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::_flatten_deep |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::accessor |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::all |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::clear |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::delete |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::first |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::first_index |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::flatten_deep |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::get |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::grep |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::head |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::insert |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::is_empty |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::join |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::map |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::natatime |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::new |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::pop |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::print |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::push |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::reduce |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::reverse |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::set |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::shallow_clone |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::shift |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::shuffle |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::sort |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::sort_in_place |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::splice |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::tail |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::uniq |
| 0 | 0 | 0 | 0s | 0s | Data::Perl::Role::Collection::Array::unshift |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Data::Perl::Role::Collection::Array; | ||||
| 2 | 1 | 1µs | $Data::Perl::Role::Collection::Array::VERSION = '0.002011'; | ||
| 3 | # ABSTRACT: Wrapping class for Perl's built in array structure. | ||||
| 4 | |||||
| 5 | 3 | 46µs | 3 | 321µs | # spent 183µs (44+139) within Data::Perl::Role::Collection::Array::BEGIN@5 which was called:
# once (44µs+139µs) by Role::Tiny::_load_module at line 5 # spent 183µs making 1 call to Data::Perl::Role::Collection::Array::BEGIN@5
# spent 108µs making 1 call to strictures::import
# spent 30µs making 1 call to strictures::VERSION |
| 6 | |||||
| 7 | 2 | 43µs | 2 | 479µs | # spent 246µs (13+233) within Data::Perl::Role::Collection::Array::BEGIN@7 which was called:
# once (13µs+233µs) by Role::Tiny::_load_module at line 7 # spent 246µs making 1 call to Data::Perl::Role::Collection::Array::BEGIN@7
# spent 233µs making 1 call to Role::Tiny::import |
| 8 | 2 | 24µs | 2 | 67µs | # spent 51µs (15+36) within Data::Perl::Role::Collection::Array::BEGIN@8 which was called:
# once (15µs+36µs) by Role::Tiny::_load_module at line 8 # spent 51µs making 1 call to Data::Perl::Role::Collection::Array::BEGIN@8
# spent 16µs making 1 call to List::Util::import |
| 9 | 2 | 37µs | 2 | 701µs | # spent 356µs (11+345) within Data::Perl::Role::Collection::Array::BEGIN@9 which was called:
# once (11µs+345µs) by Role::Tiny::_load_module at line 9 # spent 356µs making 1 call to Data::Perl::Role::Collection::Array::BEGIN@9
# spent 345µs making 1 call to Exporter::Tiny::import |
| 10 | 2 | 122µs | 2 | 53µs | # spent 30µs (7+23) within Data::Perl::Role::Collection::Array::BEGIN@10 which was called:
# once (7µs+23µs) by Role::Tiny::_load_module at line 10 # spent 30µs making 1 call to Data::Perl::Role::Collection::Array::BEGIN@10
# spent 23µs making 1 call to Exporter::import |
| 11 | |||||
| 12 | sub new { | ||||
| 13 | my $cl = CORE::shift; bless([ @_ ], $cl) | ||||
| 14 | } | ||||
| 15 | |||||
| 16 | # find the package name if possible else default to __PACKAGE__ | ||||
| 17 | #sub _blessed { blessed($_[0]) || __PACKAGE__ } | ||||
| 18 | |||||
| 19 | 17 | 29µs | # spent 31µs within Data::Perl::Role::Collection::Array::count which was called 17 times, avg 2µs/call:
# 17 times (31µs+0s) by JSON::Schema::Modern::Document::has_errors at line 17 of (eval 306)[Sub/Quote.pm:3], avg 2µs/call | ||
| 20 | |||||
| 21 | sub is_empty { CORE::scalar @{$_[0]} ? 0 : 1 } | ||||
| 22 | |||||
| 23 | { | ||||
| 24 | 3 | 1.35ms | 2 | 33µs | # spent 20µs (7+13) within Data::Perl::Role::Collection::Array::BEGIN@24 which was called:
# once (7µs+13µs) by Role::Tiny::_load_module at line 24 # spent 20µs making 1 call to Data::Perl::Role::Collection::Array::BEGIN@24
# spent 13µs making 1 call to warnings::unimport |
| 25 | sub all { @{$_[0]} } | ||||
| 26 | |||||
| 27 | 1 | 1µs | *elements = *all; | ||
| 28 | 1 | 27µs | *flatten = *all; | ||
| 29 | } | ||||
| 30 | |||||
| 31 | sub get { $_[0]->[ $_[1] ] } | ||||
| 32 | |||||
| 33 | sub pop { CORE::pop @{$_[0]} } | ||||
| 34 | |||||
| 35 | sub push { CORE::push @{$_[0]}, @_[1..$#_] } | ||||
| 36 | |||||
| 37 | sub shift { CORE::shift @{$_[0]} } | ||||
| 38 | |||||
| 39 | sub unshift { CORE::unshift @{$_[0]}, @_[1..$#_] } | ||||
| 40 | |||||
| 41 | sub clear { @{$_[0]} = () } | ||||
| 42 | |||||
| 43 | sub first { &List::Util::first($_[1], @{$_[0]}) } | ||||
| 44 | |||||
| 45 | sub first_index { &List::MoreUtils::first_index($_[1], @{$_[0]}) } | ||||
| 46 | |||||
| 47 | sub reduce { List::Util::reduce { $_[1]->($a, $b) } @{$_[0]} } | ||||
| 48 | |||||
| 49 | sub set { $_[0]->[ $_[1] ] = $_[2] } | ||||
| 50 | |||||
| 51 | sub accessor { | ||||
| 52 | if (@_ == 2) { | ||||
| 53 | $_[0]->[$_[1]]; | ||||
| 54 | } | ||||
| 55 | elsif (@_ > 2) { | ||||
| 56 | $_[0]->[$_[1]] = $_[2]; | ||||
| 57 | } | ||||
| 58 | } | ||||
| 59 | |||||
| 60 | sub natatime { | ||||
| 61 | my $iter = List::MoreUtils::natatime($_[1], @{$_[0]}); | ||||
| 62 | |||||
| 63 | if ($_[2]) { | ||||
| 64 | while (my @vals = $iter->()) { | ||||
| 65 | $_[2]->(@vals); | ||||
| 66 | } | ||||
| 67 | } | ||||
| 68 | else { | ||||
| 69 | $iter; | ||||
| 70 | } | ||||
| 71 | } | ||||
| 72 | |||||
| 73 | sub shallow_clone { blessed($_[0]) ? bless([@{$_[0]}], ref $_[0]) : [@{$_[0]}] } | ||||
| 74 | |||||
| 75 | # Data::Collection methods that return a Data::Perl::Collection::Array object | ||||
| 76 | #sub members { | ||||
| 77 | # my ($self) = @_; | ||||
| 78 | # qw/map grep member_count sort reverse print any all one none join/; | ||||
| 79 | #} | ||||
| 80 | |||||
| 81 | |||||
| 82 | sub map { | ||||
| 83 | my ($self, $cb) = @_; | ||||
| 84 | |||||
| 85 | my @res = CORE::map { $cb->($_) } @$self; | ||||
| 86 | |||||
| 87 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 88 | } | ||||
| 89 | |||||
| 90 | sub grep { | ||||
| 91 | my ($self, $cb) = @_; | ||||
| 92 | |||||
| 93 | my @res = CORE::grep { $cb->($_) } @$self; | ||||
| 94 | |||||
| 95 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 96 | } | ||||
| 97 | |||||
| 98 | sub sort { | ||||
| 99 | my ($self, $cb) = @_; | ||||
| 100 | |||||
| 101 | my @res = $cb ? CORE::sort { $cb->($a, $b) } @$self : CORE::sort @$self; | ||||
| 102 | |||||
| 103 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 104 | } | ||||
| 105 | |||||
| 106 | sub reverse { | ||||
| 107 | my ($self) = @_; | ||||
| 108 | |||||
| 109 | my @res = CORE::reverse @$self; | ||||
| 110 | |||||
| 111 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 112 | } | ||||
| 113 | |||||
| 114 | sub sort_in_place { | ||||
| 115 | @{$_[0]} = ($_[1] ? sort { $_[1]->($a, $b) } @{$_[0]} : sort @{$_[0]}); | ||||
| 116 | $_[0]; | ||||
| 117 | } | ||||
| 118 | |||||
| 119 | sub splice { | ||||
| 120 | my ($self) = @_; | ||||
| 121 | |||||
| 122 | my @res = CORE::splice @{$_[0]}, $_[1], $_[2], @_[3..$#_]; | ||||
| 123 | |||||
| 124 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 125 | } | ||||
| 126 | |||||
| 127 | sub shuffle { | ||||
| 128 | my ($self) = @_; | ||||
| 129 | |||||
| 130 | my @res = List::Util::shuffle(@$self); | ||||
| 131 | |||||
| 132 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 133 | } | ||||
| 134 | |||||
| 135 | sub uniq { | ||||
| 136 | my ($self) = @_; | ||||
| 137 | |||||
| 138 | my @res = List::MoreUtils::uniq(@$self); | ||||
| 139 | |||||
| 140 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 141 | } | ||||
| 142 | |||||
| 143 | sub delete { | ||||
| 144 | my ($self, $idx) = @_; | ||||
| 145 | |||||
| 146 | my ($res) = CORE::splice(@$self, $idx, 1); | ||||
| 147 | |||||
| 148 | $res; | ||||
| 149 | } | ||||
| 150 | |||||
| 151 | sub insert { | ||||
| 152 | my ($self, $idx, $el) = @_; | ||||
| 153 | |||||
| 154 | my ($res) = CORE::splice(@$self, $idx, 0, $el); | ||||
| 155 | |||||
| 156 | $res; | ||||
| 157 | } | ||||
| 158 | |||||
| 159 | sub flatten_deep { | ||||
| 160 | my ($self, $depth) = @_; | ||||
| 161 | |||||
| 162 | _flatten_deep(@$self, $depth); | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | sub _flatten_deep { | ||||
| 166 | my @array = @_; | ||||
| 167 | my $depth = CORE::pop @array; | ||||
| 168 | --$depth if (defined($depth)); | ||||
| 169 | |||||
| 170 | my @elements = CORE::map { | ||||
| 171 | (ref eq 'ARRAY') | ||||
| 172 | ? (defined($depth) && $depth == -1) ? $_ : _flatten_deep( @$_, $depth ) | ||||
| 173 | : $_ | ||||
| 174 | } @array; | ||||
| 175 | } | ||||
| 176 | |||||
| 177 | sub join { | ||||
| 178 | my ($self, $with) = @_; | ||||
| 179 | |||||
| 180 | CORE::join((defined $with ? $with : ','), @$self); | ||||
| 181 | } | ||||
| 182 | |||||
| 183 | sub print { | ||||
| 184 | my ($self, $fh, $arg) = @_; | ||||
| 185 | |||||
| 186 | print { $fh || *STDOUT } CORE::join((defined $arg ? $arg : ','), @$self); | ||||
| 187 | } | ||||
| 188 | |||||
| 189 | sub head { | ||||
| 190 | my ($self, $count) = @_; | ||||
| 191 | |||||
| 192 | $count = $self->count if $count > $self->count; | ||||
| 193 | $count = $self->count - -$count if $count < 0; | ||||
| 194 | |||||
| 195 | my @res = ($self->elements)[0 .. $count - 1]; | ||||
| 196 | |||||
| 197 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 198 | } | ||||
| 199 | |||||
| 200 | sub tail { | ||||
| 201 | my ($self, $count) = @_; | ||||
| 202 | |||||
| 203 | $count = $self->count if $count > $self->count; | ||||
| 204 | $count = $self->count - -$count if $count < 0; | ||||
| 205 | my $start = $self->count - $count; | ||||
| 206 | |||||
| 207 | my @res = ($self->elements)[$start .. $self->count - 1]; | ||||
| 208 | |||||
| 209 | blessed($self) ? blessed($self)->new(@res) : @res; | ||||
| 210 | } | ||||
| 211 | |||||
| 212 | 1 | 4µs | 1; | ||
| 213 | |||||
| 214 | =pod | ||||
| 215 | |||||
| 216 | =encoding UTF-8 | ||||
| 217 | |||||
| 218 | =head1 NAME | ||||
| 219 | |||||
| 220 | Data::Perl::Role::Collection::Array - Wrapping class for Perl's built in array structure. | ||||
| 221 | |||||
| 222 | =head1 VERSION | ||||
| 223 | |||||
| 224 | version 0.002011 | ||||
| 225 | |||||
| 226 | =head1 SYNOPSIS | ||||
| 227 | |||||
| 228 | use Data::Perl qw/array/; | ||||
| 229 | |||||
| 230 | my $array = array(1, 2, 3); | ||||
| 231 | |||||
| 232 | $array->push(5); | ||||
| 233 | |||||
| 234 | $array->grep(sub { $_ > 2 })->map(sub { $_ ** 2 })->elements; # (3, 5); | ||||
| 235 | |||||
| 236 | =head1 DESCRIPTION | ||||
| 237 | |||||
| 238 | This class provides a wrapper and methods for interacting with an array. | ||||
| 239 | All methods that return a list do so via a Data::Perl::Collection::Array object. | ||||
| 240 | |||||
| 241 | =head1 PROVIDED METHODS | ||||
| 242 | |||||
| 243 | =over 4 | ||||
| 244 | |||||
| 245 | =item B<new($value, $value, ....)> | ||||
| 246 | |||||
| 247 | Constructs a new Data::Perl::Collection::Array object initialized with passed | ||||
| 248 | in values, and returns it. | ||||
| 249 | |||||
| 250 | =item B<count> | ||||
| 251 | |||||
| 252 | Returns the number of elements in the array. | ||||
| 253 | |||||
| 254 | $stuff = Data::Perl::Collection::Array->new(qw/foo bar baz boo/); | ||||
| 255 | |||||
| 256 | print $stuff->count; # prints 4 | ||||
| 257 | |||||
| 258 | This method does not accept any arguments. | ||||
| 259 | |||||
| 260 | =item B<is_empty> | ||||
| 261 | |||||
| 262 | Returns a boolean value that is true when the array has no elements. | ||||
| 263 | |||||
| 264 | $stuff->is_empty ? die "No options!\n" : print "Good boy.\n"; | ||||
| 265 | |||||
| 266 | This method does not accept any arguments. | ||||
| 267 | |||||
| 268 | =item B<elements/all> | ||||
| 269 | |||||
| 270 | Returns all of the elements of the array as an array (not an array reference). | ||||
| 271 | |||||
| 272 | my @options = $stuff->elements; | ||||
| 273 | print "@options\n"; # prints "foo bar baz boo" | ||||
| 274 | |||||
| 275 | This method does not accept any arguments. | ||||
| 276 | |||||
| 277 | =item B<get($index)> | ||||
| 278 | |||||
| 279 | Returns an element of the array by its index. You can also use negative index | ||||
| 280 | numbers, just as with Perl's core array handling. | ||||
| 281 | |||||
| 282 | my $option = $stuff->get(1); | ||||
| 283 | print "$option\n"; # prints "bar" | ||||
| 284 | |||||
| 285 | If the specified element does not exist, this will return C<undef>. | ||||
| 286 | |||||
| 287 | This method accepts just one argument. | ||||
| 288 | |||||
| 289 | =item B<pop> | ||||
| 290 | |||||
| 291 | Just like Perl's builtin C<pop>. | ||||
| 292 | |||||
| 293 | This method does not accept any arguments. | ||||
| 294 | |||||
| 295 | =item B<push($value1, $value2, value3 ...)> | ||||
| 296 | |||||
| 297 | Just like Perl's builtin C<push>. Returns the number of elements in the new | ||||
| 298 | array. | ||||
| 299 | |||||
| 300 | This method accepts any number of arguments. | ||||
| 301 | |||||
| 302 | =item B<shift> | ||||
| 303 | |||||
| 304 | Just like Perl's builtin C<shift>. | ||||
| 305 | |||||
| 306 | This method does not accept any arguments. | ||||
| 307 | |||||
| 308 | =item B<unshift($value1, $value2, value3 ...)> | ||||
| 309 | |||||
| 310 | Just like Perl's builtin C<unshift>. Returns the number of elements in the new | ||||
| 311 | array. | ||||
| 312 | |||||
| 313 | This method accepts any number of arguments. | ||||
| 314 | |||||
| 315 | =item B<splice($offset, $length, @values)> | ||||
| 316 | |||||
| 317 | Just like Perl's builtin C<splice>. In scalar context, this returns the last | ||||
| 318 | element removed, or C<undef> if no elements were removed. In list context, this | ||||
| 319 | returns all the elements removed from the array, wrapped in a Collection::Array | ||||
| 320 | object. | ||||
| 321 | |||||
| 322 | This method requires at least one argument. | ||||
| 323 | |||||
| 324 | =item B<first( sub { ... } )> | ||||
| 325 | |||||
| 326 | This method returns the first matching item in the array, just like | ||||
| 327 | L<List::Util>'s C<first> function. The matching is done with a subroutine | ||||
| 328 | reference you pass to this method. The subroutine will be called against each | ||||
| 329 | element in the array until one matches or all elements have been checked. | ||||
| 330 | |||||
| 331 | my $found = $stuff->find_option( sub {/^b/} ); | ||||
| 332 | print "$found\n"; # prints "bar" | ||||
| 333 | |||||
| 334 | This method requires a single argument. | ||||
| 335 | |||||
| 336 | =item B<first_index( sub { ... } )> | ||||
| 337 | |||||
| 338 | This method returns the index of the first matching item in the array, just | ||||
| 339 | like L<List::MoreUtils>'s C<first_index> function. The matching is done with a | ||||
| 340 | subroutine reference you pass to this method. The subroutine will be called | ||||
| 341 | against each element in the array until one matches or all elements have been | ||||
| 342 | checked. | ||||
| 343 | |||||
| 344 | This method requires a single argument. | ||||
| 345 | |||||
| 346 | =item B<grep( sub { ... } )> | ||||
| 347 | |||||
| 348 | This method returns every element matching a given criteria, just like Perl's | ||||
| 349 | core C<grep> function. This method requires a subroutine which implements the | ||||
| 350 | matching logic. The returned list is provided as a Collection::Array object. | ||||
| 351 | |||||
| 352 | my @found = $stuff->grep( sub {/^b/} ); | ||||
| 353 | print "@found\n"; # prints "bar baz boo" | ||||
| 354 | |||||
| 355 | This method requires a single argument. | ||||
| 356 | |||||
| 357 | =item B<map( sub { ... } )> | ||||
| 358 | |||||
| 359 | This method transforms every element in the array and returns a new array, just | ||||
| 360 | like Perl's core C<map> function. This method requires a subroutine which | ||||
| 361 | implements the transformation. The returned list is provided as | ||||
| 362 | a Collection::Array object. | ||||
| 363 | |||||
| 364 | my @mod_options = $stuff->map( sub { $_ . "-tag" } ); | ||||
| 365 | print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" | ||||
| 366 | |||||
| 367 | This method requires a single argument. | ||||
| 368 | |||||
| 369 | =item B<reduce( sub { ... } )> | ||||
| 370 | |||||
| 371 | This method turns an array into a single value, by passing a function the | ||||
| 372 | value so far and the next value in the array, just like L<List::Util>'s | ||||
| 373 | C<reduce> function. The reducing is done with a subroutine reference you pass | ||||
| 374 | to this method. | ||||
| 375 | |||||
| 376 | my $found = $stuff->reduce( sub { $_[0] . $_[1] } ); | ||||
| 377 | print "$found\n"; # prints "foobarbazboo" | ||||
| 378 | |||||
| 379 | This method requires a single argument. | ||||
| 380 | |||||
| 381 | =item B<sort> | ||||
| 382 | |||||
| 383 | =item B<sort( sub { ... } )> | ||||
| 384 | |||||
| 385 | Returns the elements of the array in sorted order. | ||||
| 386 | |||||
| 387 | You can provide an optional subroutine reference to sort with (as you can with | ||||
| 388 | Perl's core C<sort> function). However, instead of using C<$a> and C<$b> in | ||||
| 389 | this subroutine, you will need to use C<$_[0]> and C<$_[1]>. The returned list | ||||
| 390 | is provided as a Collection::Array object. | ||||
| 391 | |||||
| 392 | # ascending ASCIIbetical | ||||
| 393 | my @sorted = $stuff->sort(); | ||||
| 394 | |||||
| 395 | # Descending alphabetical order | ||||
| 396 | my @sorted_options = $stuff->sort( sub { lc $_[1] cmp lc $_[0] } ); | ||||
| 397 | print "@sorted_options\n"; # prints "foo boo baz bar" | ||||
| 398 | |||||
| 399 | This method accepts a single argument. | ||||
| 400 | |||||
| 401 | =item B<sort_in_place> | ||||
| 402 | |||||
| 403 | =item B<sort_in_place( sub { ... } )> | ||||
| 404 | |||||
| 405 | Sorts the array I<in place>, modifying the value of the attribute. | ||||
| 406 | |||||
| 407 | You can provide an optional subroutine reference to sort with (as you can with | ||||
| 408 | Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you | ||||
| 409 | will need to use C<$_[0]> and C<$_[1]> instead. The returned list is provided | ||||
| 410 | as a Collection::Array object. | ||||
| 411 | |||||
| 412 | This method accepts a single argument. | ||||
| 413 | |||||
| 414 | =item B<reverse> | ||||
| 415 | |||||
| 416 | Returns the elements of the array in reversed order. The returned list is | ||||
| 417 | provided as a Collection::Array object. | ||||
| 418 | |||||
| 419 | This method does not accept any arguments. | ||||
| 420 | |||||
| 421 | =item B<shuffle> | ||||
| 422 | |||||
| 423 | Returns the elements of the array in random order, like C<shuffle> from | ||||
| 424 | L<List::Util>. The returned list is provided as a Collection::Array object. | ||||
| 425 | |||||
| 426 | This method does not accept any arguments. | ||||
| 427 | |||||
| 428 | =item B<uniq> | ||||
| 429 | |||||
| 430 | Returns the array with all duplicate elements removed, like C<uniq> from | ||||
| 431 | L<List::MoreUtils>. The returned list is provided as a Collection::Array object. | ||||
| 432 | |||||
| 433 | This method does not accept any arguments. | ||||
| 434 | |||||
| 435 | =item B<head($count)> | ||||
| 436 | |||||
| 437 | Returns the first C<$count> elements of the array. If C<$count> is greater | ||||
| 438 | than the number of elements in the array, the array (without spurious C<undef>s) | ||||
| 439 | is returned. Negative C<$count> means "all but the last C<$count> elements". The | ||||
| 440 | returned list is provided as a Collection::Array object. | ||||
| 441 | |||||
| 442 | =item B<tail($count)> | ||||
| 443 | |||||
| 444 | Returns the last C<$count> elements of the array. If C<$count> is greater | ||||
| 445 | than the number of elements in the array, the array (without spurious C<undef>s) | ||||
| 446 | is returned. Negative C<$count> means "all but the first C<$count> elements". The | ||||
| 447 | returned list is provided as a Collection::Array object. | ||||
| 448 | |||||
| 449 | =item B<join($str)> | ||||
| 450 | |||||
| 451 | Joins every element of the array using the separator given as argument, just | ||||
| 452 | like Perl's core C<join> function. | ||||
| 453 | |||||
| 454 | my $joined = $stuff->join(':'); | ||||
| 455 | print "$joined\n"; # prints "foo:bar:baz:boo" | ||||
| 456 | |||||
| 457 | This method requires a single argument. | ||||
| 458 | |||||
| 459 | =item B<print($handle, $str)> | ||||
| 460 | |||||
| 461 | Prints the output of join($str) to $handle. $handle defaults to STDOUT, and | ||||
| 462 | join $str defaults to join()'s default of ','. | ||||
| 463 | |||||
| 464 | $joined = $stuff->print(*STDERR, ';'); # prints foo;bar;baz to STDERR | ||||
| 465 | |||||
| 466 | =item B<set($index, $value)> | ||||
| 467 | |||||
| 468 | Given an index and a value, sets the specified array element's value. | ||||
| 469 | |||||
| 470 | This method returns the value at C<$index> after the set. | ||||
| 471 | |||||
| 472 | This method requires two arguments. | ||||
| 473 | |||||
| 474 | =item B<delete($index)> | ||||
| 475 | |||||
| 476 | Removes the element at the given index from the array. | ||||
| 477 | |||||
| 478 | This method returns the deleted value, either as an array or scalar as | ||||
| 479 | dependent on splice context semantics. Note that if no value exists, it will | ||||
| 480 | |||||
| 481 | return C<undef>. | ||||
| 482 | |||||
| 483 | This method requires one argument. | ||||
| 484 | |||||
| 485 | =item B<insert($index, $value)> | ||||
| 486 | |||||
| 487 | Inserts a new element into the array at the given index. | ||||
| 488 | |||||
| 489 | This method returns the new value at C<$index>, either as an array or scalar as | ||||
| 490 | dependent on splice context semantics. | ||||
| 491 | |||||
| 492 | This method requires two arguments. | ||||
| 493 | |||||
| 494 | =item B<clear> | ||||
| 495 | |||||
| 496 | Empties the entire array, like C<@array = ()>. | ||||
| 497 | |||||
| 498 | This method does not define a return value. | ||||
| 499 | |||||
| 500 | This method does not accept any arguments. | ||||
| 501 | |||||
| 502 | =item B<accessor($index)> | ||||
| 503 | |||||
| 504 | =item B<accessor($index, $value)> | ||||
| 505 | |||||
| 506 | This method provides a get/set accessor for the array, based on array indexes. | ||||
| 507 | If passed one argument, it returns the value at the specified index. If | ||||
| 508 | passed two arguments, it sets the value of the specified index. | ||||
| 509 | |||||
| 510 | When called as a setter, this method returns the new value at C<$index>. | ||||
| 511 | |||||
| 512 | This method accepts one or two arguments. | ||||
| 513 | |||||
| 514 | =item B<natatime($n)> | ||||
| 515 | |||||
| 516 | =item B<natatime($n, $code)> | ||||
| 517 | |||||
| 518 | This method returns an iterator which, on each call, returns C<$n> more items | ||||
| 519 | from the array, in order, like C<natatime> from L<List::MoreUtils>. A coderef | ||||
| 520 | can optionally be provided; it will be called on each group of C<$n> elements | ||||
| 521 | in the array. | ||||
| 522 | |||||
| 523 | This method accepts one or two arguments. | ||||
| 524 | |||||
| 525 | =item B<shallow_clone> | ||||
| 526 | |||||
| 527 | This method returns a shallow clone of the array reference. The return value | ||||
| 528 | is a reference to a new array with the same elements. It is I<shallow> | ||||
| 529 | because any elements that were references in the original will be the I<same> | ||||
| 530 | references in the clone. | ||||
| 531 | |||||
| 532 | =item B<flatten> | ||||
| 533 | |||||
| 534 | This method returns a list of elements in the array. This method is an alias | ||||
| 535 | to the I<elements> method. | ||||
| 536 | |||||
| 537 | =item B<flatten_deep($level)> | ||||
| 538 | |||||
| 539 | This method returns a flattened list of elements in the array. Will flatten | ||||
| 540 | arrays contained within the root array recursively - depth is controlled by the | ||||
| 541 | optional $level parameter. | ||||
| 542 | |||||
| 543 | =back | ||||
| 544 | |||||
| 545 | =head1 SEE ALSO | ||||
| 546 | |||||
| 547 | =over 4 | ||||
| 548 | |||||
| 549 | =item * L<Data::Perl> | ||||
| 550 | |||||
| 551 | =item * L<MooX::HandlesVia> | ||||
| 552 | |||||
| 553 | =back | ||||
| 554 | |||||
| 555 | =head1 AUTHOR | ||||
| 556 | |||||
| 557 | Matthew Phillips <mattp@cpan.org> | ||||
| 558 | |||||
| 559 | =head1 COPYRIGHT AND LICENSE | ||||
| 560 | |||||
| 561 | This software is copyright (c) 2020 by Matthew Phillips <mattp@cpan.org>. | ||||
| 562 | |||||
| 563 | This is free software; you can redistribute it and/or modify it under | ||||
| 564 | the same terms as the Perl 5 programming language system itself. | ||||
| 565 | |||||
| 566 | =cut | ||||
| 567 | |||||
| 568 | __END__ |