forked from purescript/purescript-arrays
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathST.js
More file actions
160 lines (141 loc) · 3.05 KB
/
Copy pathST.js
File metadata and controls
160 lines (141 loc) · 3.05 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
function newSTArray () {
return [];
}
export { newSTArray as new };
export const peekImpl = function (just) {
return function (nothing) {
return function (i) {
return function (xs) {
return function () {
return i >= 0 && i < xs.length ? just(xs[i]) : nothing;
};
};
};
};
};
export const poke = function (i) {
return function (a) {
return function (xs) {
return function () {
var ret = i >= 0 && i < xs.length;
if (ret) xs[i] = a;
return ret;
};
};
};
};
export const popImpl = function (just) {
return function (nothing) {
return function (xs) {
return function () {
return xs.length > 0 ? just(xs.pop()) : nothing;
};
};
};
};
export const pushAll = function (as) {
return function (xs) {
return function () {
return xs.push.apply(xs, as);
};
};
};
export const shiftImpl = function (just) {
return function (nothing) {
return function (xs) {
return function () {
return xs.length > 0 ? just(xs.shift()) : nothing;
};
};
};
};
export const unshiftAll = function (as) {
return function (xs) {
return function () {
return xs.unshift.apply(xs, as);
};
};
};
export const splice = function (i) {
return function (howMany) {
return function (bs) {
return function (xs) {
return function () {
return xs.splice.apply(xs, [i, howMany].concat(bs));
};
};
};
};
};
export const unsafeFreeze = function (xs) {
return function () {
return xs;
};
};
export const unsafeThaw = function (xs) {
return function () {
return xs;
};
};
function copyImpl(xs) {
return function () {
return xs.slice();
};
}
export const freeze = copyImpl;
export const thaw = copyImpl;
export const sortByImpl = (function () {
function mergeFromTo(compare, fromOrdering, xs1, xs2, from, to) {
var mid;
var i;
var j;
var k;
var x;
var y;
var c;
mid = from + ((to - from) >> 1);
if (mid - from > 1) mergeFromTo(compare, fromOrdering, xs2, xs1, from, mid);
if (to - mid > 1) mergeFromTo(compare, fromOrdering, xs2, xs1, mid, to);
i = from;
j = mid;
k = from;
while (i < mid && j < to) {
x = xs2[i];
y = xs2[j];
c = fromOrdering(compare(x)(y));
if (c > 0) {
xs1[k++] = y;
++j;
}
else {
xs1[k++] = x;
++i;
}
}
while (i < mid) {
xs1[k++] = xs2[i++];
}
while (j < to) {
xs1[k++] = xs2[j++];
}
}
return function (compare) {
return function (fromOrdering) {
return function (xs) {
return function () {
if (xs.length < 2) return xs;
mergeFromTo(compare, fromOrdering, xs, xs.slice(0), 0, xs.length);
return xs;
};
};
};
};
})();
export const toAssocArray = function (xs) {
return function () {
var n = xs.length;
var as = new Array(n);
for (var i = 0; i < n; i++) as[i] = { value: xs[i], index: i };
return as;
};
};