-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.ltl
More file actions
112 lines (96 loc) · 2.28 KB
/
Copy pathcore.ltl
File metadata and controls
112 lines (96 loc) · 2.28 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
// Lateralus Standard Library — core.ltl
// Foundational functions available in every .ltl program
module stdlib.core
/// Print a value to stdout (no newline)
pub fn print(value: any) {
io.print(value)
}
/// Print a value followed by a newline
pub fn println(value: any) {
io.println(value)
}
/// Assert a condition; throw if false
pub fn assert(condition: bool, message: str) {
if !condition {
throw AssertionError(message)
}
}
/// Return the absolute value of a number
pub fn abs(n: float) -> float {
if n < 0.0 {
return -n
}
return n
}
/// Clamp x to the range [lo, hi]
pub fn clamp(x: float, lo: float, hi: float) -> float {
if x < lo { return lo }
if x > hi { return hi }
return x
}
/// Return the minimum of two values
pub fn min(a: float, b: float) -> float {
if a < b { return a }
return b
}
/// Return the maximum of two values
pub fn max(a: float, b: float) -> float {
if a > b { return a }
return b
}
/// Convert any value to a string representation
pub fn to_str(value: any) -> str {
return value as str
}
/// Return the length of a list or string
pub fn len(collection: any) -> int {
return sizeof collection
}
/// Iterate a function n times
pub fn repeat(n: int, body: fn(int)) {
let mut i = 0
while i < n {
body(i)
i += 1
}
}
/// Map a function over a list, returning a new list
pub fn map(lst: any, f: fn(any) -> any) -> any {
let mut result = []
for item in lst {
result = result + [f(item)]
}
return result
}
/// Filter a list by a predicate
pub fn filter(lst: any, pred: fn(any) -> bool) -> any {
let mut result = []
for item in lst {
if pred(item) {
result = result + [item]
}
}
return result
}
/// Reduce a list to a single value
pub fn reduce(lst: any, init: any, f: fn(any, any) -> any) -> any {
let mut acc = init
for item in lst {
acc = f(acc, item)
}
return acc
}
/// Sum a list of numbers
pub fn sum(lst: any) -> float {
return reduce(lst, 0.0, fn(a, b) a + b)
}
/// Range: return a list [start..end)
pub fn range(start: int, end: int) -> any {
let mut result = []
let mut i = start
while i < end {
result = result + [i]
i += 1
}
return result
}