| 1 | package autobox::List::Util; | ||||||||||
| 2 | |||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 9 | ▶ | use base 'autobox'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 10 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 10 | my $class = shift; | |||||||||
| 10 | 10 | $class->SUPER::import(ARRAY => 'autobox::List::Util::_private'); | |||||||||
| 11 | } | ||||||||||
| 12 | |||||||||||
| 13 | package autobox::List::Util::_private; | ||||||||||
| 14 | |||||||||||
| 15 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 9 | ▶ | use Module::Load; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 14 | ▶ | sub first { | ||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 14 | load List::Util; | |||||||||
| 21 | 14 | my ($self, $coderef) = @_; | |||||||||
| 22 | 14 | ▶ | return List::Util::first { $coderef->() } @$self | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 4 | ▶ | sub max { | ||||||||
|
Subroutine
max: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 4 | load List::Util; | |||||||||
| 27 | 4 | my $self = shift; | |||||||||
| 28 | 4 | return List::Util::max @$self | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 4 | ▶ | sub maxstr { | ||||||||
|
Subroutine
maxstr: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 4 | load List::Util; | |||||||||
| 33 | 4 | my $self = shift; | |||||||||
| 34 | 4 | return List::Util::maxstr @$self | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 5 | ▶ | sub min { | ||||||||
|
Subroutine
min: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 5 | load List::Util; | |||||||||
| 39 | 5 | my $self = shift; | |||||||||
| 40 | 5 | return List::Util::min @$self | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 4 | ▶ | sub minstr { | ||||||||
|
Subroutine
minstr: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 4 | load List::Util; | |||||||||
| 45 | 4 | my $self = shift; | |||||||||
| 46 | 4 | return List::Util::minstr @$self | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 18 | ▶ | sub reduce { | ||||||||
|
Subroutine
reduce: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 18 | load List::Util; | |||||||||
| 51 | 18 | my ($self, $coderef) = @_; | |||||||||
| 52 | 70 | ▶ | return List::Util::reduce { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 53 | #FIXME: this needs to know the package we are exporting to | ||||||||||
| 54 | 70 | local ($main::a, $main::b) = ($a, $b); | |||||||||
| 55 | 70 | $coderef->(); | |||||||||
| 56 | 18 | } @$self | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 7 | ▶ | sub shuffle { | ||||||||
|
Subroutine
shuffle: called CC=2 SCAR=6.9
| |||||||||||
| 60 | 7 | load List::Util; | |||||||||
| 61 | 7 | my $self = shift; | |||||||||
| 62 | 7 | ▶ | return List::Util::shuffle @$self if wantarray; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 4 | return [ List::Util::shuffle @$self ]; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 6 | ▶ | sub sum { | ||||||||
|
Subroutine
sum: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 6 | load List::Util; | |||||||||
| 68 | 6 | my $self = shift; | |||||||||
| 69 | 6 | return List::Util::sum @$self | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | package autobox::List::Util; | ||||||||||
| 73 | |||||||||||
| 74 | =head1 NAME | ||||||||||
| 75 | |||||||||||
| 76 | autobox::List::Util - bring the List::Util functions to autobox | ||||||||||
| 77 | |||||||||||
| 78 | =head1 VERSION | ||||||||||
| 79 | |||||||||||
| 80 | Version 20090629 | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 9 | our $VERSION = '20090629'; | |||||||||
| 85 | |||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||
| 87 | |||||||||||
| 88 | C<autobox::List::Util> brings all of the functions from List::Util | ||||||||||
| 89 | to arrays as methods. | ||||||||||
| 90 | |||||||||||
| 91 | use autobox::List::Util; | ||||||||||
| 92 | |||||||||||
| 93 | my @array = qw/ foo bar baz /; | ||||||||||
| 94 | |||||||||||
| 95 | print @array->first(sub { /ar/ }), "\n"; # "bar" | ||||||||||
| 96 | |||||||||||
| 97 | print [5, 6, 3, 4]->max, "\n"; # 6 | ||||||||||
| 98 | |||||||||||
| 99 | print @array->maxstr, "\n"; # baz | ||||||||||
| 100 | |||||||||||
| 101 | print [5, 6, 3, 4]->min, "\n"; # 3 | ||||||||||
| 102 | |||||||||||
| 103 | print @array->minstr, "\n"; # foo | ||||||||||
| 104 | |||||||||||
| 105 | print [1 .. 10]->shuffle, "\n"; #1 to 10 randomly shuffled | ||||||||||
| 106 | |||||||||||
| 107 | print [1 .. 10]->sum, "\n"; # 55 | ||||||||||
| 108 | |||||||||||
| 109 | print [1 .. 10]->reduce( sub { $a + $b } ), "\n"; # 55 | ||||||||||
| 110 | |||||||||||
| 111 | =head1 METHODS | ||||||||||
| 112 | |||||||||||
| 113 | =head2 first(coderef) | ||||||||||
| 114 | |||||||||||
| 115 | This method behaves nearly the same as the first function from List::Util, | ||||||||||
| 116 | but it takes a coderef not a block because methods can't use prototypes. | ||||||||||
| 117 | |||||||||||
| 118 | =head2 reduce(coderef) | ||||||||||
| 119 | |||||||||||
| 120 | This method behaves nearly the same as the reduce function from List::Util, | ||||||||||
| 121 | but it takes a coderef not a block for the same reason. It also has a bug | ||||||||||
| 122 | (see L<BUGS>) | ||||||||||
| 123 | |||||||||||
| 124 | =head2 shuffle | ||||||||||
| 125 | |||||||||||
| 126 | If called in scalar context it returns a reference to an array instead | ||||||||||
| 127 | of a list. This allows shuffle to be chained with other calls. | ||||||||||
| 128 | |||||||||||
| 129 | =head2 max, maxstr, min, minstr, sum | ||||||||||
| 130 | |||||||||||
| 131 | These methods behave exactly the same as their List::Util counterparts. | ||||||||||
| 132 | |||||||||||
| 133 | =head1 AUTHOR | ||||||||||
| 134 | |||||||||||
| 135 | Chas. J. Owens IV, C<< <chas.owens at gmail.com> >> | ||||||||||
| 136 | |||||||||||
| 137 | =head1 BUGS | ||||||||||
| 138 | |||||||||||
| 139 | The reduce method works with $main::a and $main::b, not your current | ||||||||||
| 140 | package's $a and $b, so you need to say | ||||||||||
| 141 | |||||||||||
| 142 | print @array->reduce( sub { $main::a + $main::b } ), "\n"; | ||||||||||
| 143 | |||||||||||
| 144 | if you are not in the main package. Reduce uses $_, so it doesn't | ||||||||||
| 145 | suffer from this problem. | ||||||||||
| 146 | |||||||||||
| 147 | =head1 SUPPORT | ||||||||||
| 148 | |||||||||||
| 149 | You can find documentation for this module with the perldoc command. | ||||||||||
| 150 | |||||||||||
| 151 | perldoc autobox::List::Util | ||||||||||
| 152 | |||||||||||
| 153 | |||||||||||
| 154 | You can also look for information at: | ||||||||||
| 155 | |||||||||||
| 156 | =over 4 | ||||||||||
| 157 | |||||||||||
| 158 | =item * RT: CPAN's request tracker | ||||||||||
| 159 | |||||||||||
| 160 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=autobox-List-Util> | ||||||||||
| 161 | |||||||||||
| 162 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 163 | |||||||||||
| 164 | L<http://annocpan.org/dist/autobox-List-Util> | ||||||||||
| 165 | |||||||||||
| 166 | =item * CPAN Ratings | ||||||||||
| 167 | |||||||||||
| 168 | L<http://cpanratings.perl.org/d/autobox-List-Util> | ||||||||||
| 169 | |||||||||||
| 170 | =item * Search CPAN | ||||||||||
| 171 | |||||||||||
| 172 | L<http://search.cpan.org/dist/autobox-List-Util/> | ||||||||||
| 173 | |||||||||||
| 174 | =back | ||||||||||
| 175 | |||||||||||
| 176 | |||||||||||
| 177 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 178 | |||||||||||
| 179 | |||||||||||
| 180 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 181 | |||||||||||
| 182 | Copyright 2009 Chas. J. Owens IV, all rights reserved. | ||||||||||
| 183 | |||||||||||
| 184 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 185 | under the same terms as Perl itself. | ||||||||||
| 186 | |||||||||||
| 187 | |||||||||||
| 188 | =cut | ||||||||||
| 189 | |||||||||||
| 190 | 9 | 1; # End of autobox::List::Util | |||||||||