Creates a custom `IIf` function to demonstrate basic lazy evaluation. The division-by-zero error in the unevaluated branch is safely ignored.
ID: 1229
using uCalcSoftware;
var uc = new uCalc();
static void MyIIf(uCalc.Callback cb) {
var condition = cb.ArgBool(1);
var thenPart = cb.ArgExpr(2);
var elsePart = cb.ArgExpr(3);
if (condition) {
cb.Return(thenPart.Evaluate());
} else {
cb.Return(elsePart.Evaluate());
}
}
uc.DefineFunction("MyIIf(cond As Bool, ByExpr thenExpr, ByExpr elseExpr)", MyIIf);
// The 'else' branch contains 1/0, but since the condition is true,
// it is never evaluated.
Console.WriteLine(uc.Eval("MyIIf(10 > 5, 100, 1/0)"));
// The 'then' branch contains 1/0, but it is never evaluated.
Console.WriteLine(uc.Eval("MyIIf(10 < 5, 1/0, 200)"));
100
200 using uCalcSoftware; var uc = new uCalc(); static void MyIIf(uCalc.Callback cb) { var condition = cb.ArgBool(1); var thenPart = cb.ArgExpr(2); var elsePart = cb.ArgExpr(3); if (condition) { cb.Return(thenPart.Evaluate()); } else { cb.Return(elsePart.Evaluate()); } } uc.DefineFunction("MyIIf(cond As Bool, ByExpr thenExpr, ByExpr elseExpr)", MyIIf); // The 'else' branch contains 1/0, but since the condition is true, // it is never evaluated. Console.WriteLine(uc.Eval("MyIIf(10 > 5, 100, 1/0)")); // The 'then' branch contains 1/0, but it is never evaluated. Console.WriteLine(uc.Eval("MyIIf(10 < 5, 1/0, 200)"));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call MyIIf(uCalcBase::Callback cb) {
auto condition = cb.ArgBool(1);
auto thenPart = cb.ArgExpr(2);
auto elsePart = cb.ArgExpr(3);
if (condition) {
cb.Return(thenPart.Evaluate());
} else {
cb.Return(elsePart.Evaluate());
}
}
int main() {
uCalc uc;
uc.DefineFunction("MyIIf(cond As Bool, ByExpr thenExpr, ByExpr elseExpr)", MyIIf);
// The 'else' branch contains 1/0, but since the condition is true,
// it is never evaluated.
cout << uc.Eval("MyIIf(10 > 5, 100, 1/0)") << endl;
// The 'then' branch contains 1/0, but it is never evaluated.
cout << uc.Eval("MyIIf(10 < 5, 1/0, 200)") << endl;
}
100
200 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call MyIIf(uCalcBase::Callback cb) { auto condition = cb.ArgBool(1); auto thenPart = cb.ArgExpr(2); auto elsePart = cb.ArgExpr(3); if (condition) { cb.Return(thenPart.Evaluate()); } else { cb.Return(elsePart.Evaluate()); } } int main() { uCalc uc; uc.DefineFunction("MyIIf(cond As Bool, ByExpr thenExpr, ByExpr elseExpr)", MyIIf); // The 'else' branch contains 1/0, but since the condition is true, // it is never evaluated. cout << uc.Eval("MyIIf(10 > 5, 100, 1/0)") << endl; // The 'then' branch contains 1/0, but it is never evaluated. cout << uc.Eval("MyIIf(10 < 5, 1/0, 200)") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub MyIIf(ByVal cb As uCalc.Callback)
Dim condition = cb.ArgBool(1)
Dim thenPart = cb.ArgExpr(2)
Dim elsePart = cb.ArgExpr(3)
If condition Then
cb.Return(thenPart.Evaluate())
Else
cb.Return(elsePart.Evaluate())
End If
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineFunction("MyIIf(cond As Bool, ByExpr thenExpr, ByExpr elseExpr)", AddressOf MyIIf)
'// The 'else' branch contains 1/0, but since the condition is true,
'// it is never evaluated.
Console.WriteLine(uc.Eval("MyIIf(10 > 5, 100, 1/0)"))
'// The 'then' branch contains 1/0, but it is never evaluated.
Console.WriteLine(uc.Eval("MyIIf(10 < 5, 1/0, 200)"))
End Sub
End Module
100
200 Imports System Imports uCalcSoftware Public Module Program Public Sub MyIIf(ByVal cb As uCalc.Callback) Dim condition = cb.ArgBool(1) Dim thenPart = cb.ArgExpr(2) Dim elsePart = cb.ArgExpr(3) If condition Then cb.Return(thenPart.Evaluate()) Else cb.Return(elsePart.Evaluate()) End If End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineFunction("MyIIf(cond As Bool, ByExpr thenExpr, ByExpr elseExpr)", AddressOf MyIIf) '// The 'else' branch contains 1/0, but since the condition is true, '// it is never evaluated. Console.WriteLine(uc.Eval("MyIIf(10 > 5, 100, 1/0)")) '// The 'then' branch contains 1/0, but it is never evaluated. Console.WriteLine(uc.Eval("MyIIf(10 < 5, 1/0, 200)")) End Sub End Module