Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: argmin & argmax

by Roy Johnson (Monsignor)
on May 04, 2005 at 03:42 UTC ( #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.

Comment on Re: argmin & argmax
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2015-07-07 01:16 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (86 votes), past polls