Commit | Line | Data |
---|---|---|
f4a2945e JH |
1 | # List::Util.pm |
2 | # | |
2ff28616 | 3 | # Copyright (c) 1997-2009 Graham Barr <gbarr@pobox.com>. All rights reserved. |
f4a2945e JH |
4 | # This program is free software; you can redistribute it and/or |
5 | # modify it under the same terms as Perl itself. | |
2ff28616 GB |
6 | # |
7 | # This module is normally only loaded if the XS module is not available | |
f4a2945e JH |
8 | |
9 | package List::Util; | |
10 | ||
82f35e8b | 11 | use strict; |
f4a2945e JH |
12 | require Exporter; |
13 | ||
3630f57e | 14 | our @ISA = qw(Exporter); |
8b198969 | 15 | our @EXPORT_OK = qw(first min max minstr maxstr reduce sum sum0 shuffle); |
c9612cb4 | 16 | our $VERSION = "1.27"; |
3630f57e | 17 | our $XS_VERSION = $VERSION; |
60f3865b | 18 | $VERSION = eval $VERSION; |
f4a2945e | 19 | |
3630f57e CBW |
20 | require XSLoader; |
21 | XSLoader::load('List::Util', $XS_VERSION); | |
09c2a9b8 | 22 | |
8b198969 CBW |
23 | sub sum0 |
24 | { | |
25 | return 0 unless @_; | |
26 | goto ∑ | |
27 | } | |
28 | ||
f4a2945e JH |
29 | 1; |
30 | ||
31 | __END__ | |
32 | ||
33 | =head1 NAME | |
34 | ||
35 | List::Util - A selection of general-utility list subroutines | |
36 | ||
37 | =head1 SYNOPSIS | |
38 | ||
c29e891d | 39 | use List::Util qw(first max maxstr min minstr reduce shuffle sum); |
f4a2945e JH |
40 | |
41 | =head1 DESCRIPTION | |
42 | ||
43 | C<List::Util> contains a selection of subroutines that people have | |
44 | expressed would be nice to have in the perl core, but the usage would | |
45 | not really be high enough to warrant the use of a keyword, and the size | |
46 | so small such that being individual extensions would be wasteful. | |
47 | ||
48 | By default C<List::Util> does not export any subroutines. The | |
49 | subroutines defined are | |
50 | ||
51 | =over 4 | |
52 | ||
53 | =item first BLOCK LIST | |
54 | ||
55 | Similar to C<grep> in that it evaluates BLOCK setting C<$_> to each element | |
56 | of LIST in turn. C<first> returns the first element where the result from | |
57 | BLOCK is a true value. If BLOCK never returns true or LIST was empty then | |
58 | C<undef> is returned. | |
59 | ||
60 | $foo = first { defined($_) } @list # first defined value in @list | |
61 | $foo = first { $_ > $value } @list # first value in @list which | |
62 | # is greater than $value | |
c29e891d | 63 | |
f4a2945e JH |
64 | This function could be implemented using C<reduce> like this |
65 | ||
66 | $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list | |
67 | ||
68 | for example wanted() could be defined() which would return the first | |
69 | defined value in @list | |
70 | ||
71 | =item max LIST | |
72 | ||
73 | Returns the entry in the list with the highest numerical value. If the | |
74 | list is empty then C<undef> is returned. | |
75 | ||
76 | $foo = max 1..10 # 10 | |
77 | $foo = max 3,9,12 # 12 | |
78 | $foo = max @bar, @baz # whatever | |
79 | ||
80 | This function could be implemented using C<reduce> like this | |
81 | ||
82 | $foo = reduce { $a > $b ? $a : $b } 1..10 | |
83 | ||
84 | =item maxstr LIST | |
85 | ||
86 | Similar to C<max>, but treats all the entries in the list as strings | |
87 | and returns the highest string as defined by the C<gt> operator. | |
88 | If the list is empty then C<undef> is returned. | |
c29e891d GB |
89 | |
90 | $foo = maxstr 'A'..'Z' # 'Z' | |
f4a2945e JH |
91 | $foo = maxstr "hello","world" # "world" |
92 | $foo = maxstr @bar, @baz # whatever | |
93 | ||
94 | This function could be implemented using C<reduce> like this | |
95 | ||
96 | $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z' | |
97 | ||
98 | =item min LIST | |
99 | ||
100 | Similar to C<max> but returns the entry in the list with the lowest | |
101 | numerical value. If the list is empty then C<undef> is returned. | |
102 | ||
103 | $foo = min 1..10 # 1 | |
104 | $foo = min 3,9,12 # 3 | |
105 | $foo = min @bar, @baz # whatever | |
106 | ||
107 | This function could be implemented using C<reduce> like this | |
108 | ||
109 | $foo = reduce { $a < $b ? $a : $b } 1..10 | |
110 | ||
111 | =item minstr LIST | |
112 | ||
113 | Similar to C<min>, but treats all the entries in the list as strings | |
114 | and returns the lowest string as defined by the C<lt> operator. | |
115 | If the list is empty then C<undef> is returned. | |
116 | ||
c29e891d GB |
117 | $foo = minstr 'A'..'Z' # 'A' |
118 | $foo = minstr "hello","world" # "hello" | |
119 | $foo = minstr @bar, @baz # whatever | |
f4a2945e JH |
120 | |
121 | This function could be implemented using C<reduce> like this | |
122 | ||
123 | $foo = reduce { $a lt $b ? $a : $b } 'A'..'Z' | |
124 | ||
125 | =item reduce BLOCK LIST | |
126 | ||
ddf53ba4 GB |
127 | Reduces LIST by calling BLOCK, in a scalar context, multiple times, |
128 | setting C<$a> and C<$b> each time. The first call will be with C<$a> | |
129 | and C<$b> set to the first two elements of the list, subsequent | |
130 | calls will be done by setting C<$a> to the result of the previous | |
131 | call and C<$b> to the next element in the list. | |
f4a2945e JH |
132 | |
133 | Returns the result of the last call to BLOCK. If LIST is empty then | |
134 | C<undef> is returned. If LIST only contains one element then that | |
135 | element is returned and BLOCK is not executed. | |
136 | ||
137 | $foo = reduce { $a < $b ? $a : $b } 1..10 # min | |
138 | $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr | |
139 | $foo = reduce { $a + $b } 1 .. 10 # sum | |
140 | $foo = reduce { $a . $b } @bar # concat | |
141 | ||
2ff28616 GB |
142 | If your algorithm requires that C<reduce> produce an identity value, then |
143 | make sure that you always pass that identity value as the first argument to prevent | |
144 | C<undef> being returned | |
145 | ||
146 | $foo = reduce { $a + $b } 0, @values; # sum with 0 identity value | |
147 | ||
1bfb5477 GB |
148 | =item shuffle LIST |
149 | ||
150 | Returns the elements of LIST in a random order | |
151 | ||
c29e891d GB |
152 | @cards = shuffle 0..51 # 0..51 in a random order |
153 | ||
f4a2945e JH |
154 | =item sum LIST |
155 | ||
82f35e8b RH |
156 | Returns the sum of all the elements in LIST. If LIST is empty then |
157 | C<undef> is returned. | |
f4a2945e JH |
158 | |
159 | $foo = sum 1..10 # 55 | |
160 | $foo = sum 3,9,12 # 24 | |
161 | $foo = sum @bar, @baz # whatever | |
162 | ||
163 | This function could be implemented using C<reduce> like this | |
164 | ||
165 | $foo = reduce { $a + $b } 1..10 | |
166 | ||
2ff28616 GB |
167 | If your algorithm requires that C<sum> produce an identity of 0, then |
168 | make sure that you always pass C<0> as the first argument to prevent | |
169 | C<undef> being returned | |
170 | ||
171 | $foo = sum 0, @values; | |
172 | ||
8b198969 CBW |
173 | =item sum0 LIST |
174 | ||
175 | Similar to C<sum>, except this returns 0 when given an empty list, rather | |
176 | than C<undef>. | |
177 | ||
f4a2945e JH |
178 | =back |
179 | ||
9c3c560b JH |
180 | =head1 KNOWN BUGS |
181 | ||
182 | With perl versions prior to 5.005 there are some cases where reduce | |
183 | will return an incorrect result. This will show up as test 7 of | |
184 | reduce.t failing. | |
185 | ||
f4a2945e JH |
186 | =head1 SUGGESTED ADDITIONS |
187 | ||
188 | The following are additions that have been requested, but I have been reluctant | |
189 | to add due to them being very simple to implement in perl | |
190 | ||
191 | # One argument is true | |
192 | ||
193 | sub any { $_ && return 1 for @_; 0 } | |
194 | ||
195 | # All arguments are true | |
196 | ||
197 | sub all { $_ || return 0 for @_; 1 } | |
198 | ||
199 | # All arguments are false | |
200 | ||
201 | sub none { $_ && return 0 for @_; 1 } | |
202 | ||
203 | # One argument is false | |
204 | ||
205 | sub notall { $_ || return 1 for @_; 0 } | |
206 | ||
207 | # How many elements are true | |
208 | ||
209 | sub true { scalar grep { $_ } @_ } | |
210 | ||
211 | # How many elements are false | |
212 | ||
213 | sub false { scalar grep { !$_ } @_ } | |
214 | ||
ddf53ba4 GB |
215 | =head1 SEE ALSO |
216 | ||
217 | L<Scalar::Util>, L<List::MoreUtils> | |
218 | ||
f4a2945e JH |
219 | =head1 COPYRIGHT |
220 | ||
2ff28616 | 221 | Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. |
f4a2945e JH |
222 | This program is free software; you can redistribute it and/or |
223 | modify it under the same terms as Perl itself. | |
224 | ||
225 | =cut |