Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: nested tabular ternary

by diotalevi (Canon)
on Apr 11, 2006 at 01:15 UTC ( [id://542415]=note: print w/replies, xml ) Need Help??


in reply to nested tabular ternary

You weren't nesting your ternaries. You seemed to think you were but you weren't. You were putting the next ternary's conditional into the result field of the previous ternary. You should have put the entire next ternary - conditional, result a, and result b - into the result field of the enclosing ternary.

$result = ($major_length == 4800) ? 18 : ($major_length == 8552 and $val == 4) ? 33 : die "not handled";

or...

$result = ($major_length == 4800) ? 18 : ($major_length == 8552) ? ($val == 4 ? 33 : $val == 1 ? 18 : die +"not handled" ) : die "not handled";

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: nested tabular ternary
by McDarren (Abbot) on Apr 11, 2006 at 01:42 UTC
    Update: - fixed screwed-up formatting within code tags.

    Just an observation regards style. (And please don't get me wrong - this is just an observation, not a critisism - each to their own :)

    I find it interesting that both yours and Liverpole's answer use a similar layout style for the ternary. That is - to break the line after an operator.

    This of course goes against what is recommended by TheDamian in PBP (pp. 27-29 & 121-123). Personally, I prefer his recommended style, and I would have written your second example like so:

    $result = ($major_length == 4800) ? 18 : ($major_length == 8552) ? ($val == 4 ? 33 : $val == 1 ? 18 : die "not handled") : die "not handled";
    To me, this is more readable - it looks more like a "table", and it's more obvious that there is some nesting. Also, having an operator at the start of a line makes it much more obvious that it's a continuation of the previous line.

    Cheers,
    Darren :)

      I don't see the "table" and it looks like you just scattered your code all across the page, as if it were scrambled.

      No wait.. it looks like a bad translation of the original lisp into formatted perl. It really does look like lisp indenting conventions were attempting to be followed in your example.

      (if (= major-length 4800) 18 (if (= major-length 8552) (if (= val 4) 33 (if (= val 1) 18 (error "not handled"))) (error "not handled")))

      Here's a nicer lisp version of the same thing. It's got more in common with what I originally suggested than does what you alleged TheDamian recommended.

      (cond ((= major-length 4800) 18) ((= major-length 8552) (cond ((= val 4) 33) ((= val 1) 18) (error "not handled"))) (error "not handled"))

      The above is just a more verbose version of the following. This is getting to be seriously terse to the point that there's almost not enough whitespace in there. I'm just following standard indenting rules so this is just stock lisp.

      (ecase major-length (4800 18) (8552 (ecase var (4 33) (1 18))))

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        I don't see the "table" and it looks like you just scattered your code all across the page, as if it were scrambled.
        hmm... maybe that has something to do with how it renders in your browser vs. mine, although as it's enclosed in code tags I wouldn't have thought so. But anyway, the idea is that the ":" and "?" parts of the ternary line up vertically - producing the table effect - hence the term "tabular ternary". Have you seen the examples in PBP that I referred to?
        Here's a nicer lisp version of the same thing.
        I've never used lisp, so I can't comment on that :)
        It's got more in common with what I originally suggested than does what you alleged TheDamian recommended.
        um, I'm not sure that I alleged anything. All I did was refer to two of the practices that he recommends in his book PBP, namely:
        • "When producing a value, use tabular ternaries" (pp. 121-123), and
        • "Break long expressions before an operator" (pp. 27-29)

        But anyway, as I said - my comments weren't meant as a critisism, but rather a commentary on style.

        Cheers,
        Darren :)

      I can't make heads or tail of your "more readable" "table". Did you mistakenly used tabs?

      Aligning ? and : provides the best results for me. Parens provide a visual scope:

      $result = ($major_length == 4800 ? 18 : ($major_length == 8552 ? ($val == 4 ? 33 : ($val == 1 ? 18 : die "not handled") ) : die "not handled" ) );

      And then there's Perlish:

      ; $result = ($major_length == 4800 ? 18 : ($major_length == 8552 ? ($val == + 4 ? 33 + : ($val == 1 ? 18 + : die "not handled") ) : die "not + handled" ) )

      None of these are particularly readable &mdash or maintainable. if should be used here.

        I can't make heads or tail of your "more readable" "table". Did you mistakenly used tabs?
        No, I never use tabs (I have vim configured to convert them to spaces), and I have no idea what happened there. It looked fine in Firefox (my default browser) when I first previewed and created it, but after yours and diotalevi's comments, I opened an IE window and took a look, and... ewww, I see what you mean. Anyway, I re-pasted (changed nothing) and now it looks okay for me in IE. So hopefully it makes sense to everyone else now :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found