Demonstrates using `RewindOnChange(true)` to perform recursive-style transformations, such as creating a variadic `AddUp` function.
ID: 346
using uCalcSoftware;
var uc = new uCalc();
var t = uc.ExpressionTransformer;
// Base case: a single argument
t.FromTo("AddUp({x})", "{x}");
// Recursive case: multiple arguments
// RewindOnChange(true) causes the transformer to re-scan the string after a replacement.
// This allows AddUp(1,2,3) -> (1 + AddUp(2,3)) -> (1 + (2 + AddUp(3))) -> (1 + (2 + 3))
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))").RewindOnChange = true;
Console.WriteLine("Input: AddUp(1, 2, 3, 4)");
Console.WriteLine($"Result: {uc.Eval("AddUp(1, 2, 3, 4)")}");
Input: AddUp(1, 2, 3, 4)
Result: 10 using uCalcSoftware; var uc = new uCalc(); var t = uc.ExpressionTransformer; // Base case: a single argument t.FromTo("AddUp({x})", "{x}"); // Recursive case: multiple arguments // RewindOnChange(true) causes the transformer to re-scan the string after a replacement. // This allows AddUp(1,2,3) -> (1 + AddUp(2,3)) -> (1 + (2 + AddUp(3))) -> (1 + (2 + 3)) t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))").RewindOnChange = true; Console.WriteLine("Input: AddUp(1, 2, 3, 4)"); Console.WriteLine($"Result: {uc.Eval("AddUp(1, 2, 3, 4)")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.ExpressionTransformer();
// Base case: a single argument
t.FromTo("AddUp({x})", "{x}");
// Recursive case: multiple arguments
// RewindOnChange(true) causes the transformer to re-scan the string after a replacement.
// This allows AddUp(1,2,3) -> (1 + AddUp(2,3)) -> (1 + (2 + AddUp(3))) -> (1 + (2 + 3))
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))").RewindOnChange(true);
cout << "Input: AddUp(1, 2, 3, 4)" << endl;
cout << "Result: " << uc.Eval("AddUp(1, 2, 3, 4)") << endl;
}
Input: AddUp(1, 2, 3, 4)
Result: 10 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.ExpressionTransformer(); // Base case: a single argument t.FromTo("AddUp({x})", "{x}"); // Recursive case: multiple arguments // RewindOnChange(true) causes the transformer to re-scan the string after a replacement. // This allows AddUp(1,2,3) -> (1 + AddUp(2,3)) -> (1 + (2 + AddUp(3))) -> (1 + (2 + 3)) t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))").RewindOnChange(true); cout << "Input: AddUp(1, 2, 3, 4)" << endl; cout << "Result: " << uc.Eval("AddUp(1, 2, 3, 4)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.ExpressionTransformer
'// Base case: a single argument
t.FromTo("AddUp({x})", "{x}")
'// Recursive case: multiple arguments
'// RewindOnChange(true) causes the transformer to re-scan the string after a replacement.
'// This allows AddUp(1,2,3) -> (1 + AddUp(2,3)) -> (1 + (2 + AddUp(3))) -> (1 + (2 + 3))
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))").RewindOnChange = true
Console.WriteLine("Input: AddUp(1, 2, 3, 4)")
Console.WriteLine($"Result: {uc.Eval("AddUp(1, 2, 3, 4)")}")
End Sub
End Module
Input: AddUp(1, 2, 3, 4)
Result: 10 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.ExpressionTransformer '// Base case: a single argument t.FromTo("AddUp({x})", "{x}") '// Recursive case: multiple arguments '// RewindOnChange(true) causes the transformer to re-scan the string after a replacement. '// This allows AddUp(1,2,3) -> (1 + AddUp(2,3)) -> (1 + (2 + AddUp(3))) -> (1 + (2 + 3)) t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))").RewindOnChange = true Console.WriteLine("Input: AddUp(1, 2, 3, 4)") Console.WriteLine($"Result: {uc.Eval("AddUp(1, 2, 3, 4)")}") End Sub End Module