1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
import {
Ok,
Error,
List,
BitString,
UtfCodepoint,
toBitString,
stringBits,
} from "./gleam.js";
import {
CompileError as RegexCompileError,
Match as RegexMatch,
} from "./gleam/regex.js";
import { Some, None } from "./gleam/option.js";
const Nil = undefined;
export function identity(x) {
return x;
}
export function parse_int(value) {
if (/^[-+]?(\d+)$/.test(value)) {
return new Ok(parseInt(value));
} else {
return new Error(Nil);
}
}
export function parse_float(value) {
if (/^[-+]?(\d+)\.(\d+)$/.test(value)) {
return new Ok(parseFloat(value));
} else {
return new Error(Nil);
}
}
export function to_string(term) {
return term.toString();
}
export function float_to_string(float) {
let string = float.toString();
if (string.indexOf(".") >= 0) {
return string;
} else {
return string + ".0";
}
}
export function int_to_base_string(int, base) {
return int.toString(base);
}
export function string_replace(string, target, substitute) {
return string.replaceAll(target, substitute);
}
export function string_reverse(string) {
return [...string].reverse().join("");
}
export function string_length(string) {
let iterator = graphemes_iterator(string);
if (iterator) {
let i = 0;
for (let _ of iterator) {
i++;
}
return i;
} else {
return string.match(/./gu).length;
}
}
function graphemes_iterator(string) {
if (Intl && Intl.Segmenter) {
return new Intl.Segmenter("en-gb").segment(string)[Symbol.iterator]();
}
}
export function pop_grapheme(string) {
let first;
let iterator = graphemes_iterator(string);
if (iterator) {
first = iterator.next().value?.segment;
} else {
first = string.match(/./u)?.[0];
}
if (first) {
return new Ok([first, string.slice(first.length)]);
} else {
return new Error(Nil);
}
}
export function lowercase(string) {
return string.toLowerCase();
}
export function uppercase(string) {
return string.toUpperCase();
}
export function less_than(a, b) {
return a < b;
}
export function add(a, b) {
return a + b;
}
export function equal(a, b) {
return a === b;
}
export function split(xs, pattern) {
return List.fromArray(xs.split(pattern));
}
export function join(xs) {
return xs.toArray().join("");
}
export function length(data) {
return data.length;
}
export function slice_string(string, from, length) {
return string.slice(from, from + length);
}
export function crop_string(string, substring) {
return string.substring(string.indexOf(substring));
}
export function index_of(haystack, needle) {
return haystack.indexOf(needle) | 0;
}
export function starts_with(haystack, needle) {
return haystack.startsWith(needle);
}
export function ends_with(haystack, needle) {
return haystack.endsWith(needle);
}
export function split_once(haystack, needle) {
let index = haystack.indexOf(needle);
if (index >= 0) {
let before = haystack.slice(0, index);
let after = haystack.slice(index + needle.length);
return new Ok([before, after]);
} else {
return new Error(Nil);
}
}
export function trim(string) {
return string.trim();
}
export function trim_left(string) {
return string.trimLeft();
}
export function trim_right(string) {
return string.trimRight();
}
export function bit_string_from_string(string) {
return new toBitString([stringBits(string)]);
}
export function bit_string_concat(bit_strings) {
return toBitString(bit_strings.toArray().map((b) => b.buffer));
}
export function log(term) {
console.log(term);
}
export function crash(message) {
throw new globalThis.Error(message);
}
export function bit_string_to_string(bit_string) {
try {
let decoder = new TextDecoder("utf-8", { fatal: true });
return new Ok(decoder.decode(bit_string.buffer));
} catch (_error) {
return new Error(undefined);
}
}
export function print(string) {
if (typeof process === "object") {
process.stdout.write(string); // We can write without a trailing newline
} else {
console.log(string); // We're in a browser. Newlines are mandated
}
}
export function ceiling(float) {
return Math.ceil(float);
}
export function floor(float) {
return Math.floor(float);
}
export function round(float) {
return Math.round(float);
}
export function truncate(float) {
return Math.trunc(float);
}
export function power(base, exponent) {
return Math.pow(base, exponent);
}
export function bit_string_slice(bits, position, length) {
let start = Math.min(position, position + length);
let end = Math.max(position, position + length);
if (start < 0 || end > bits.length) return new Error(Nil);
let buffer = new Uint8Array(bits.buffer.buffer, start, Math.abs(length));
return new Ok(new BitString(buffer));
}
export function codepoint(int) {
return new UtfCodepoint(int);
}
export function regex_check(regex, string) {
return regex.test(string);
}
export function compile_regex(pattern, options) {
try {
let flags = "gu";
if (options.case_insensitive) flags += "i";
if (options.multi_line) flags += "m";
return new Ok(new RegExp(pattern, flags));
} catch (error) {
let number = (error.columnNumber || 0) | 0;
return new Error(new RegexCompileError(error.message, number));
}
}
export function regex_scan(regex, string) {
let matches = Array.from(string.matchAll(regex)).map((match) => {
let content = match.shift();
let submatches = match.map((x) => (x ? new Some(x) : new None()));
return new RegexMatch(content, List.fromArray(submatches));
});
return List.fromArray(matches);
}
export function map_hash(obj) {
return JSON.stringify(obj);
}
export function new_map() {
return new Map();
}
export function map_size(map) {
return map.size;
}
export function map_to_list(map) {
return List.fromArray([...map.values()]);
}
export function map_from_list(list) {
return new Map(list.toArray().map(([a, b]) => [map_hash(a), [a, b]]));
}
export function map_has_key(k, map) {
return map.has(map_hash(k));
}
export function map_remove(k, map) {
const result = new Map(map);
result.delete(map_hash(k));
return result;
}
export function map_filter(f, map) {
const result = new Map();
map.forEach(([key, value], hash) => {
if (f(key)) {
result.set(hash, [key, value]);
}
})
return result;
}
export function map_get(from, get) {
const entry = from.get(map_hash(get));
if (entry) {
return new Ok(entry[1]); // [0] is the key, [1] is the value
} else {
return new Error(Nil);
}
}
export function map_insert(key, value, map) {
return new Map(map).set(map_hash(key), [key, value]);
}
export function map_keys(map) {
return List.fromArray([...map.values()].map(([key, value]) => key));
}
export function map_values(map) {
return List.fromArray([...map.values()].map(([key, value]) => value));
}
export function map_map_values(fn, map) {
const result = new Map();
map.forEach(([key, value], hash) => result.set(hash, [key, fn(key, value)]));
return result;
}
export function map_merge(into, merge) {
return new Map([...into, ...merge]);
}
export function map_take(keys, map) {
const result = new Map();
keys.toArray().forEach(key => {
const hash = map_hash(key);
const keyValue = map.get(hash);
if (keyValue !== undefined) {
result.set(hash, keyValue);
}
});
return result;
}
|