3 ms·
Ask HN: I casually came up with chained unary operators, do you like them?
Working on a simple virtual machine and a new programming language, I have discovered that the interpreter I have developed can read chained unary operators nominally.
It seems a useful side effect...
$a = 0
print ++$a++
# Prints "1" and sets a = 2 in memory
$a = 1
print --$a++
# Prints "0" and sets a = 1 in memory
$a = 0
print ++++$a
# Prints "2" and sets a = 2 in memory
$a = 0
print $a++++
# Prints "0" and sets a = 2 in memory
$a = 10
print ++$a----
# Prints "11" and sets a = 9 in memory
$a = 10
print ++--$a----++
# Prints "10" and sets a = 9 in memory
- babygoat 8y agoI personally prefer that these operations be kept separate for clarity. An extra line is justified.
- tcbasche 8y agoI feel like when someone has to count plus signs to see what the code is doing it's a bad sign.
- sloaken 8y agoReminded me of this: https://www.ioccc.org/ https://www.ioccc.org/
- Someone 8y agoReminded me of http://www.eelis.net/C++/analogliterals.xhtml http://www.eelis.net/C++/analogliterals.xhtml, which starts with unsigned int a = 4; unsigned int b = I---------I; assert( a == b ); Moves on to 2D: unsigned int c = ( o-----o | ! ! ! ! ! o-----o ).area; assert( c == (I-----I) * (I-------I) ); and puts the cherry on the cake with 3D literals: assert( top( o-------o |L \ | L \ | o-------o | ! ! ! ! ! o | ! L | ! L| ! o-------o ) == ( o-------o | ! ! ! o-------o ) );
- Adamantcheese 8y agoReminds me of good ol' APL.
- sattoshi 8y ago$a = 2 $b = -------$a What does $b equal? More importantly, how is this sane? I just wrote it and I'm not quite so myself without taking my time and separating the dashes.
- quickthrower2 8y agoI’m not a fan of ++a and similar code golf constructs. I really only use a++ for loops. I like to keep code simple and readable. Well done on writing an interpreter though. It’s not easy.