Math slots in Hyperlambda
This project provides math functions to Magic. More specifically, it provides the following slots.
- [math.multiply] - Multiplication
- [math.divide] - Division
- [math.add] - Addition
- [math.subtract] - Subtraction
- [math.modulo] - Modulo
- [math.decrement] - Decrements a node’s value, optionally by [step], defaulting to 1
- [math.increment] - Increments a node’s value, optionally by [step], defaulting to 1
All of the above besides the two last slots can be given any number of arguments, including as its value, and will treat the first argument as the “base”, and performing the rest of the arguments self assigning the base as it proceeds. For instance, the following code will first divide 100 by 4, then divide that result by 5 again, resulting in 5.
math.divide:int:100 :int:4 :int:5
The value of the above [math.divide] node after evaluating the above Hyperlambda will be 5. All of the above slots will also evaluate the children collection as a lambda, before starting the actual math function, allowing you to recursively raise signals to retrieve values that are supposed to be mathematically handled somehow. This allows you to recursively nest math operations, such as for instance.
.one:int:5 .two:int:2 math.multiply .:int:3 math.add get-value:x:@.one get-value:x:@.two
The above of course will first add 5 and 2, then multiple the result of that with 3, resulting in 21.
Incrementing and decrementing values
The above [math.increment] and [math.decrement] slots, will instead of yielding a result, inline modify the value of the node(s) it is pointing to, assuming its value is an expression. In addition these two slots can take an optional “step” argument, allowing you to declare how much the incrementation/decrementation process should add/reduce the original node’s value by. Below is an example that decrements the value found in its expression by 2.
.value:int:5 math.decrement:x:- .:int:2
After executing the above, the result of [.value] will be 3. The default “step” value if ommitted will be 1. Below is an example.
Notice - You can use any slot invocation to retrieve the step value for the increment/decrement slots, including for instance an invocation to [get-value], or your custom slots. This is dues to that the first argument supplied to these slots will be assumed to be the “step” value you want.
The modulo slot divides its argument(s) by its base, and returns the remainder.
.int:17 math.modulo:x:- .:int:10
The above results in 7.
The source code for this repository can be found at github.com/polterguy/magic.lambda.math, and you can provide feedback, provide bug reports, etc at the same place.