Illustrates lazy evaluation by creating a custom 'Repeat' function that executes a code block (passed with `ByExpr`) a specified number of times.

ID: 1175

				
					using uCalcSoftware;

var uc = new uCalc();

static void MyRepeat(uCalc.Callback cb) {
   var count = cb.ArgInt32(1);
   var action = cb.ArgExpr(2);

   for (int i = 1; i <= count; i++) {
      action.Execute(); // Evaluate the passed-in expression
   }
}


// Define a variable that our action will modify
uc.DefineVariable("counter = 0");

// Define the function. The 'action' parameter is marked with ByExpr
// to ensure it's passed as an unevaluated expression object.
uc.DefineFunction("Repeat(count As Int, ByExpr action)", MyRepeat);

// Call the custom Repeat function. The expression 'counter++' is not
// evaluated here; it's passed to the callback to be executed in a loop.
uc.Eval("Repeat(5, counter++)");

// Verify the side effect
Console.WriteLine($"Final counter value: {uc.Eval("counter")}");
				
			
Final counter value: 5
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyRepeat(uCalcBase::Callback cb) {
   auto count = cb.ArgInt32(1);
   auto action = cb.ArgExpr(2);

   for (int i = 1; i <= count; i++) {
      action.Execute(); // Evaluate the passed-in expression
   }
}

int main() {
   uCalc uc;
   // Define a variable that our action will modify
   uc.DefineVariable("counter = 0");

   // Define the function. The 'action' parameter is marked with ByExpr
   // to ensure it's passed as an unevaluated expression object.
   uc.DefineFunction("Repeat(count As Int, ByExpr action)", MyRepeat);

   // Call the custom Repeat function. The expression 'counter++' is not
   // evaluated here; it's passed to the callback to be executed in a loop.
   uc.Eval("Repeat(5, counter++)");

   // Verify the side effect
   cout << "Final counter value: " << uc.Eval("counter") << endl;
}
				
			
Final counter value: 5
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyRepeat(ByVal cb As uCalc.Callback)
      Dim count = cb.ArgInt32(1)
      Dim action = cb.ArgExpr(2)
      
      For i  As Integer = 1 To count
         action.Execute() '// Evaluate the passed-in expression
      Next
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define a variable that our action will modify
      uc.DefineVariable("counter = 0")
      
      '// Define the function. The 'action' parameter is marked with ByExpr
      '// to ensure it's passed as an unevaluated expression object.
      uc.DefineFunction("Repeat(count As Int, ByExpr action)", AddressOf MyRepeat)
      
      '// Call the custom Repeat function. The expression 'counter++' is not
      '// evaluated here; it's passed to the callback to be executed in a loop.
      uc.Eval("Repeat(5, counter++)")
      
      '// Verify the side effect
      Console.WriteLine($"Final counter value: {uc.Eval("counter")}")
   End Sub
End Module
				
			
Final counter value: 5