Skip to content

Commit d479bfa

Browse files
akoclaude
andcommitted
Fix math examples: rename module to MathTest, avoid RETURN inside loop
- Rename module from Math to MathTest (avoids conflict with reserved names) - Refactor IsPrime to use flag variable instead of RETURN inside WHILE loop (Mendix CE0068: end events cannot be placed inside a loop) - Update test file module references to match Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6946b44 commit d479bfa

2 files changed

Lines changed: 92 additions & 87 deletions

File tree

mdl-examples/doctype-tests/04-math-examples.mdl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
--
1212
-- ============================================================================
1313

14-
create module Math;
14+
create module MathTest;
1515

1616
/**
1717
* Determine if the input number is a prime number.
@@ -28,7 +28,7 @@ create module Math;
2828
* @param $Number The integer to test for primality
2929
* @returns true if the number is prime, false otherwise
3030
*/
31-
CREATE MICROFLOW Math.IsPrime (
31+
CREATE MICROFLOW MathTest.IsPrime (
3232
$Number: Integer
3333
) RETURNS Boolean AS $IsPrime
3434
FOLDER 'Math'
@@ -54,28 +54,32 @@ BEGIN
5454
-- Initialize trial division variables
5555
-- Start checking odd divisors from 3
5656
DECLARE $Divisor Integer = 3;
57+
DECLARE $IsPrime Boolean = true;
5758

5859
-- Only need to check up to sqrt(N)
5960
-- Mathematical insight: if N = a*b, one factor must be <= sqrt(N)
6061
DECLARE $MaxDivisor Integer = round(sqrt($Number));
6162

6263
-- Main trial division loop
63-
WHILE $Divisor <= $MaxDivisor
64+
-- Note: RETURN inside a loop is not allowed in Mendix (CE0068),
65+
-- so we use a flag variable and BREAK instead.
66+
WHILE $Divisor <= $MaxDivisor AND $IsPrime
6467
BEGIN
6568
-- Test if current divisor divides the number evenly
6669
IF $Number mod $Divisor = 0 THEN
67-
LOG INFO NODE 'Math' 'Found divisor: {1}, not prime' WITH ({1} = $Divisor);
68-
RETURN false;
70+
LOG INFO NODE 'Math' 'Found divisor: {1}, not prime' WITH ({1} = toString($Divisor));
71+
SET $IsPrime = false;
6972
END IF;
7073

7174
-- Increment by 2 to skip even numbers (optimization)
7275
SET $Divisor = $Divisor + 2;
7376
END WHILE;
7477

75-
-- No divisors found - number is prime
76-
LOG INFO NODE 'Math' 'Number {1} is prime' WITH ({1} = $Number);
78+
IF $IsPrime THEN
79+
LOG INFO NODE 'Math' 'Number {1} is prime' WITH ({1} = toString($Number));
80+
END IF;
7781

78-
RETURN true;
82+
RETURN $IsPrime;
7983
END;
8084
/
8185

@@ -93,7 +97,7 @@ END;
9397
* @param $N The position in the Fibonacci sequence (0-based index)
9498
* @returns The Nth Fibonacci number as a Long integer
9599
*/
96-
CREATE MICROFLOW Math.Fibonacci (
100+
CREATE MICROFLOW MathTest.Fibonacci (
97101
$N: Integer
98102
) RETURNS Long AS $Result
99103
FOLDER 'Math'
@@ -118,19 +122,20 @@ BEGIN
118122

119123
-- Initialize sliding window variables for iterative calculation
120124
-- Previous two values: F(i-2) and F(i-1)
125+
DECLARE $Result Long = 0;
121126
DECLARE $Previous2 Long = 0;
122127
DECLARE $Previous1 Long = 1;
123128
DECLARE $Current Long = 0;
124129
DECLARE $Counter Integer = 2;
125-
LOG INFO NODE 'Math' 'Calculating Fibonacci({1})' WITH ({1} = $N);
130+
LOG INFO NODE 'Math' 'Calculating Fibonacci({1})' WITH ({1} = toString($N));
126131

127132
-- Main iteration loop: Calculate F(2) through F(N)
128133
-- Uses sliding window: shift values after each calculation
129134
WHILE $Counter <= $N
130135
BEGIN
131136
-- Calculate current Fibonacci number using recurrence relation
132137
SET $Current = $Previous1 + $Previous2;
133-
LOG DEBUG NODE 'Math' 'F({1}) = {2}' WITH ({1} = $Counter, {2} = $Current);
138+
LOG DEBUG NODE 'Math' 'F({1}) = {2}' WITH ({1} = toString($Counter), {2} = toString($Current));
134139

135140
-- Slide the window: shift values for next iteration
136141
SET $Previous2 = $Previous1;
@@ -140,7 +145,7 @@ BEGIN
140145

141146
-- Store final result and return
142147
SET $Result = $Current;
143-
LOG INFO NODE 'Math' 'Fibonacci({1}) = {2}' WITH ({1} = $N, {2} = $Result);
148+
LOG INFO NODE 'Math' 'Fibonacci({1}) = {2}' WITH ({1} = toString($N), {2} = toString($Result));
144149
RETURN $Result;
145150
END;
146151
/

0 commit comments

Comments
 (0)