omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / loops-foreach.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | note the collection: names separated by newlines |
| 8 | text fruit "apple\nbanana\ncherry\ndate\nelderberry\n" |
| 9 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 10 | fn emit a view uchar * to void |
| 11 | o = ulong 0 |
| 12 | while lt o len a bound 4096 |
| 13 | k = write 1 a o sub len a o |
| 14 | if le k 0 |
| 15 | exit 1 |
| 16 | end |
| 17 | o = add o ulong k |
| 18 | end |
| 19 | end |
| 20 | note foreach visits every newline-terminated element of the collection in order |
| 21 | fn main to int |
| 22 | s = ulong 0 |
| 23 | for i ulong len fruit |
| 24 | if eq get fruit i 10 |
| 25 | emit part fruit s add sub i s 1 |
| 26 | s = add i 1 |
| 27 | end |
| 28 | end |
| 29 | ret 0 |
| 30 | end |
| 31 | </syntaxhighlight> |
| 32 | {{out}} |
| 33 | <pre> |
| 34 | apple |
| 35 | banana |
| 36 | cherry |
| 37 | date |
| 38 | elderberry |
| 39 | </pre> |