nenu-lang / loops
StableLoops
- Use loop collection as value for one binding or loop collection as key, value for two.
- continue skips the rest of the current iteration; break exits the nearest loop.
- Do not structurally mutate the collection being iterated.
Looping An Array
code
1number total = 023loop expenses as expense {4 total = total + expense.amount5}The one-binding form receives each element. The two-binding form receives the numeric index first and the element second. Both bindings are local to the loop body.
Looping A Map
code
1number room_total = 023loop balances.entries() as member_id, amount {4 room_total = room_total + amount5}For maps, the first binding is the string key and the second is the typed value. entries() keeps the same map value while making the key-and-value intent explicit.
Continue And Break
code
1number open_total = 023loop expenses as expense {4 if expense.status == ExpenseStatus.Settled {5 continue6 }78 if open_total >= 500 {9 break10 }1112 open_total = open_total + expense.amount13}continueskips statements below it and begins the next iteration.breakexits the nearest enclosing loop.- Both keywords are only valid inside a loop.
Choose A Loop Or Collection Method
| Goal | Prefer |
|---|---|
| Produce a filtered array | filter |
| Project every item to another value | collect |
| Check any or all items | any or all |
| Accumulate a total or perform several statements per item | loop |
| Need early exit | loop with break |
| Need to skip selected items while doing other work | loop with continue |
Mutation Safety
code
1Expense[] open_expenses = []23loop expenses as expense {4 if expense.status != ExpenseStatus.Settled {5 open_expenses.push(expense)6 }7}Common Mistakes
| Wrong | Correct |
|---|---|
for expense in expenses | loop expenses as expense |
foreach expenses | loop expenses as expense |
while condition | Use a collection loop or restructure the workflow. |
break outside a loop | Return, throw, or use a conditional as appropriate. |
