interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / determine-if-a-string-is-collapsible / determine-if-a-string-is-collapsible.om

72 lines · 1576 bytes Raw Blame History
1note Rosetta Code task "Determine if a string is collapsible": https://rosettacode.org/wiki/Determine_if_a_string_is_collapsible
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text lq "<<<"
6text rq ">>>"
7text tlen " length "
8text c0 "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
9text c1 "..1111111111111111111111111111111111111111111111111111111111111117777888"
10text c2 "I never give 'em hell, I just tell the truth, and they think it's hell. "
11text c3 " --- Harry S Truman "
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38fn line a view uchar * to void
39emit lq
40emit a
41emit rq
42emit tlen
43emitu len a
44emit nl
45end
46note collapse keeps a byte only when it differs from the byte before it
47fn collapse a view uchar * to void
48o = new uchar 256 0
49n = ulong 0
50for i ulong len a
51c = get a i
52k = true
53if gt i 0
54k = ne c get a sub i 1
55end
56if k
57put o n c
58n = add n 1
59end
60end
61line a
62line part o 0 n
63end
64fn main to int
65note the empty string, as a slice of length 0
66collapse part c0 0 0
67collapse c0
68collapse c1
69collapse c2
70collapse c3
71ret 0
72end