Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: argmin & argmax

by Roy Johnson (Monsignor)
on May 04, 2005 at 03:42 UTC ( [id://453806]=note: print w/replies, xml ) Need Help??


in reply to argmin & argmax

You can tighten things up a little by initializing with the first element rather than dealing with undef:
sub argmax(&@) { return() unless @_ > 1; my $block = shift; my $index = shift; my $max = $block->($index); for (@_) { my $val = $block->($_); ($max, $index) = ($val, $_) if $val > $max; } return wantarray ? ($index, $max) : $index; }

Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://453806]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-24 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found