omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / loops-foreach / loops-foreach.om
| 1 | note Rosetta Code task "Loops/Foreach": https://rosettacode.org/wiki/Loops/Foreach |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | note the collection: names separated by newlines |
| 6 | text fruit "apple\nbanana\ncherry\ndate\nelderberry\n" |
| 7 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 8 | fn emit a view uchar * to void |
| 9 | o = ulong 0 |
| 10 | while lt o len a bound 4096 |
| 11 | k = write 1 a o sub len a o |
| 12 | if le k 0 |
| 13 | exit 1 |
| 14 | end |
| 15 | o = add o ulong k |
| 16 | end |
| 17 | end |
| 18 | note foreach visits every newline-terminated element of the collection in order |
| 19 | fn main to int |
| 20 | s = ulong 0 |
| 21 | for i ulong len fruit |
| 22 | if eq get fruit i 10 |
| 23 | emit part fruit s add sub i s 1 |
| 24 | s = add i 1 |
| 25 | end |
| 26 | end |
| 27 | ret 0 |
| 28 | end |