develooper Front page | perl.fwp | Postings from November 2007

new "!"-based secret operators

Thread Next
From:
Dmitry Karasik
Date:
November 30, 2007 00:27
Subject:
new "!"-based secret operators
Message ID:
20071130082702.GA66805@tetsuo.karasik.eu.org
Good news everyone!

Excuse exclamation marks abound, but this message is all about a set of new
secret operators I thought of, all based on the exclamation sign. The boolean
negation is not really often used, but when is, the brevity of "!" cannot be
overestimated.

Anyway, here's a set of conditional decrement/increment operators:

  $x +=!! $y  is same as $x++ if $y;
  $x -=!! $y       --    $x-- if $y;
  $x +=!  $y       --    $x++ unless $y;
  $x -=!  $y       --    $x-- unless $y;

and for the completeness sake, 

  $x *=!! $y       --    $x = 0 unless $y ,
  $x *=!  $y       --    $x = 0 if $y;

This bunch, I think, can be appropriately named "screwdriver operators":

   -=! and -=!!        - flathead
   +=! and +=!!        - phillips
   *=! and *=!!        - torx

I don't know what name fits best to distinguish between ! and !! versions
though.

But that's not all. There's also a binary form for the increment and decrement:

   $x +!!+ $y      --    $x + ( $y ? 1 : 0);
   $x -!!- $y      --    $x - ( $y ? 1 : 0);
   $x +!+  $y      --    $x + ( $y ? 0 : 1);
   $x -!-  $y      --    $x - ( $y ? 0 : 1);
  
These actually are not completely useless. Consider the often-used calculation
of how many buckets are required for a number of items, with max items per
bucket.  The usual form is 

   $n = int( $items / $max) + (( $items % $max) ? 1 : 0);

whereas 
   
   $n = int( $items / $max) +!!+ $items % $max;

is both more short and elegant :) Again, I don't know how to name these four.

And finally, we come to the useless ones, which I found funny but unfortunately
completely impractical. Here's a not-or (NOR) boolean operator that only
reliably operates on 0s and 1s as arguments (actually on a bit more than that,
but it's not that interesting): 

   0 !~! 0 = 1
   0 !~! 1 = 0
   1 !~! 0 = 0
   1 !~! 1 = 0

This one I named the Tesla operator, because it somehow resembles two
electrodes with a spark between them. And finally, for the pure fun of it -
two boolean constants, 

    !()            -- true  
    !%!            -- false

These two deserve to be named the Mongolian constants, because !%!
really resembles that soyombo thingy on the mongolian national flag
( http://upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Flag_of_Mongolia.svg/125px-Flag_of_Mongolia.svg.png ).
I was even tempted to say Mongolian Tao constants, because the thingy actually
contains the yin-yang sign, which can be also thought as a graphical representation of
a non-boolean, yet definitely binary logic :)

-- 
Sincerely,
	Dmitry Karasik


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About