Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/2perl/List::Util

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  | will give a
.\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
.\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "List::Util 3"
.TH List::Util 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
List::Util \- A selection of general\-utility list subroutines
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\&    use List::Util qw(first max maxstr min minstr reduce shuffle sum);
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\f(CW\*(C`List::Util\*(C'\fR contains a selection of subroutines that people have
expressed would be nice to have in the perl core, but the usage would
not really be high enough to warrant the use of a keyword, and the size
so small such that being individual extensions would be wasteful.
.PP
By default \f(CW\*(C`List::Util\*(C'\fR does not export any subroutines. The
subroutines defined are
.IP "first \s-1BLOCK\s0 \s-1LIST\s0" 4
.IX Item "first BLOCK LIST"
Similar to \f(CW\*(C`grep\*(C'\fR in that it evaluates \s-1BLOCK\s0 setting \f(CW$_\fR to each element
of \s-1LIST\s0 in turn. \f(CW\*(C`first\*(C'\fR returns the first element where the result from
\&\s-1BLOCK\s0 is a true value. If \s-1BLOCK\s0 never returns true or \s-1LIST\s0 was empty then
\&\f(CW\*(C`undef\*(C'\fR is returned.
.Sp
.Vb 3
\&    $foo = first { defined($_) } @list    # first defined value in @list
\&    $foo = first { $_ > $value } @list    # first value in @list which
\&                                          # is greater than $value
.Ve
.Sp
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
.Sp
.Vb 1
\&    $foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list
.Ve
.Sp
for example \fIwanted()\fR could be \fIdefined()\fR which would return the first
defined value in \f(CW@list\fR
.IP "max \s-1LIST\s0" 4
.IX Item "max LIST"
Returns the entry in the list with the highest numerical value. If the
list is empty then \f(CW\*(C`undef\*(C'\fR is returned.
.Sp
.Vb 3
\&    $foo = max 1..10                # 10
\&    $foo = max 3,9,12               # 12
\&    $foo = max @bar, @baz           # whatever
.Ve
.Sp
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
.Sp
.Vb 1
\&    $foo = reduce { $a > $b ? $a : $b } 1..10
.Ve
.IP "maxstr \s-1LIST\s0" 4
.IX Item "maxstr LIST"
Similar to \f(CW\*(C`max\*(C'\fR, but treats all the entries in the list as strings
and returns the highest string as defined by the \f(CW\*(C`gt\*(C'\fR operator.
If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned.
.Sp
.Vb 3
\&    $foo = maxstr 'A'..'Z'          # 'Z'
\&    $foo = maxstr "hello","world"   # "world"
\&    $foo = maxstr @bar, @baz        # whatever
.Ve
.Sp
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
.Sp
.Vb 1
\&    $foo = reduce { $a gt $b ? $a : $b } 'A'..'Z'
.Ve
.IP "min \s-1LIST\s0" 4
.IX Item "min LIST"
Similar to \f(CW\*(C`max\*(C'\fR but returns the entry in the list with the lowest
numerical value. If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned.
.Sp
.Vb 3
\&    $foo = min 1..10                # 1
\&    $foo = min 3,9,12               # 3
\&    $foo = min @bar, @baz           # whatever
.Ve
.Sp
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
.Sp
.Vb 1
\&    $foo = reduce { $a < $b ? $a : $b } 1..10
.Ve
.IP "minstr \s-1LIST\s0" 4
.IX Item "minstr LIST"
Similar to \f(CW\*(C`min\*(C'\fR, but treats all the entries in the list as strings
and returns the lowest string as defined by the \f(CW\*(C`lt\*(C'\fR operator.
If the list is empty then \f(CW\*(C`undef\*(C'\fR is returned.
.Sp
.Vb 3
\&    $foo = minstr 'A'..'Z'          # 'A'
\&    $foo = minstr "hello","world"   # "hello"
\&    $foo = minstr @bar, @baz        # whatever
.Ve
.Sp
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
.Sp
.Vb 1
\&    $foo = reduce { $a lt $b ? $a : $b } 'A'..'Z'
.Ve
.IP "reduce \s-1BLOCK\s0 \s-1LIST\s0" 4
.IX Item "reduce BLOCK LIST"
Reduces \s-1LIST\s0 by calling \s-1BLOCK\s0 multiple times, setting \f(CW$a\fR and \f(CW$b\fR
each time. The first call will be with \f(CW$a\fR and \f(CW$b\fR set to the first
two elements of the list, subsequent calls will be done by
setting \f(CW$a\fR to the result of the previous call and \f(CW$b\fR to the next
element in the list.
.Sp
Returns the result of the last call to \s-1BLOCK\s0. If \s-1LIST\s0 is empty then
\&\f(CW\*(C`undef\*(C'\fR is returned. If \s-1LIST\s0 only contains one element then that
element is returned and \s-1BLOCK\s0 is not executed.
.Sp
.Vb 4
\&    $foo = reduce { $a < $b ? $a : $b } 1..10       # min
\&    $foo = reduce { $a lt $b ? $a : $b } 'aa'..'zz' # minstr
\&    $foo = reduce { $a + $b } 1 .. 10               # sum
\&    $foo = reduce { $a . $b } @bar                  # concat
.Ve
.IP "shuffle \s-1LIST\s0" 4
.IX Item "shuffle LIST"
Returns the elements of \s-1LIST\s0 in a random order
.Sp
.Vb 1
\&    @cards = shuffle 0..51      # 0..51 in a random order
.Ve
.IP "sum \s-1LIST\s0" 4
.IX Item "sum LIST"
Returns the sum of all the elements in \s-1LIST\s0.
.Sp
.Vb 3
\&    $foo = sum 1..10                # 55
\&    $foo = sum 3,9,12               # 24
\&    $foo = sum @bar, @baz           # whatever
.Ve
.Sp
This function could be implemented using \f(CW\*(C`reduce\*(C'\fR like this
.Sp
.Vb 1
\&    $foo = reduce { $a + $b } 1..10
.Ve
.SH "KNOWN BUGS"
.IX Header "KNOWN BUGS"
With perl versions prior to 5.005 there are some cases where reduce
will return an incorrect result. This will show up as test 7 of
reduce.t failing.
.SH "SUGGESTED ADDITIONS"
.IX Header "SUGGESTED ADDITIONS"
The following are additions that have been requested, but I have been reluctant
to add due to them being very simple to implement in perl
.PP
.Vb 1
\&  # One argument is true
.Ve
.PP
.Vb 1
\&  sub any { $_ && return 1 for @_; 0 }
.Ve
.PP
.Vb 1
\&  # All arguments are true
.Ve
.PP
.Vb 1
\&  sub all { $_ || return 0 for @_; 1 }
.Ve
.PP
.Vb 1
\&  # All arguments are false
.Ve
.PP
.Vb 1
\&  sub none { $_ && return 0 for @_; 1 }
.Ve
.PP
.Vb 1
\&  # One argument is false
.Ve
.PP
.Vb 1
\&  sub notall { $_ || return 1 for @_; 0 }
.Ve
.PP
.Vb 1
\&  # How many elements are true
.Ve
.PP
.Vb 1
\&  sub true { scalar grep { $_ } @_ }
.Ve
.PP
.Vb 1
\&  # How many elements are false
.Ve
.PP
.Vb 1
\&  sub false { scalar grep { !$_ } @_ }
.Ve
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
Copyright (c) 1997\-2001 Graham Barr <gbarr@pobox.com>. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.