-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathjs_vector.js
137 lines (116 loc) · 2.18 KB
/
js_vector.js
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
'use strict';
function filterInPlace(p, a) {
var i = 0;
var j = 0;
while(i < a.length) {
var v = a[i];
if (p(v)) {
a[j] = v;
j = j + 1 | 0;
}
i = i + 1 | 0;
};
a.splice(j);
}
function empty(a) {
a.splice(0);
}
function pushBack(x, xs) {
xs.push(x);
}
function memByRef(x, xs) {
return xs.indexOf(x) >= 0;
}
function iter(f, xs) {
for(var i = 0 ,i_finish = xs.length - 1 | 0; i <= i_finish; ++i){
f(xs[i]);
}
}
function iteri(f, a) {
for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){
f(i, a[i]);
}
}
function toList(a) {
var _i = a.length - 1 | 0;
var _res = /* [] */0;
while(true) {
var res = _res;
var i = _i;
if (i < 0) {
return res;
}
_res = /* :: */[
a[i],
res
];
_i = i - 1 | 0;
continue ;
};
}
function init(n, f) {
var v = new Array(n);
for(var i = 0 ,i_finish = n - 1 | 0; i <= i_finish; ++i){
v[i] = f(i);
}
return v;
}
function copy(x) {
var len = x.length;
var b = new Array(len);
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
b[i] = x[i];
}
return b;
}
function map(f, a) {
var l = a.length;
var r = new Array(l);
for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){
r[i] = f(a[i]);
}
return r;
}
function foldLeft(f, x, a) {
var r = x;
for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){
r = f(r, a[i]);
}
return r;
}
function foldRight(f, a, x) {
var r = x;
for(var i = a.length - 1 | 0; i >= 0; --i){
r = f(a[i], r);
}
return r;
}
function mapi(f, a) {
var l = a.length;
if (l === 0) {
return [];
}
var r = new Array(l);
for(var i = 0 ,i_finish = l - 1 | 0; i <= i_finish; ++i){
r[i] = f(i, a[i]);
}
return r;
}
function append(x, a) {
return a.concat([x]);
}
exports.filterInPlace = filterInPlace;
exports.empty = empty;
exports.pushBack = pushBack;
exports.copy = copy;
exports.memByRef = memByRef;
exports.iter = iter;
exports.iteri = iteri;
exports.toList = toList;
exports.map = map;
exports.mapi = mapi;
exports.foldLeft = foldLeft;
exports.foldRight = foldRight;
exports.init = init;
exports.append = append;
/* No side effect */