use
vars
qw(@ISA @EXPORT $VERSION $a $b)
;
@ISA
=
qw(Exporter)
;
@EXPORT
=
qw(first min max minstr maxstr reduce sum shuffle)
;
$VERSION
=
"1.23"
;
$VERSION
=
eval
$VERSION
;
sub
reduce (&@) {
my
$code
=
shift
;
my
$type
= Scalar::Util::reftype(
$code
);
unless
(
$type
and
$type
eq
'CODE'
) {
Carp::croak(
"Not a subroutine reference"
);
}
no
strict
'refs'
;
return
shift
unless
@_
> 1;
my
$caller
=
caller
;
local
(*{
$caller
.
"::a"
}) = \
my
$a
;
local
(*{
$caller
.
"::b"
}) = \
my
$b
;
$a
=
shift
;
foreach
(
@_
) {
$b
=
$_
;
$a
= &{
$code
}();
}
$a
;
}
sub
first (&@) {
my
$code
=
shift
;
my
$type
= Scalar::Util::reftype(
$code
);
unless
(
$type
and
$type
eq
'CODE'
) {
Carp::croak(
"Not a subroutine reference"
);
}
foreach
(
@_
) {
return
$_
if
&{
$code
}();
}
undef
;
}
sub
sum (@) { reduce {
$a
+
$b
}
@_
}
sub
min (@) { reduce {
$a
<
$b
?
$a
:
$b
}
@_
}
sub
max (@) { reduce {
$a
>
$b
?
$a
:
$b
}
@_
}
sub
minstr (@) { reduce {
$a
lt
$b
?
$a
:
$b
}
@_
}
sub
maxstr (@) { reduce {
$a
gt
$b
?
$a
:
$b
}
@_
}
sub
shuffle (@) {
my
@a
=\(
@_
);
my
$n
;
my
$i
=
@_
;
map
{
$n
=
rand
(
$i
--);
(${
$a
[
$n
]},
$a
[
$n
] =
$a
[
$i
])[0];
}
@_
;
}
1;