Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: One out of three ain't bad (order)

by ph713 (Pilgrim)
on Oct 22, 2005 at 08:42 UTC ( [id://502194]=note: print w/replies, xml ) Need Help??


in reply to Re: One out of three ain't bad (order)
in thread One out of three ain't bad

I like this !! based solution the best. It's the most logical way to solve the problem - convert the arguments to boolean truth values, then sum them. You could do it for an arbitrarily long list of variables with:
use List::Util qw( sum ); if( 1 == sum( map { !!$_ } @inputs) ) { ... }

Replies are listed 'Best First'.
Re^3: One out of three ain't bad (order)
by Tanktalus (Canon) on Oct 22, 2005 at 14:46 UTC

    As long as you're going to go through the same code path for each element in the array anyway, why not de-obfuscate that a bit and use the ternary operator?

    if (1 == sum( map { $_ ? 1 : 0 } @inputs ))
    While tye may be right that, in practice, Perl can't change the value of !0, I just don't like relying on obscure implementation details when clear, well-defined implementation details are available to me to accomplish the same thing in a readable, understandable, and maintainable manner.

      Why are you using sum instead of grep here? If you are summing 1 and 0 then the sum will always be the same as the grep answer. For what it's worth I woul disagree that ($_ ? 1 : 0) is clearer in anyway than !!$_. To me the !! version yells bool, while I had to read the ?: version twice before comprehension came. That might be in part because Perl6's new bool context (don't shoot me if that isn't the right wording) lets you ask for a true/false value explicity so thats what I would expext !! to do.


      ___________
      Eric Hodges

        I'm using some because the parent used sum. And s/he's using sum because s/he is emulating tye's addition of booleans.

        You're right that !! screams boolean. However, that's not what is happening here. We are adding numbers here. Let me ask you what "true + false + true" is. That's just nonsensical. Programmers have been getting away with it for years because computers only deal with numbers - whether that's boolean, character, or, oddly enough, numerical. But, abstractly, it makes no sense. You can't add booleans. Boolean algebra consists of operators such as not, and, or, xor, nand, and nor. (I may have missed some - it's been a few years since I've dealt that directly with boolean algebra in the theoretical sense.) Addition, subtraction, multiplication, division - all of these are nonsensical in the boolean world. In fact, the multiplication symbol (the middle-dot, ·) was reused for the "and" operator, and the addition symbol (+) was reused for the "or" operator, IIRC (which I may not recall correctly...). And, as in mathematical algebra, the multiplication symbol is optional between terms, further reusing the sigils.

        Therefore, adding boolean values is just confusing. The answer to the question above is that "true + false + true" equals "true" (numerically: 1 || 0 || 1) - and the OP wants this to be a count of 2. Which, if my memory serves, the OP wants "a nor b nor c" Unfortunately, the nor solution doesn't scale past three.

        grep, unlike sum, uses boolean context. So the choices are to either sum numbers (using the ternary operator, for example), or to grep a list in scalar context. Adding booleans makes me question whether the author knows what s/he's doing, or is just copying what someone else is doing - whether s/he's too clever (in taking advantage of undefined behaviour) or not clever enough by half (in just copying something that works without understanding that it's undefined behaviour).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found