-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.ts
62 lines (50 loc) · 1.42 KB
/
stack.ts
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
// A generic Stack in JavaScript.
export class Stack<T> {
static empty = new Stack<never>()
constructor()
constructor(head: T, tail: Stack<T>)
constructor(readonly head?: T, readonly tail?: Stack<T>) {
if (tail == null) {
this.head = this.tail = undefined
this.empty = true
} else {
this.empty = false
}
}
readonly data:
| { head: T; tail: Stack<T>; empty: true }
| { head?: undefined; tail?: undefined; empty: false } = this as any
readonly empty: boolean
push(head: T): Stack<T> {
return new Stack(head, this)
}
shift(): readonly [T, Stack<T>] | readonly [undefined, Stack<never>] {
if (this.tail) {
return [this.head!, this.tail]
} else {
return [undefined, Stack.empty]
}
}
map<U>(fn: (value: T) => U): Stack<U> {
if (this.empty) return Stack.empty
return new Stack<U>(fn(this.head!), this.tail!.map(fn))
}
filter(fn: (value: T) => unknown): Stack<T> {
if (this.empty) return Stack.empty
if (fn(this.head!)) {
return new Stack(this.head!, this.tail!.filter(fn))
} else {
return this.tail!.filter(fn)
}
}
some(fn: (value: T) => unknown): boolean {
if (this.empty) return false
if (fn(this.head!)) return true
return this.tail!.some(fn)
}
every(fn: (value: T) => unknown): boolean {
if (this.empty) return true
if (!fn(this.head!)) return false
return this.tail!.every(fn)
}
}