exprv4:tutorials:complete_examples



Complete Examples

This chapter combines the ideas from the previous tutorials into complete scripts.

The examples use:

  • Variables and assignment.
  • Operators and conditions.
  • Control flow.
  • Functions and methods.
  • Classes and objects.
  • `set` for local helper values.
  • `none()`, `nan()`, and `error(…)` handling.

Use these examples as patterns for your own scripts.


Tool Radius and Area

This example calculates tool radius and cross-section area from a diameter.

function ToolRadius(diameter)
{
    if(!diameter.is_num_notnan() || diameter <= 0)
    {
        error('Tool diameter must be a positive number');
    };
 
    return diameter / 2;
}
 
function ToolArea(diameter)
{
    set radius = ToolRadius(diameter);
    return pi() * radius ** 2;
}
 
diameter = 6;
radius = ToolRadius(diameter);
area = ToolArea(diameter);
 
'Tool radius=' + radius + ', area=' + area;

What this shows:

  • Use functions to name reusable calculations.
  • Use `set` for temporary helper values inside a function.
  • Validate inputs before using them in important calculations.
  • Use `error(…)` when bad input should stop the script.

Probe Status

This example converts a probe error value into a status string.

function ProbeStatus(errorValue)
{
    if(errorValue.is_none())
    {
        return 'missing';
    };
 
    if(!errorValue.is_num_notnan())
    {
        return 'invalid';
    };
 
    if(errorValue <= 0.01)
    {
        return 'excellent';
    }
    else if(errorValue <= 0.05)
    {
        return 'acceptable';
    }
    else if(errorValue <= 0.10)
    {
        return 'warning';
    };
 
    return 'failed';
}
 
probeError = 0.03;
status = ProbeStatus(probeError);
 
status;

The final value is `'acceptable'`.

What this shows:

  • Use `none()` for missing values.
  • Use `.is_num_notnan()` before numeric comparisons.
  • Use `else if` for ordered decisions.
  • Return clear status values instead of scattered logic.

Safe Division

This example returns `none()` instead of failing when division cannot be done.

function SafeDivide(a, b)
{
    if(!a.is_num_notnan() || !b.is_num_notnan())
    {
        return none();
    };
 
    if(b == 0)
    {
        return none();
    };
 
    return a / b;
}
 
result = SafeDivide(10, 2);
 
if(result.is_none())
{
    'division failed';
}
else
{
    'result=' + result;
};

The final value is `'result=5'`.

What this shows:

  • Use `none()` when a function has no usable result but the script can continue.
  • Check `none()` before using a value.
  • Keep error-handling behavior explicit.

Counter With loop

This example counts how many times a loop runs.

count = 0;
 
loop(5)
{
    count += 1;
};
 
count;

The final value is `5`.

The same pattern can be used for repeated calculations, repeated checks, or simple fixed-count automation steps.


Sum With for

This example adds numbers from `1` to `5`.

sum = 0;
 
for(i = 1; i <= 5; i += 1)
{
    sum += i;
};
 
sum;

The final value is `15`.

What this shows:

  • Use `for` when you need a counter.
  • Use compound assignment for simple updates.
  • Keep loop start, condition, and update in one place.

Skip Values With continue

This example adds only odd numbers.

sum = 0;
 
for(i = 1; i <= 6; i += 1)
{
    if(i % 2 == 0)
    {
        continue;
    };
 
    sum += i;
};
 
sum;

The final value is `9` because the script adds `1 + 3 + 5`.

What this shows:

  • Use `%` to test remainders.
  • Use `continue` to skip the rest of one loop iteration.
  • Keep the main work after guard checks.

Find First Matching Value

This example searches for the first value larger than a limit.

limit = 10;
found = none();
 
for(i = 1; i <= 20; i += 1)
{
    value = i * 3;
 
    if(value > limit)
    {
        found = value;
        break;
    };
};
 
found;

The final value is `12`.

What this shows:

  • Use `none()` as an initial “not found” value.
  • Use `break` to stop when the result is found.
  • Check the result later with `found.is_none()` if needed.

