Applies `RewindOnChange` to a default rule set to enable complex, multi-rule transformations for a custom `Average` function.
ID: 981
using uCalcSoftware;
var uc = new uCalc();
var t = uc.ExpressionTransformer;
// Enable rewind for all subsequent rules in this transformer.
t.DefaultRuleSet.SetRewindOnChange(true);
// Define the recursive rules.
t.FromTo("AddUp({x})", "{x}");
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))");
t.FromTo("ArgCount({x})", "1");
t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))");
// The main rule that combines the others.
t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})");
var expression = "Average(1, 2, 3, 4)";
Console.WriteLine($"Input: {expression}");
Console.WriteLine($"Transform: {t.Transform(expression)}");
Console.WriteLine($"Eval: {uc.Eval(expression)}");
Input: Average(1, 2, 3, 4)
Transform: (1 + (2 + (3 + 4))) / (1 + (1 + (1 + 1)))
Eval: 2.5 using uCalcSoftware; var uc = new uCalc(); var t = uc.ExpressionTransformer; // Enable rewind for all subsequent rules in this transformer. t.DefaultRuleSet.SetRewindOnChange(true); // Define the recursive rules. t.FromTo("AddUp({x})", "{x}"); t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))"); t.FromTo("ArgCount({x})", "1"); t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))"); // The main rule that combines the others. t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})"); var expression = "Average(1, 2, 3, 4)"; Console.WriteLine($"Input: {expression}"); Console.WriteLine($"Transform: {t.Transform(expression)}"); Console.WriteLine($"Eval: {uc.Eval(expression)}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
auto t = uc.ExpressionTransformer();
// Enable rewind for all subsequent rules in this transformer.
t.DefaultRuleSet().SetRewindOnChange(true);
// Define the recursive rules.
t.FromTo("AddUp({x})", "{x}");
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))");
t.FromTo("ArgCount({x})", "1");
t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))");
// The main rule that combines the others.
t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})");
auto expression = "Average(1, 2, 3, 4)";
cout << "Input: " << expression << endl;
cout << "Transform: " << t.Transform(expression) << endl;
cout << "Eval: " << uc.Eval(expression) << endl;
}
Input: Average(1, 2, 3, 4)
Transform: (1 + (2 + (3 + 4))) / (1 + (1 + (1 + 1)))
Eval: 2.5 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.ExpressionTransformer(); // Enable rewind for all subsequent rules in this transformer. t.DefaultRuleSet().SetRewindOnChange(true); // Define the recursive rules. t.FromTo("AddUp({x})", "{x}"); t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))"); t.FromTo("ArgCount({x})", "1"); t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))"); // The main rule that combines the others. t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})"); auto expression = "Average(1, 2, 3, 4)"; cout << "Input: " << expression << endl; cout << "Transform: " << t.Transform(expression) << endl; cout << "Eval: " << uc.Eval(expression) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t = uc.ExpressionTransformer
'// Enable rewind for all subsequent rules in this transformer.
t.DefaultRuleSet.SetRewindOnChange(true)
'// Define the recursive rules.
t.FromTo("AddUp({x})", "{x}")
t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))")
t.FromTo("ArgCount({x})", "1")
t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))")
'// The main rule that combines the others.
t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})")
Dim expression = "Average(1, 2, 3, 4)"
Console.WriteLine($"Input: {expression}")
Console.WriteLine($"Transform: {t.Transform(expression)}")
Console.WriteLine($"Eval: {uc.Eval(expression)}")
End Sub
End Module
Input: Average(1, 2, 3, 4)
Transform: (1 + (2 + (3 + 4))) / (1 + (1 + (1 + 1)))
Eval: 2.5 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t = uc.ExpressionTransformer '// Enable rewind for all subsequent rules in this transformer. t.DefaultRuleSet.SetRewindOnChange(true) '// Define the recursive rules. t.FromTo("AddUp({x})", "{x}") t.FromTo("AddUp({x}, {y})", "({x} + AddUp({y}))") t.FromTo("ArgCount({x})", "1") t.FromTo("ArgCount({x}, {y})", "(1 + ArgCount({y}))") '// The main rule that combines the others. t.FromTo("Average({x}, {y})", "AddUp({x}, {y}) / ArgCount({x}, {y})") Dim expression = "Average(1, 2, 3, 4)" Console.WriteLine($"Input: {expression}") Console.WriteLine($"Transform: {t.Transform(expression)}") Console.WriteLine($"Eval: {uc.Eval(expression)}") End Sub End Module