Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: argmin & argmax

by ikegami (Patriarch)
on May 13, 2025 at 00:01 UTC ( [id://11165020]=note: print w/replies, xml ) Need Help??


in reply to argmin & argmax

I've always wished that Sort::Key provided such functions.

In case you're not familiar with Sort::Key, at its core is keysort KEY_GENERATOR LIST, which could be used as follows:

# Sort the objects according to the value of their `name` attribute. my @sorted_objects = keysort { $_->name } @unsorted_objects;

It has variants.

# Sort the objects according to the value of their uint id my @sorted_objects = ukeysort { $_->id } @unsorted_objects;

You'd think the module would also provide similar subs to find the elements that would sort first and/or last, but it doesn't.

Your argmin and argmax sub are effectively specific variants of those missing subs.

Replies are listed 'Best First'.
Re^2: argmin & argmax
by sleet (Monk) on May 13, 2025 at 02:13 UTC

      Nice ...or is it? Seeing as it returns the top x values, is the algorithm used O(n) as desired, or O(n log n) like a sort? That said, the extra log n doesn't add much, though.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2025-07-17 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.