-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib_showcase.fun
More file actions
executable file
·90 lines (82 loc) · 2.32 KB
/
stdlib_showcase.fun
File metadata and controls
executable file
·90 lines (82 loc) · 2.32 KB
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
#!/usr/bin/env fun
/*
* Standard library showcase example for arrays, strings, math, ranges, and base64.
*/
#include <arrays.fun>
#include <strings.fun>
#include <math.fun>
#include <utils/range.fun>
#include <encoding/base64.fun>
print("=== Arrays ===")
arr = [1,2,2,3,4]
print(join(array_slice(arr, 1, 3), ",")) // "2,2,3"
print(join(array_reverse(arr), ",")) // "4,3,2,2,1"
print(array_index_of(arr, 3)) // 3's index
print(array_contains(arr, 5)) // 0
print(join(array_unique(arr), ",")) // "1,2,3,4"
print(join(array_flatten1([[1,2],[3],[4,5]]), ",")) // "1,2,3,4,5"
print("=== Strings ===")
s = " Hello World \n"
print("["+str_trim(s)+"]") // "[Hello World]"
print(str_starts_with("foobar", "foo")) // 1
print(str_ends_with("foobar", "bar")) // 1
parts = str_split("a,b,c", ",")
print(join(parts, "|")) // "a|b|c"
print(str_replace_all("banana", "na", "NA")) // "baNANA"
print(str_to_lower("FUN")) // "fun"
print(str_to_upper("Fun")) // "FUN"
print(str_repeat("ha", 3)) // "hahaha"
print("=== Math ===")
print(abs(-5)) // 5
print(clamp(42, 0, 10)) // 10
print(gcd(54, 24)) // 6
print(lcm(21, 6)) // 42
print(powi(3, 5)) // 243
print(array_min([9,2,8,3])) // 2
print(array_max([9,2,8,3])) // 9
print(min3(3,1,2)) // 1
print(max3(3,1,2)) // 3
print("=== Range ===")
print(join(range(5), ",")) // "0,1,2,3,4"
print(join(range2(3, 8), ",")) // "3,4,5,6,7"
print(join(range3(10, 0, -3), ",")) // "10,7,4,1"
print("=== Base64 ===")
bytes = [0x48,0x65,0x6c,0x6c,0x6f] // "Hello"
b64 = b64_encode_bytes(bytes)
print(b64) // "SGVsbG8="
print(join(b64_decode_to_bytes(b64), ",")) // "72,101,108,108,111"
/* Expected output:
=== Arrays ===
2,2,3
4,3,2,2,1
3
0
1,2,3,4
1,2,3,4,5
=== Strings ===
[Hello World]
1
1
a|b|c
baNANA
fun
FUN
hahaha
=== Math ===
5
10
6
42
243
2
9
1
3
=== Range ===
0,1,2,3,4
3,4,5,6,7
10,7,4,1
=== Base64 ===
SGVsbG8=
72,101,108,108,111
*/