-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinger_vector.ts
More file actions
112 lines (81 loc) · 2.53 KB
/
finger_vector.ts
File metadata and controls
112 lines (81 loc) · 2.53 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
import { FingerTree } from './finger_tree'
import * as FT from './finger_tree'
export {
FingerVector,
mkVector, singleton,
cons, snoc, append,
isEmpty, length,
head, tail, last, init, popLeft, popRight,
elementAt, take, drop, splitAt,
map, foldl, foldr
}
type Size = number
type FingerVector<T> = FingerTree<T, Size>
const SizeDict = FT.mkMeasureMonoidDict<{}, Size>(
_ => 1,
0,
(x, y) => x + y
);
const empty = FT.mkTree<any, number>(SizeDict);
function mkVector<T>(): FingerVector<T> {
return empty;
}
function isEmpty<T>(vec: FingerVector<T>) {
return FT.isEmpty(vec);
}
function singleton<T>(x: T): FingerVector<T> {
return FT.singleton(x, SizeDict);
}
function cons<T>(x: T, vec: FingerVector<T>) {
return FT.cons(x, vec);
}
function snoc<T>(x: T, vec: FingerVector<T>) {
return FT.snoc(x, vec);
}
function append<T>(vec1: FingerVector<T>, vec2: FingerVector<T>): FingerVector<T> {
return FT.concat(vec1, vec2);
}
function length<T>(vec: FingerVector<T>) {
return FT.measure(vec);
}
function head<T>(vec: FingerVector<T>): T | undefined {
return FT.peekLeft(vec);
}
function tail<T>(vec: FingerVector<T>): FingerVector<T> | undefined {
const res = FT.popLeft(vec);
return res && res[1];
}
function last<T>(vec: FingerVector<T>): T | undefined {
return FT.peekRight(vec);
}
function init<T>(vec: FingerVector<T>): FingerVector<T> | undefined {
const res = FT.popRight(vec);
return res && res[1];
}
function popLeft<T>(vec: FingerVector<T>): [T, FingerVector<T>] | undefined {
return FT.popLeft(vec);
}
function popRight<T>(vec: FingerVector<T>): [T, FingerVector<T>] | undefined {
return FT.popRight(vec);
}
function elementAt<T>(index: number, vec: FingerVector<T>): T | undefined {
return FT.search(vec, x => index < x);
}
function splitAt<T>(index: number, vec: FingerVector<T>): [FingerVector<T>, FingerVector<T>] {
return FT.split(vec, x => index < x);
}
function take<T>(n: number, vec: FingerVector<T>): FingerVector<T> {
return FT.splitLeft(vec, x => n < x);
}
function drop<T>(n: number, vec: FingerVector<T>): FingerVector<T> {
return FT.splitRight(vec, x => n < x);
}
function map<A, B>(vec: FingerVector<A>, f: (x: A) => B): FingerVector<B> {
return FT.map(vec, f, SizeDict);
}
function foldr<A, B>(vec: FingerVector<A>, initial: B, f: (x: A, acc: B) => B): B {
return FT.foldr(vec, initial, f);
}
function foldl<A, B>(vec: FingerVector<A>, initial: B, f: (acc: B, x: A) => B): B {
return FT.foldl(vec, initial, f);
}