File Coverage

blib/lib/Data/Perl/Role/Collection/Array.pm
Criterion Covered Total %
statement 104 106 98.1
branch 33 40 82.5
condition 3 3 100.0
subroutine 37 38 97.3
pod 30 30 100.0
total 207 217 95.3


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