This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
dump.c: White-space only
[perl5.git] / cpan / List-Util / lib / List / Util.pm
CommitLineData
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
9package List::Util;
10
82f35e8b 11use strict;
f4a2945e
JH
12require Exporter;
13
3630f57e 14our @ISA = qw(Exporter);
8b198969 15our @EXPORT_OK = qw(first min max minstr maxstr reduce sum sum0 shuffle);
c9612cb4 16our $VERSION = "1.27";
3630f57e 17our $XS_VERSION = $VERSION;
60f3865b 18$VERSION = eval $VERSION;
f4a2945e 19
3630f57e
CBW
20require XSLoader;
21XSLoader::load('List::Util', $XS_VERSION);
09c2a9b8 22
8b198969
CBW
23sub sum0
24{
25 return 0 unless @_;
26 goto &sum;
27}
28
f4a2945e
JH
291;
30
31__END__
32
33=head1 NAME
34
35List::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
43C<List::Util> contains a selection of subroutines that people have
44expressed would be nice to have in the perl core, but the usage would
45not really be high enough to warrant the use of a keyword, and the size
46so small such that being individual extensions would be wasteful.
47
48By default C<List::Util> does not export any subroutines. The
49subroutines defined are
50
51=over 4
52
53=item first BLOCK LIST
54
55Similar to C<grep> in that it evaluates BLOCK setting C<$_> to each element
56of LIST in turn. C<first> returns the first element where the result from
57BLOCK is a true value. If BLOCK never returns true or LIST was empty then
58C<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
64This function could be implemented using C<reduce> like this
65
66 $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list
67
68for example wanted() could be defined() which would return the first
69defined value in @list
70
71=item max LIST
72
73Returns the entry in the list with the highest numerical value. If the
74list 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
80This 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
86Similar to C<max>, but treats all the entries in the list as strings
87and returns the highest string as defined by the C<gt> operator.
88If 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
94This 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
100Similar to C<max> but returns the entry in the list with the lowest
101numerical 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
107This 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
113Similar to C<min>, but treats all the entries in the list as strings
114and returns the lowest string as defined by the C<lt> operator.
115If 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
121This 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
127Reduces LIST by calling BLOCK, in a scalar context, multiple times,
128setting C<$a> and C<$b> each time. The first call will be with C<$a>
129and C<$b> set to the first two elements of the list, subsequent
130calls will be done by setting C<$a> to the result of the previous
131call and C<$b> to the next element in the list.
f4a2945e
JH
132
133Returns the result of the last call to BLOCK. If LIST is empty then
134C<undef> is returned. If LIST only contains one element then that
135element 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
142If your algorithm requires that C<reduce> produce an identity value, then
143make sure that you always pass that identity value as the first argument to prevent
144C<undef> being returned
145
146 $foo = reduce { $a + $b } 0, @values; # sum with 0 identity value
147
1bfb5477
GB
148=item shuffle LIST
149
150Returns 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
156Returns the sum of all the elements in LIST. If LIST is empty then
157C<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
163This function could be implemented using C<reduce> like this
164
165 $foo = reduce { $a + $b } 1..10
166
2ff28616
GB
167If your algorithm requires that C<sum> produce an identity of 0, then
168make sure that you always pass C<0> as the first argument to prevent
169C<undef> being returned
170
171 $foo = sum 0, @values;
172
8b198969
CBW
173=item sum0 LIST
174
175Similar to C<sum>, except this returns 0 when given an empty list, rather
176than C<undef>.
177
f4a2945e
JH
178=back
179
9c3c560b
JH
180=head1 KNOWN BUGS
181
182With perl versions prior to 5.005 there are some cases where reduce
183will return an incorrect result. This will show up as test 7 of
184reduce.t failing.
185
f4a2945e
JH
186=head1 SUGGESTED ADDITIONS
187
188The following are additions that have been requested, but I have been reluctant
189to 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
217L<Scalar::Util>, L<List::MoreUtils>
218
f4a2945e
JH
219=head1 COPYRIGHT
220
2ff28616 221Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved.
f4a2945e
JH
222This program is free software; you can redistribute it and/or
223modify it under the same terms as Perl itself.
224
225=cut