I use the MOD function quite a bit, but KM's MOD function is based on 0 to n-1, while I normally need it to deal with values from 1 to n instead. This results in a lot of extra coding and debugging.
Is it possible to introduce a new function perhaps called MOD1 ("MOD1" means "MOD with a base of 1") which works exactly like MOD but uses the range 1..n instead of 0..n-1? This would simplify my life.
E.g.,
Column 1
Column 2
1 MOD 3 => 1
1 MOD1 3 => 1
2 MOD 3 => 2
2 MOD1 3 => 2
3 MOD 3 => 0
3 MOD1 3 => 3
4 MOD 3 =>1
4 MOD1 3 => 1
There are a few languages which seem to use 1 as the base for MOD, like APL, Lua, R and MATLAB.
I see now that "MOD1" isn't the best name for it, because the "1" looks like an operand, which is confusing. I'm trying to come up with other words that would look nice. The operation that we call MOD is sometimes also called "circular increment" or "wrapping increment." Therefore perhaps a 1-based wrapping operator could be called "WRAP".
E.g., 4 WRAP 3 => 1
E.g., 4 CIRCLE 3 => 1
After pondering this, I think WRAP would be the best operator name for a 1-based MOD.
Are there any other KM users here who would like to see a 1-based MOD operator? Or do you usually need MOD to work with 0..n-1?
Yes, I agree! That's why I wanted it renamed to WRAP, short for "wrapping increment." It's not MOD. This is just semantics. I'm sure you understand what I want, because I made it very clear. And I asked whether people use 0-based or 1-based wrapping calculations, and nobody has replied.
I retract my claim about other languages using 1-based MOD. Sorry.
True, a couple of extra millisecond usually makes no human perceivable difference. Ease of reading is much more important. And I must admit that for me, as someone not yet having gone the paces learning myself the boolean operators, simple arithmetic just reads much more fluently.
To me this calculation feels like an operation just straight doing the thing; instead of having to check what was done, possibly correcting it. But I understand that the IF statement (or boolean operations) is possibly the more conventional approach
I'm aware of the ternary operator, for sure, but doesn't that mess prove my case exactly? I don't want to be encoding and then debugging long calculations like that when it could look as simple as this:
Of course, I know 90% of my requests are not going to be implemented, but I can still ask.
We're just showing two different approaches to a common problem:
Construct what you need from primitives
Have built-in function to do a (relatively) specific thing
Other languages let us make our own functions within our scripts using primitives -- KM doesn't, though we can spoof them with favourites and/or routines and sub macros.
I'm just not sure that what you want is generally useful enough that it warrants its own built-in function.
I whipped this together pretty quickly, so I'm not 100% positive it's foolproof, but it seems to work in testing. To use, drop the zip file (do not unzip!) on the Keyboard Maestro dock icon, then relaunch the editor and the engine.
Then just add a new action to a macro—if you're using the Actions list, you'll see the WRAP Function action in the Third Party Plug Ins section of the list. If you're using Edit → Insert Action → By Name (as you should be ), just type WRAP and it should show up in the list.
Requisite disclaimer: Claude helped me get the modified MOD working in Perl.
I cannot imagine I would add an inbuilt operator for MOD1, as it's not really a standard sort of thing. It's possible that I will add some sort of ability to add custom functions, though I imagine that would have some horrible complexities (for example - are they a kind of preference, independent of the macros, but then they can't be used with shared macros, or macro syncing, or are they some sort of new style of macros, but how on earth would that work, it certainly couldn't be made up of actions because calculations have to be instant results.
Hmm. Everything is always more complex than you think it will be.