This is what I threw on my scratchpad, plus two bug fixes:
sub maxima(&@)
{
my $measure = shift @_;
return if ! @_;
my @maxima = shift @_;
my $max;
$max = $measure->( $_ ) for @maxima;
for( @_ ) {
my $key = $measure->( $_ );
if( $max < $key ) {
@maxima = $_;
$max = $key;
} elsif( $max == $key ) {
push @maxima, $_;
}
}
return @maxima;
}
sub minima(&@)
{
my $measure = shift @_;
return if ! @_;
my @minima = shift @_;
my $min;
$min = $measure->( $_ ) for @minima;
for( @_ ) {
my $key = $measure->( $_ );
if( $key < $min ) {
$min = $key;
@minima = $_;
} elsif( $key == $min ) {
push @minima, $_;
}
}
return @minima;
}
sub strmaxima(&@)
{
my $measure = shift @_;
return if ! @_;
my @maxima = shift @_;
my $max;
$max = $measure->( $_ ) for @maxima;
for( @_ ) {
my $key = $measure->( $_ );
if( $max lt $key ) {
@maxima = $_;
$max = $key;
} elsif( $max eq $key ) {
push @maxima, $_;
}
}
return @maxima;
}
sub strminima(&@)
{
my $measure = shift @_;
return if ! @_;
my @minima = shift @_;
my $min;
$min = $measure->( $_ ) for @minima;
for( @_ ) {
my $key = $measure->( $_ );
if( $key lt $min ) {
@minima = $_;
$min = $key;
} elsif( $key eq $min ) {
push @minima, $_;
}
}
return @minima;
}
Which I think would be fine additions to List::Util.
However, (Update: I added return if ! @_; to each because ) these should return an empty list not (undef) when given an empty list.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|