-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprelude.affine
More file actions
191 lines (161 loc) · 3.78 KB
/
Copy pathprelude.affine
File metadata and controls
191 lines (161 loc) · 3.78 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
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
// SPDX-License-Identifier: PMPL-1.0-or-later
// AffineScript Standard Library - Prelude
// Common functions and utilities automatically available
// ============================================================================
// Option type - represents optional values
// ============================================================================
type Option<T> = Some(T) | None
fn is_some<T>(opt: Option<T>) -> Bool {
match opt {
Some(_) => true,
None => false
}
}
fn is_none<T>(opt: Option<T>) -> Bool {
match opt {
Some(_) => false,
None => true
}
}
fn unwrap<T>(opt: Option<T>) -> T {
match opt {
Some(value) => value,
None => panic("Called unwrap on None")
}
}
fn unwrap_or<T>(opt: Option<T>, default: T) -> T {
match opt {
Some(value) => value,
None => default
}
}
// ============================================================================
// Result type - represents success or failure
// ============================================================================
type Result<T, E> = Ok(T) | Err(E)
fn is_ok<T, E>(res: Result<T, E>) -> Bool {
match res {
Ok(_) => true,
Err(_) => false
}
}
fn is_err<T, E>(res: Result<T, E>) -> Bool {
match res {
Ok(_) => false,
Err(_) => true
}
}
fn unwrap_result<T, E>(res: Result<T, E>) -> T {
match res {
Ok(value) => value,
Err(_) => panic("Called unwrap on Err")
}
}
fn unwrap_or_result<T, E>(res: Result<T, E>, default: T) -> T {
match res {
Ok(value) => value,
Err(_) => default
}
}
// ============================================================================
// List utilities
// ============================================================================
fn map<T, U>(arr: [T], f: T -> U) -> [U] {
let result = [];
for x in arr {
result = result ++ [f(x)];
}
result
}
fn filter<T>(arr: [T], predicate: T -> Bool) -> [T] {
let result = [];
for x in arr {
if predicate(x) {
result = result ++ [x];
}
}
result
}
fn fold<T, U>(arr: [T], init: U, f: (U, T) -> U) -> U {
let acc = init;
for x in arr {
acc = f(acc, x);
}
acc
}
/// Conforms to aLib collection/contains spec v1.0
fn contains<T>(arr: [T], element: T) -> Bool {
for x in arr {
if x == element {
return true;
}
}
false
}
fn sum(arr: [Int]) -> Int {
fold(arr, 0, |acc, x| acc + x)
}
fn product(arr: [Int]) -> Int {
fold(arr, 1, |acc, x| acc * x)
}
// ============================================================================
// Comparison and ordering
// ============================================================================
fn min(a: Int, b: Int) -> Int {
if a < b { a } else { b }
}
fn max(a: Int, b: Int) -> Int {
if a > b { a } else { b }
}
fn clamp(value: Int, min_val: Int, max_val: Int) -> Int {
if value < min_val {
min_val
} else if value > max_val {
max_val
} else {
value
}
}
// ============================================================================
// Boolean utilities
// ============================================================================
fn not(b: Bool) -> Bool {
if b { false } else { true }
}
fn all(arr: [Bool]) -> Bool {
for b in arr {
if not(b) {
return false;
}
}
true
}
fn any(arr: [Bool]) -> Bool {
for b in arr {
if b {
return true;
}
}
false
}
// ============================================================================
// Range and iteration utilities
// ============================================================================
fn range(start: Int, end: Int) -> [Int] {
let result = [];
let i = start;
while i < end {
result = result ++ [i];
i = i + 1;
}
result
}
fn repeat<T>(value: T, n: Int) -> [T] {
let result = [];
let i = 0;
while i < n {
result = result ++ [value];
i = i + 1;
}
result
}