Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Secret Perl Operators: the boolean list squash operator, x!!

by Aristotle (Chancellor)
on Jul 31, 2006 at 20:40 UTC ( [id://564866]=note: print w/replies, xml ) Need Help??


in reply to Re: Secret Perl Operators: the boolean list squash operator, x!!
in thread Secret Perl Operators: the boolean list squash operator, x!!

Let’s see an example of how clean and self-describing such a thing looks in the language of your choice. Go on, astound me. :-)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: Secret Perl Operators: the boolean list squash operator, x!!
by Anonymous Monk on Aug 01, 2006 at 20:05 UTC
    Let’s see an example of how clean and self-describing such a thing looks in the language of your choice. Go on, astound me. :-)

    I'm not likely to astound anyone. I write boring code, deliberately. It's easier to find bugs in boring code; code that's full of clever, subtle tricks causes all the problems, in my experience.

    A simple series of append statements describes exactly what you want to do, but you seem to want to make life complicated, because you seem overly-concerned about the repetition of the variable name and the .= operator. I think that's only a real concern if you have to write such repeated expressions a lot in a given body of code, but if you really have to be concise (which is part of what I think you mean by "clean"), I still think there are simpler ways to write them.

    Here's an even more concise way to write the main expression, using two helper functions. No wierd operator tricks are required, and this code could be written in just about any language, let alone a "language of choice". In some languages, using macros as opposed to helper functions might give a performance boost, but the principle remains the same...

    # If the constant string "admin" changes, you can change it # in just one place... sub admin_link { my ($flag) = @_; if ($flag ) { return('admin'); } return(""); } # this can obviously be optimized as necessary... sub join_if_defined { my ( $join_string,@list) = @_; my @defined_elements; @defined_elements = grep( defined($_),@list); return join($join_string, @defined_elements ); } @sections = ( 'http://example.net/app', admin_link($is_admin), $subsite, $node, $id, $subnode ); $uri = join_if_defined("/", @sections);

    I think that's reasonablely concise; it's certainly easier to read than a list containing phrases like "a list of one element consisting of <something> repeated not not <some expression> times".

      Sorry, but I find that 10× less readable than doing the work in place would be. In a realistic codebase, these pieces would be far apart, and making sure you understand exactly what is going on would require crossreferencing. Dependencies should not be introduced without cause, as they always carry a cost. Your suggestion is just an overmodularised version of this:

      my $uri = join '/', 'http://example.net/app', grep { defined } ( ( $is_admin ? 'admin' : undef ), $subsite, $node, $id, $subnode, );

      That is basically tye’s suggestion. And it’s a fine suggestion if you can resolve the irregularities enough to do it this way. But it doesn’t cover all cases that x!! can – see my other example where grep is clearly inappropriate.

      Makeshifts last the longest.

      To be equivalent to the parent code, and to retain the genericity of ( ... ) x!! ...,
      return("");
      should be
      return;
      or
      return wantarray ? () : "";

      This doesn't affect your argument.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found