The Power of Symbiosis: Fusing Math and Functions
Mathematical Expressions and Dynamic Functions are not isolated tools; they are designed to work together seamlessly within a single Computation String. This interaction is the cornerstone of the engine's power, allowing you to build sophisticated logic that can process, calculate, and format data in one fluid, highly readable operation.
The core principle is simple: the result of any part of the string can be used as an input for another part. The engine automatically handles the order of execution, resolving the innermost components first and passing their results outwards. This creates a powerful data-flow pipeline within a single line of text.
Pattern 1: Using Math Expressions inside Dynamic Functions
This is the most common pattern for data transformation. You perform a calculation and immediately use its result as an argument for a dynamic function. This is perfect for formatting calculated values, making decisions based on them, or manipulating them as strings.
Use Case: Converting and Formatting a Temperature
Imagine operand a holds a temperature in Celsius, and you need to display it in Fahrenheit with a descriptive label for a dashboard or log message.
Computation String: $format('Temperature is %.1f°F', a * 1.8 + 32)
How it works:
Innermost Evaluation (Math): The engine first identifies the mathematical expression a * 1.8 + 32. If a is 25, the result is 77.
Argument Substitution: This numeric result, 77, is then passed as the second argument to the $format function.
Outermost Evaluation (Function): The function is now effectively $format('Temperature is %.1f°F', 77).
Final Output: The formatted string: 'Temperature is 77.0°F'.
Pattern 2: Using Dynamic Functions inside Math Expressions
Conversely, you can use the output of a dynamic function as a value in a mathematical calculation. This is extremely useful when you need to extract a number from a text payload before using it in a formula.
Use Case: Calculating Flow Rate from a JSON Message
Imagine operand b contains a JSON string from a flow meter, like {"reading": {"value": 50.7, "unit": "L/min"}}, and you need to scale this value to Liters per second (L/s).
Computation String: $parse('json', b, '$.reading.value') / 60
How it works:
Innermost Evaluation (Function): The engine first executes the dynamic function $parse('json', b, '$.reading.value').
Value Extraction: This function extracts the numeric value 50.7 from the JSON payload.
Outermost Evaluation (Math): This result, 50.7, is then used in the mathematical expression, which becomes 50.7 / 60.
Final Output: The final result of the entire operation is the number 0.845.
Advanced Symbiosis: Combining Patterns
The true power emerges when you combine these patterns, creating a multi-stage data pipeline in a single expression.
Use Case: Conditional Logic on a Parsed and Scaled Value
Let's expand the previous example. Now, you want to check if the calculated flow rate in L/s is within a safe operating range (e.g., between 0.5 and 1.5 L/s).
Computation String: inrange(0.5, $parse('json', b, '$.reading.value') / 60, 1.5)
How it works:
Innermost (Function): $parse('json', b, '$.reading.value') is resolved to 50.7.
Next Level (Math): The expression becomes 50.7 / 60, which is resolved to 0.845.
Outermost (Function): The inrange function is now evaluated as inrange(0.5, 0.845, 1.5).
Final Output: Since 0.845 is between 0.5 and 1.5, the function returns 1.0 (true).
This symbiotic relationship is the key to creating powerful, single-line operations that would otherwise require multiple steps, intermediate variables, or external logic. It allows for concise, readable, and highly efficient data processing directly at the edge.
Last updated