Nested Functions and Expressions

The true power and flexibility of the Computation String is revealed in its ability to handle deeply nested logic. You are not limited to a single layer of interaction between math and functions. Any argument of any function can be another function or a complete mathematical expression.

The engine resolves these complex strings by following a strict "inside-out" evaluation order, ensuring that the innermost calculations are performed first.


The "Inside-Out" Evaluation Principle

When the engine encounters a nested Computation String, it does not simply read it from left to right. Instead, it identifies the most deeply nested, complete set of parentheses () and evaluates that segment first. The result of that evaluation then replaces the original segment, and the process repeats until the entire string is resolved.


Practical Examples of Nesting

Example 1: Function Inside a Function

This is useful for pre-processing a value before using it in another function.

Scenario: You receive a comma-separated string in operand a ('data,123,end') and an index in operand b (1). You want to extract the element at that index and prepend a label to it.

Computation String: $concat('Value: ', $split(a, ',', b))

Evaluation Steps:

  1. Innermost: $split(a, ',', b) is evaluated first. It splits 'data,123,end' by , and returns the element at index 1, which is '123'.

  2. Outermost: The string is now effectively $concat('Value: ', '123').

Final Result: 'Value: 123'.

Example 2: Expression Containing a Function

This pattern is common when you need to extract a value and then immediately use it in a calculation.

Scenario: A JSON payload in operand a contains {"level": 85}. You need to calculate the remaining capacity as a percentage.

Computation String: 100 - $parse('json', a, '$.level')

Evaluation Steps:

  1. Innermost: $parse('json', a, '$.level') is executed, returning the numeric value 85.

  2. Outermost: The string becomes a simple math expression: 100 - 85.

Final Result: 15.

Example 3: The Ultimate Combination

This example demonstrates multiple levels of nesting, combining function calls and arithmetic within the arguments of an outer function.

Scenario: You receive a JSON payload in operand a ({"value": 50}). You want to create a string that shows the original value and a calculated value (original value + 10).

Computation String: $concat($parse('json', a, '$.value'), ':', $parse('json', a, '$.value') + 10)

Evaluation Steps:

  1. The engine sees $concat with three arguments. It must resolve each argument before it can perform the concatenation.

  2. Argument 1: $parse('json', a, '$.value') is resolved. Result: 50.

  3. Argument 2: ' : ' is a simple literal. Result: ' : '.

  4. Argument 3: $parse('json', a, '$.value') + 10 is a sub-expression.

  • The engine first resolves the function inside: $parse('json', a, '$.value') → 50.

  • Then it performs the math: 50 + 10 → 60.

  1. Final Assembly: The engine now executes the top-level function with the resolved arguments: $concat(50, ' : ', 60).

Final Result: '50 : 60'.

This recursive capability allows you to build incredibly powerful and concise data transformation logic directly within a single Computation String.

Last updated