-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.azor
167 lines (138 loc) · 4.11 KB
/
stdlib.azor
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
map{A, B} : [B](f : B(A), l : [A])
= if head ~ tail <- l
then f(head) ~ map{A, B}(f, tail)
else [] of B
filter{A} : [A](f : BOOL(A), l : [A])
= if head ~ tail <- l
then let new_tail <- filter{A}(f, tail)
in if f(head)
then head ~ new_tail
else new_tail
else [] of A
reduce{A, B} : B(f : B(A, B), l : [A], seed : B)
= if head ~ tail <- l
then let new_seed <- f(head, seed)
in reduce{A, B}(f, tail, new_seed)
else seed
zip{A, B} : [(A, B)](l1 : [A], l2 : [B])
= if head1 ~ tail1 <- l1
then if head2 ~ tail2 <- l2
then (head1, head2) ~ zip{A, B}(tail1, tail2)
else [] of (A, B)
else [] of (A, B)
println : ()(s : [INT])
= let _ <- print(s) in
print("\r\n")
reverseHelper{A} : [A](l1 : [A], l2 : [A])
= if head ~ tail <- l1
then reverseHelper{A}(tail, head ~ l2)
else l2
reverse{A} : [A](l : [A])
= reverseHelper{A}(l, [] of A)
asciiDigitOffset = 48
i2sHelper : [INT](n : INT)
= let lastDigit <- n % 10 in
if n < 10
then [lastDigit + asciiDigitOffset]
else (lastDigit + asciiDigitOffset) ~ i2sHelper(n / 10)
i2s : [INT](n : INT)
= if n < 0
then '-' ~ reverse{INT}(i2sHelper(-n))
else reverse{INT}(i2sHelper(n))
b2s : [INT](b: BOOL) = if b then "true" else "false"
concat{A} : [A](l1 : [A], l2 : [A])
= if first ~ rest <- l1
then first ~ concat{A}(rest, l2)
else l2
scat = concat{INT}
range : [INT](start : INT, end : INT)
= if start == end
then [] of INT
else (start) ~ range(start + 1, end)
all : BOOL(l : [BOOL])
= if head ~ tail <- l
then if head then all(tail) else false
else true
any : BOOL(l : [BOOL])
= if head ~ tail <- l
then if head then true else any(tail)
else false
list_eq{A} : BOOL(l1 : [A], l2 : [A], equal : BOOL(A, A))
= if head1 ~ tail1 <- l1
then if head2 ~ tail2 <- l2
then equal(head1, head2) & list_eq{A}(tail1, tail2, equal)
else false
else if head2 ~ tail2 <- l2
then false
else true
sjoin : [INT](l: [[INT]], joiner: [INT])
= if head1 ~ tail1 <- l
then if head2 ~ tail2 <- tail1
then concat{INT}(head1, concat{INT}(joiner, sjoin(tail1, joiner)))
else head1
else ""
l2s{A} : [INT](l: [A], stringifier: [INT](A))
= '[' ~ concat{INT}(
sjoin(map{A, [INT]}(stringifier, l), ", "),
"]",
)
repeat{A} : [A](a : A, times : INT)
= if times <= 0
then [] of A
else a ~ repeat{A}(a, times - 1)
repeatF{A} : [A](a : A(), times : INT)
= if times <= 0
then [] of A
else a() ~ repeatF{A}(a, times - 1)
len{A} : INT(l : [A])
= if head ~ tail <- l
then 1 + len{A}(tail)
else 0
at{A} : [A](l : [A], i : INT)
= if head ~ tail <- l
then if i == 0
then [head]
else at{A}(tail, i - 1)
else [] of A
index{A} : INT(l : [A], element : A, equal: BOOL(A, A))
= if head ~ tail <- l
then if equal(head, element)
then 0
else let i <- index{A}(tail, element, equal) in
if i == -1 then i else i + 1
else -1
find{K, V} : [V](l : [(K, V)], key: K, equal: BOOL(K, K))
= if head ~ tail <- l
then let (_k, _v) <- head in
if equal(_k, key)
then [_v]
else find{K, V}(tail, key, equal)
else [] of V
intEq(m : INT, n : INT) = m == n
parseUIntHelper : [INT](s : [INT], n : INT)
= if d ~ ds <- s
then let digit_val <- d - asciiDigitOffset in
if (0 <= digit_val) & (9 >= digit_val)
then parseUIntHelper(ds, digit_val + (10 * n))
else [] of INT
else [n]
parseUInt : [INT](s : [INT])
= if d ~ ds <- s
then if d == (0 + asciiDigitOffset)
then if d2 ~ ds2 <- ds
then [] of INT
else [0]
else parseUIntHelper(s, 0)
else [] of INT
parseInt : [INT](s : [INT])
= if sign ~ digits <- s
then if sign == '-'
then if num ~ _ <- parseUInt(digits)
then [-num]
else [] of INT
else parseUInt(s)
else [] of INT
rpad : [INT](s : [INT], length : INT, c : INT)
= if head ~ tail <- s
then head ~ rpad(tail, length - 1, c)
else repeat{INT}(c, length)