Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Ternary vs. Sort vs. Max

by sundialsvc4 (Abbot)
on Aug 10, 2015 at 23:16 UTC ( #1138089=note: print w/replies, xml ) Need Help??

in reply to Ternary vs. Sort vs. Max

If “that’s the whole point of it,” as you say, then I can’t speak to that.   (And, mind you, I do not intend in any way to judge that.)   All that I can say is that I believe one should always look for the simplest, the most obvious, and of course the reasonably least-expensive way to do something.   “If what you are doing should require no thought, pretty-please don’t make me think about it.”

For instance, if you want to find the largest value in a list ... say, the largest card in your poker hand ... then you do not need to sort the list to find that out.   Your eyes can make a single furtive glance over your hand, and now you know.   Therefore, I would not ask the computer to perform any extra work for me, if it would not pay off otherwise.

As for the other two, ternary operators work really well in the place where (I think) they were intended:   when you want to make a simple, either/or choice in an arithmetic expression.   But, I always want to see my “glance rule” clearly upheld.   I want to always see, “at a glance,” exactly what I expect to see.

I like things like max() simply because they are nice and expressive.   They clearly say what is “the designer’s intent.”   In most languages they will do just the right thing, efficiently.

As an aside, I encountered one programming language ... HP/2000 Access BASIC ... which had a MAX/MIN operator.   It was so very expressive ... such a nice feature.

Replies are listed 'Best First'.
Re^2: Ternary vs. Sort vs. Max
by Your Mother (Bishop) on Aug 11, 2015 at 01:35 UTC

    Replied to the wrong post again. If I were a cop I'd be doing a field sobriety test on you at this point.

    Only one language with min/max, huh? Ever use SQL? I understand it's a bizarre, fringe language used by nearly no one but maybe you've heard of it.

      Are you really serious, “Mom?”

      Pardon me, ladies and gentlemen, while I “ROTFMAO™” ... and conclude, most graciously, that seriousness in this matter simply cannot be possible.

      O-p-e-r-a-t-o-r-:   a unary or binary element within an arithmetic expression, viz:

      445 IF (A MAX B) > 5 THEN 550

      It very elegantly captures the most-common use case for a ternary operator, without requiring a function-call having two or more arguments (which that language did not support).

        You should really take a look at Perl 6: it enables you to build your own infix max operator in just a couple of code lines (see for example Moritz's tutorial on the subject: So that you could have something like:
        if ($c max $d > 5) { # ... }
        Oh, but I forgot, you absolutely (and unreasonably IMHO) hate Perl 6, too bad for you, or maybe you should reconsider your opinion on the subject.

        And, BTW, NO, I did not downvote your post. If anyone did, it's not me.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1138089]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2018-07-17 21:44 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (378 votes). Check out past polls.