This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the crypt() pickier: if downgrading doesn't work,
[perl5.git] / ext / List / Util / lib / List / Util.pm
CommitLineData
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
7package List::Util;
8
9require Exporter;
ee4ffb48 10require DynaLoader;
f4a2945e 11
ee4ffb48 12our @ISA = qw(Exporter DynaLoader);
1bfb5477 13our @EXPORT_OK = qw(first min max minstr maxstr reduce sum shuffle);
163827e7 14our $VERSION = "1.05_00";
f4a2945e 15
ee4ffb48 16bootstrap List::Util $VERSION;
f4a2945e 17
f4a2945e
JH
181;
19
20__END__
21
22=head1 NAME
23
24List::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
32C<List::Util> contains a selection of subroutines that people have
33expressed would be nice to have in the perl core, but the usage would
34not really be high enough to warrant the use of a keyword, and the size
35so small such that being individual extensions would be wasteful.
36
37By default C<List::Util> does not export any subroutines. The
38subroutines defined are
39
40=over 4
41
42=item first BLOCK LIST
43
44Similar to C<grep> in that it evaluates BLOCK setting C<$_> to each element
45of LIST in turn. C<first> returns the first element where the result from
46BLOCK is a true value. If BLOCK never returns true or LIST was empty then
47C<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
53This function could be implemented using C<reduce> like this
54
55 $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list
56
57for example wanted() could be defined() which would return the first
58defined value in @list
59
60=item max LIST
61
62Returns the entry in the list with the highest numerical value. If the
63list 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
69This 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
75Similar to C<max>, but treats all the entries in the list as strings
76and returns the highest string as defined by the C<gt> operator.
77If 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
83This 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
89Similar to C<max> but returns the entry in the list with the lowest
90numerical 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
96This 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
102Similar to C<min>, but treats all the entries in the list as strings
103and returns the lowest string as defined by the C<lt> operator.
104If 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
110This 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
116Reduces LIST by calling BLOCK multiple times, setting C<$a> and C<$b>
117each time. The first call will be with C<$a> and C<$b> set to the first
118two elements of the list, subsequent calls will be done by
119setting C<$a> to the result of the previous call and C<$b> to the next
c29e891d 120element in the list.
f4a2945e
JH
121
122Returns the result of the last call to BLOCK. If LIST is empty then
123C<undef> is returned. If LIST only contains one element then that
124element 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
133Returns 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
139Returns 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
145This 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
153The following are additions that have been requested, but I have been reluctant
154to 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 182Copyright (c) 1997-2001 Graham Barr <gbarr@pobox.com>. All rights reserved.
f4a2945e
JH
183This program is free software; you can redistribute it and/or
184modify it under the same terms as Perl itself.
185
186=cut