Hello, I wanted to start a discussion on any elegant one-line algorithms people use to eliminate other much longer implementations. I hope others have some to share.
Example 1 - if x=0, set it to 1 and vice versa - essentially Boolean toggle:
x=0^x
(Note Apple doesn’t like traditional math - so I don’t believe it will work there)
Example 2 - I wanted to turn on light switch 2 from the double tap of a different light switch 1. So, if button 1 gets double tapped, set switch 2 to 1, if button 2 gets double tapped, set switch 2 to 0:
If any button gets double tapped, set switch 2 to (button number - 2)^2
Example 3 - Make x-y always return a positive number in more limiting languages - like tasmota rules:
More straightforward mathematics (0^0 = 1 isn't immediately obvious, and opinion is slightly divided on the matter), and likely marginal execution time benefits too.
Ya, your approach is definitely much easier. Not sure why I started using the power of 0 20+ years ago, but have been using it ever since. Maybe I was trying to catch non-binary numbers as well - who knows.
But you gotta admit it’s pretty elegant - and in the 20+ years I’ve done it, only the Apple calculator app has spit back an error. IIRC, the old Apple II’s processed it correctly… but math has changed. And so have programming/scripting languages, btw…