Tool Object

This example uses a class to keep tool data and tool calculations together.

class ToolInfo(number, diameter)
{
    Number = number;
    Diameter = diameter;
 
    function Radius()
    {
        return Diameter / 2;
    }
 
    function Description()
    {
        return 'T' + Number + ' D=' + Diameter + ' R=' + Radius();
    }
}
 
tool = ToolInfo(3, 8);
tool.Description();

The final value is `'T3 D=8 R=4'`.

What this shows:

  • Use a class when data and behavior belong together.
  • Store persistent object fields with normal assignment.
  • Define methods with `function` inside the class.
  • Call methods with dot syntax.

Probe Result Object

This example stores a probe result and provides helper methods for status and display text.

class ProbeResult(x, y, z, errorValue)
{
    X = x;
    Y = y;
    Z = z;
    Error = errorValue;
 
    function IsValid()
    {
        return Error.is_num_notnan() && Error <= 0.05;
    }
 
    function Status()
    {
        if(Error.is_none())
        {
            return 'missing';
        };
 
        if(!Error.is_num_notnan())
        {
            return 'invalid';
        };
 
        if(Error <= 0.05)
        {
            return 'ok';
        };
 
        return 'too large';
    }
 
    function Summary()
    {
        return 'X=' + X + ', Y=' + Y + ', Z=' + Z + ', status=' + Status();
    }
}
 
probe = ProbeResult(10, 20, -1.5, 0.03);
probe.Summary();

The final value is `'X=10, Y=20, Z=-1.5, status=ok'`.

What this shows:

  • Use object fields to keep related data together.
  • Use methods for logic that belongs to that data.
  • Use `.is_none()` and `.is_num_notnan()` before numeric decisions.

Average Calculator Object

This example accumulates values and calculates an average.

class Average()
{
    Count = 0;
    Sum = 0;
 
    function Add(value)
    {
        if(!value.is_num_notnan())
        {
            return false;
        };
 
        Count += 1;
        Sum += value;
        return true;
    }
 
    function Value()
    {
        if(Count == 0)
        {
            return none();
        };
 
        return Sum / Count;
    }
}
 
avg = Average();
avg.Add(10);
avg.Add(20);
avg.Add(30);
 
avg.Value();

The final value is `20`.

What this shows:

  • Use object fields for state that changes over time.
  • Use methods to protect updates with validation.
  • Return `none()` when no average is available.

String Cleanup

This example normalizes text input.

function NormalizeToolName(name)
{
    if(!name.is_string())
    {
        return none();
    };
 
    return name.trim().upper();
}
 
NormalizeToolName('  tool 3  ');

The final value is `'TOOL 3'`.

What this shows:

  • Strings have methods such as `.trim()` and `.upper()`.
  • Methods can be chained.
  • Return `none()` when input is not usable.

Simple Validation Before Workflow

This example validates several values before a workflow continues.

function RequireNumber(value, name)
{
    if(!value.is_num_notnan())
    {
        error(name + ' must be a valid number');
    };
 
    return value;
}
 
function RequirePositive(value, name)
{
    RequireNumber(value, name);
 
    if(value <= 0)
    {
        error(name + ' must be positive');
    };
 
    return value;
}
 
toolDiameter = RequirePositive(6, 'Tool diameter');
probeStep = RequirePositive(0.2, 'Probe step');
 
'Tool diameter=' + toolDiameter + ', probe step=' + probeStep;

What this shows:

  • Validate early.
  • Use clear error messages.
  • Split validation into small reusable functions.

For scripts that affect machine behavior:

  • Prefer clear names over short names.
  • Validate important values before using them.
  • Use `set` for temporary helper variables.
  • Use functions to name reusable logic.
  • Use classes when data and behavior belong together.
  • Use `none()` for optional missing results.
  • Use `error(…)` when the script should stop.
  • Add comments for why something is done, not for what obvious code already says.

Where To Go Next

You have finished the beginner tutorial path.

Next useful sections:

exprv4/tutorials/complete_examples.txt · Last modified: by andrej

Page Tools