Commit | Line | Data |
---|---|---|
f4a2945e JH |
1 | # List::Util.pm |
2 | # | |
c29e891d | 3 | # Copyright (c) 1997-2001 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. | |
6 | ||
7 | package List::Util; | |
8 | ||
9 | require Exporter; | |
ee4ffb48 | 10 | require DynaLoader; |
f4a2945e | 11 | |
ee4ffb48 | 12 | our @ISA = qw(Exporter DynaLoader); |
1bfb5477 | 13 | our @EXPORT_OK = qw(first min max minstr maxstr reduce sum shuffle); |
163827e7 | 14 | our $VERSION = "1.05_00"; |
f4a2945e | 15 | |
ee4ffb48 | 16 | bootstrap List::Util $VERSION; |
f4a2945e | 17 | |
f4a2945e JH |
18 | 1; |
19 | ||
20 | __END__ | |
21 | ||
22 | =head1 NAME | |
23 | ||
24 | List::Util - A selection of general-utility list subroutines | |
25 | ||
26 | =head1 SYNOPSIS | |
27 | ||
c29e891d | 28 | use List::Util qw(first max maxstr min minstr reduce shuffle sum); |
f4a2945e JH |
29 | |
30 | =head1 DESCRIPTION | |
31 | ||
32 | C<List::Util> contains a selection of subroutines that people have | |
33 | expressed would be nice to have in the perl core, but the usage would | |
34 | not really be high enough to warrant the use of a keyword, and the size | |
35 | so small such that being individual extensions would be wasteful. | |
36 | ||
37 | By default C<List::Util> does not export any subroutines. The | |
38 | subroutines defined are | |
39 | ||
40 | =over 4 | |
41 | ||
42 | =item first BLOCK LIST | |
43 | ||
44 | Similar to C<grep> in that it evaluates BLOCK setting C<$_> to each element | |
45 | of LIST in turn. C<first> returns the first element where the result from | |
46 | BLOCK is a true value. If BLOCK never returns true or LIST was empty then | |
47 | C<undef> is returned. | |
48 | ||
49 | $foo = first { defined($_) } @list # first defined value in @list | |
50 | $foo = first { $_ > $value } @list # first value in @list which | |
51 | # is greater than $value | |
c29e891d | 52 | |
f4a2945e JH |
53 | This function could be implemented using C<reduce> like this |
54 | ||
55 | $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list | |
56 | ||
57 | for example wanted() could be defined() which would return the first | |
58 | defined value in @list | |
59 | ||
60 | =item max LIST | |
61 | ||
62 | Returns the entry in the list with the highest numerical value. If the | |
63 | list is empty then C<undef> is returned. | |
64 | ||
65 | $foo = max 1..10 # 10 | |
66 | $foo = max 3,9,12 # 12 | |
67 | $foo = max @bar, @baz # whatever | |
68 | ||
69 | This function could be implemented using C<reduce> like this | |
70 | ||
71 | $foo = reduce { $a > $b ? $a : $b } 1..10 | |
72 | ||
73 | =item maxstr LIST | |
74 | ||
75 | Similar to C<max>, but treats all the entries in the list as strings | |
76 | and returns the highest string as defined by the C<gt> operator. | |
77 | If the list is empty then C<undef> is returned. | |
c29e891d GB |
78 | |
79 | $foo = maxstr 'A'..'Z' # 'Z' | |
f4a2945e JH |
80 | $foo = maxstr "hello","world" # "world" |
81 | $foo = maxstr @bar, @baz # whatever | |
82 | ||
83 | This function could be implemented using C<reduce> like this | |
84 | ||
85 | $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z' | |
86 | ||
87 | =item min LIST | |
88 | ||
89 | Similar to C<max> but returns the entry in the list with the lowest | |
90 | numerical value. If the list is empty then C<undef> is returned. | |
91 | ||
92 | $foo = min 1..10 # 1 | |
93 | $foo = min 3,9,12 # 3 | |
94 | $foo = min @bar, @baz # whatever | |
95 | ||
96 | This function could be implemented using C<reduce> like this | |
97 | ||
98 | $foo = reduce { $a < $b ? $a : $b } 1..10 | |
99 | ||
100 | =item minstr LIST | |
101 | ||
102 | Similar to C<min>, but treats all the entries in the list as strings | |
103 | and returns the lowest string as defined by the C<lt> operator. | |
104 | If the list is empty then C<undef> is returned. | |
105 | ||
c29e891d GB |
106 | $foo = minstr 'A'..'Z' # 'A' |
107 | $foo = minstr "hello","world" # "hello" | |
108 | $foo = minstr @bar, @baz # whatever | |
f4a2945e JH |
109 | |
110 | This function could be implemented using C<reduce> like this | |
111 | ||
112 | $foo = reduce { $a lt $b ? $a : $b } 'A'..'Z' | |
113 | ||
114 | =item reduce BLOCK LIST | |
115 | ||
116 | Reduces LIST by calling BLOCK multiple times, setting C<$a> and C<$b> | |
117 | each time. The first call will be with C<$a> and C<$b> set to the first | |
118 | two elements of the list, subsequent calls will be done by | |
119 | setting C<$a> to the result of the previous call and C<$b> to the next | |
c29e891d | 120 | element in the list. |
f4a2945e JH |
121 | |
122 | Returns the result of the last call to BLOCK. If LIST is empty then | |
123 | C<undef> is returned. If LIST only contains one element then that | |
124 | element is returned and BLOCK is not executed. | |
125 | ||
126 | $foo = reduce { $a < $b ? $a : $b } 1..10 # min | |
127 | $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr | |
128 | $foo = reduce { $a + $b } 1 .. 10 # sum | |
129 | $foo = reduce { $a . $b } @bar # concat | |
130 | ||
1bfb5477 GB |
131 | =item shuffle LIST |
132 | ||
133 | Returns the elements of LIST in a random order | |
134 | ||
c29e891d GB |
135 | @cards = shuffle 0..51 # 0..51 in a random order |
136 | ||
f4a2945e JH |
137 | =item sum LIST |
138 | ||
139 | Returns the sum of all the elements in LIST. | |
140 | ||
141 | $foo = sum 1..10 # 55 | |
142 | $foo = sum 3,9,12 # 24 | |
143 | $foo = sum @bar, @baz # whatever | |
144 | ||
145 | This function could be implemented using C<reduce> like this | |
146 | ||
147 | $foo = reduce { $a + $b } 1..10 | |
148 | ||
149 | =back | |
150 | ||
151 | =head1 SUGGESTED ADDITIONS | |
152 | ||
153 | The following are additions that have been requested, but I have been reluctant | |
154 | to add due to them being very simple to implement in perl | |
155 | ||
156 | # One argument is true | |
157 | ||
158 | sub any { $_ && return 1 for @_; 0 } | |
159 | ||
160 | # All arguments are true | |
161 | ||
162 | sub all { $_ || return 0 for @_; 1 } | |
163 | ||
164 | # All arguments are false | |
165 | ||
166 | sub none { $_ && return 0 for @_; 1 } | |
167 | ||
168 | # One argument is false | |
169 | ||
170 | sub notall { $_ || return 1 for @_; 0 } | |
171 | ||
172 | # How many elements are true | |
173 | ||
174 | sub true { scalar grep { $_ } @_ } | |
175 | ||
176 | # How many elements are false | |
177 | ||
178 | sub false { scalar grep { !$_ } @_ } | |
179 | ||
180 | =head1 COPYRIGHT | |
181 | ||
c29e891d | 182 | Copyright (c) 1997-2001 Graham Barr <gbarr@pobox.com>. All rights reserved. |
f4a2945e JH |
183 | This program is free software; you can redistribute it and/or |
184 | modify it under the same terms as Perl itself. | |
185 | ||
186 | =cut |