-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprelude.affine
More file actions
141 lines (119 loc) · 3.18 KB
/
Copy pathprelude.affine
File metadata and controls
141 lines (119 loc) · 3.18 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
// SPDX-License-Identifier: PMPL-1.0-or-later
// AffineScript Standard Library - Prelude
// Common functions and utilities automatically available
module prelude;
// ============================================================================
// Core sum types (canonical home — ADR-011)
//
// `Option` and `Result` (and their constructors) are owned here, the
// foundational module. The `option` / `result` modules provide the
// *operations* over them and `use prelude::{...}` for the types. The
// duplicate/conflicting Option/Result ops that previously lived here
// (is_some, is_none, unwrap, unwrap_or, is_ok, is_err, unwrap_result,
// unwrap_or_result) were removed in #133: option::* and result::* are
// the single canonical bindings for those.
// ============================================================================
type Option<T> = Some(T) | None
type Result<T, E> = Ok(T) | Err(E)
// ============================================================================
// List utilities
// ============================================================================
fn map<T, U>(arr: [T], f: T -> U) -> [U] {
let mut result = [];
for x in arr {
result = result ++ [f(x)];
}
result
}
fn filter<T>(arr: [T], predicate: T -> Bool) -> [T] {
let mut 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 mut 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 mut result = [];
let mut i = start;
while i < end {
result = result ++ [i];
i = i + 1;
}
result
}
fn repeat<T>(value: T, n: Int) -> [T] {
let mut result = [];
let mut i = 0;
while i < n {
result = result ++ [value];
i = i + 1;
}
result
}