-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.ltl
More file actions
123 lines (103 loc) · 2.67 KB
/
stack.ltl
File metadata and controls
123 lines (103 loc) · 2.67 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
// ===================================================================
// Lateralus stdlib — stack
// LIFO stack with standard push/pop/peek operations
// ===================================================================
// -- Stack construction ----------------------------------------------
/// Create a new empty stack.
pub fn new() -> list {
return []
}
/// Create a stack from a list (last element = top of stack).
pub fn from_list(items: list) -> list {
let s = []
for item in items {
s = s + [item]
}
return s
}
// -- Core operations -------------------------------------------------
/// Push an element onto the top of the stack.
pub fn push(s: list, item) -> list {
return s + [item]
}
/// Pop the top element. Returns [value, new_stack].
pub fn pop(s: list) -> list {
if len(s) == 0 {
return [nil, s]
}
let top = s[len(s) - 1]
let rest = slice(s, 0, len(s) - 1)
return [top, rest]
}
/// Peek at the top element without removing.
pub fn peek(s: list) {
if len(s) == 0 {
return nil
}
return s[len(s) - 1]
}
// -- Inspection ------------------------------------------------------
/// Return the number of elements.
pub fn size(s: list) -> int {
return len(s)
}
/// Check whether the stack is empty.
pub fn is_empty(s: list) -> bool {
return len(s) == 0
}
/// Check whether `item` is in the stack.
pub fn contains(s: list, item) -> bool {
for el in s {
if el == item {
return true
}
}
return false
}
// -- Bulk operations -------------------------------------------------
/// Reverse the stack (bottom becomes top).
pub fn reversed(s: list) -> list {
return reverse(s)
}
/// Swap the top two elements. Returns new stack.
pub fn swap_top(s: list) -> list {
if len(s) < 2 {
return s
}
let a = s[len(s) - 1]
let b = s[len(s) - 2]
let base = slice(s, 0, len(s) - 2)
return base + [a, b]
}
/// Duplicate the top element.
pub fn dup(s: list) -> list {
if len(s) == 0 {
return s
}
return s + [s[len(s) - 1]]
}
/// Drop the top `n` elements.
pub fn drop(s: list, n: int) -> list {
let keep = max(0, len(s) - n)
return slice(s, 0, keep)
}
/// Apply a function to every element, return new stack.
pub fn map_stack(s: list, func) -> list {
return map(s, func)
}
/// Fold (reduce) from bottom to top.
pub fn fold(s: list, init, func) {
let acc = init
for el in s {
acc = func(acc, el)
}
return acc
}
/// Clear the stack — return empty.
pub fn clear(s: list) -> list {
return []
}
/// Convert stack to a printable string.
pub fn to_string(s: list) -> str {
return "Stack(" + str(s) + ")"
}