1
0
Fork 0
forked from fun/fun

For range() loops.

This commit is contained in:
Johannes Findeisen 2025-09-14 17:06:42 +02:00
commit 238a9a3bbe
2 changed files with 203 additions and 0 deletions

60
examples/for_range_test.fun Executable file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env fun
// for/range test: globals, nested loops, and a function using range
print("=== for/range test start ===")
// Basic: 0..4
for i in range(0, 5)
print(i) // expect: 0,1,2,3,4
// Using variables for bounds
number start = 2
number end = 6
for j in range(start, end)
print(j) // expect: 2,3,4,5
// Function that sums a half-open range [a, b)
fun sum_range(a, b)
number acc = 0
for k in range(a, b)
acc = acc + k
return acc
print(sum_range(1, 4)) // expect: 6 (1+2+3)
// Nested ranges
for x in range(1, 3)
for y in range(1, 3)
print(x + y) // expect: 2,3,3,4
print("=== for/range test end ===")
// for/range test: globals, nested loops, and a function using range
print("=== for/range test start ===")
// Basic: 0..4
for i in range(0, 5)
print(i) // expect: 0,1,2,3,4
// Using variables for bounds
number start = 2
number end = 6
for j in range(start, end)
print(j) // expect: 2,3,4,5
// Function that sums a half-open range [a, b)
fun sum_range(a, b)
number acc = 0
for k in range(a, b)
acc = acc + k
return acc
print(sum_range(1, 4)) // expect: 6 (1+2+3)
// Nested ranges
for x in range(1, 3)
for y in range(1, 3)
print(x + y) // expect: 2,3,3,4
print("=== for/range test end ===")