Content-type: text/html
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
By default "List::Util" does not export any subroutines. The subroutines defined are
$foo = first { defined($_) } @list # first defined value in @list $foo = first { $_ > $value } @list # first value in @list which # is greater than $value
This function could be implemented using "reduce" like this
$foo = reduce { defined($a) ? $a : wanted($b) ? $b : undef } undef, @list
for example wanted() could be defined() which would return the first defined value in @list
$foo = max 1..10 # 10 $foo = max 3,9,12 # 12 $foo = max @bar, @baz # whatever
This function could be implemented using "reduce" like this
$foo = reduce { $a > $b ? $a : $b } 1..10
$foo = maxstr 'A'..'Z' # 'Z' $foo = maxstr "hello","world" # "world" $foo = maxstr @bar, @baz # whatever
This function could be implemented using "reduce" like this
$foo = reduce { $a gt $b ? $a : $b } 'A'..'Z'
$foo = min 1..10 # 1 $foo = min 3,9,12 # 3 $foo = min @bar, @baz # whatever
This function could be implemented using "reduce" like this
$foo = reduce { $a < $b ? $a : $b } 1..10
$foo = minstr 'A'..'Z' # 'A' $foo = minstr "hello","world" # "hello" $foo = minstr @bar, @baz # whatever
This function could be implemented using "reduce" like this
$foo = reduce { $a lt $b ? $a : $b } 'A'..'Z'
Returns the result of the last call to BLOCK. If LIST is empty then "undef" is returned. If LIST only contains one element then that element is returned and BLOCK is not executed.
$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
@cards = shuffle 0..51 # 0..51 in a random order
$foo = sum 1..10 # 55 $foo = sum 3,9,12 # 24 $foo = sum @bar, @baz # whatever
This function could be implemented using "reduce" like this
$foo = reduce { $a + $b } 1..10
# One argument is true sub any { $_ && return 1 for @_; 0 } # All arguments are true sub all { $_ || return 0 for @_; 1 } # All arguments are false sub none { $_ && return 0 for @_; 1 } # One argument is false sub notall { $_ || return 1 for @_; 0 } # How many elements are true sub true { scalar grep { $_ } @_ } # How many elements are false sub false { scalar grep { !$_ } @_ }