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

Re^2: Ternary vs. Sort vs. Max

by Anonymous Monk
on Aug 10, 2015 at 12:34 UTC ( [id://1138022]=note: print w/replies, xml ) Need Help??


in reply to Re: Ternary vs. Sort vs. Max
in thread Ternary vs. Sort vs. Max

The basic problem in this example, though, is that none of these three examples are actually equivalent!

They accomplish the same thing in different ways... what definition of "equivalent" are you using here?

Replies are listed 'Best First'.
Re^3: Ternary vs. Sort vs. Max
by Laurent_R (Canon) on Aug 10, 2015 at 13:18 UTC
    They are equivalent for finding the max of only two values, but a single ternary operator can't be used when there are more than two values or when the number of values is not known in advance. But for only 2 values, I prefer the ternary operator.

    Then, for more values, I would not be too reluctant at using sort for just a handful of values, but I would avoid it for a large list because of the overhead. So, in that latter case, I would rather use the List::Utils max function, or roll my own sequential iteration through the list if the type of max I am looking for is something more complicated than just the largest number in a list (or perhaps use the List::Utils handsome reduce function with a appropriate code block).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found