-
Notifications
You must be signed in to change notification settings - Fork 21
/
jssm_util.ts
331 lines (210 loc) · 7.14 KB
/
jssm_util.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { JssmError } from './jssm_error';
/*******
*
* Predicate for validating an array for uniqueness. Not generally meant for
* external use.
*
*/
function arr_uniq_p<T>(el: T, i: number, source: T[]): boolean {
return source.indexOf(el) === i;
}
const array_box_if_string = n =>
typeof n === 'string'? [n] : n;
// this is explicitly about other peoples' data, so it has to be weakly typed
/* eslint-disable flowtype/no-weak-types */
const weighted_rand_select: Function = (options: Array<any>, probability_property: string = 'probability'): any => {
if (!Array.isArray(options)) {
throw new TypeError('options must be a non-empty array of objects');
}
if (!(typeof options[0] === 'object')) {
throw new TypeError('options must be a non-empty array of objects');
}
const frand : Function = (cap): number => Math.random() * cap,
or_one : Function = (item): any => item === undefined? 1 : item,
prob_sum : number = options.reduce( (acc, val:any): number => acc + or_one(val[probability_property]), 0 ),
rnd : number = frand(prob_sum);
let cursor : number = 0,
cursor_sum : number = 0;
while ((cursor_sum += or_one(options[cursor++][probability_property])) <= rnd) { } // eslint-disable-line no-empty,fp/no-loops
return options[cursor-1];
};
/* eslint-enable flowtype/no-weak-types */
/*******
*
* Returns, for a non-negative integer argument `n`, the series `[0 .. n]`.
*
* ```typescript
* import { seq } from './jssm';
*
* seq(5); // [0, 1, 2, 3, 4]
* seq(0); // []
* ```
*
*/
function seq(n: number): number[] {
if (!(Number.isInteger(n))) {
throw new TypeError('seq/1 takes a non-negative integer n as an argument');
}
if (n < 0) {
throw new TypeError('seq/1 takes a non-negative integer n as an argument');
}
return (new Array(n))
.fill(true)
.map( (_, i): number => i );
}
/*******
*
* Returns the histograph of an array as a `Map`. Makes no attempt to cope
* with deep equality; will fail for complex contents, as such.
*
* ```typescript
* import { histograph } from './jssm';
*
* histograph( [0, 0, 1, 1, 2, 2, 1] ); // Map()
* ```
*
*/
const histograph: Function = (ar : any[]): Map<any, number> => // eslint-disable-line flowtype/no-weak-types
ar.sort()
.reduce(
(m,v): Map<any, any> => // TODO FIXME eslint-disable-line flowtype/no-weak-types,no-sequences
( m.set(v, (m.has(v)? m.get(v)+1 : 1)) , m),
new Map()
);
const weighted_sample_select: Function = (n: number, options: Array<any>, probability_property: string): Array<any> => // TODO FIXME no any // eslint-disable-line flowtype/no-weak-types
seq(n)
.map( (_i): any => // TODO FIXME eslint-disable-line flowtype/no-weak-types
weighted_rand_select(options, probability_property)
);
const weighted_histo_key: Function = (n: number, opts: Array<any>, prob_prop: string, extract: string): Array<any> => // TODO FIXME no any // eslint-disable-line flowtype/no-weak-types
histograph(
weighted_sample_select(n, opts, prob_prop)
.map(
(s): any => s[extract] // TODO FIXME eslint-disable-line flowtype/no-weak-types
)
);
/*******
*
* Internal method generating names for edges for the hook lookup map. Not
* meant for external use.
*
*/
function name_bind_prop_and_state(prop: string, state: string): string {
if (typeof prop !== 'string') {
throw new JssmError(undefined, `Name of property must be a string; got ${prop}`);
}
if (typeof state !== 'string') {
throw new JssmError(undefined, `Name of state must be a string; got ${prop}`);
}
return JSON.stringify([prop, state]);
}
/*******
*
* Internal method generating names for edges for the hook lookup map. Not
* meant for external use.
*
*/
const hook_name = (from: string, to: string): string =>
JSON.stringify([from, to]);
/*******
*
* Internal method generating names for actions for the hook lookup map. Not
* meant for external use.
*
*/
const named_hook_name = (from: string, to: string, action: string): string =>
JSON.stringify([from, to, action]);
/*******
*
* Creates a SplitMix32 random generator. Used by the randomness test suite.
*
* Sourced from `bryc`: https://github.com/bryc/code/blob/master/jshash/PRNGs.md#splitmix32
*
* Replaces the Mulberry generator, which was found to have problems
*
*/
function gen_splitmix32(a? : number | undefined) {
if (a === undefined) {
a = new Date().getTime();
}
return function() {
a |= 0;
a = a + 0x9e3779b9 | 0;
var t = a ^ a >>> 16;
t = Math.imul(t, 0x21f0aaad);
t = t ^ t >>> 15;
t = Math.imul(t, 0x735a2d97);
return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
}
}
/*******
*
* Reduces an array to its unique contents. Compares with `===` and makes no
* effort to deep-compare contents; two matching arrays or objects contained
* will be treated as distinct, according to javascript rules. This also means
* that `NaNs` will be ***dropped***, because they do not self-compare.
*
* ```typescript
* unique( [] ); // []
* unique( [0,0] ); // [0]
* unique( [0,1,2, 0,1,2, 0,1,2] ); // [0,1,2]
* unique( [ [1], [1] ] ); // [ [1], [1] ] because arrays don't match
* unique( [0,NaN,2] ); // [0,2]
* ```
*
*/
const unique = <T>(arr?: T[]) =>
arr.filter( (v, i, a) => a.indexOf(v) === i );
/*******
*
* Lists all repeated items in an array along with their counts. Subject to
* matching rules of Map. `NaN` is manually removed because of conflict rules
* around {@link unique}. Because these are compared with `===` and because
* arrays and objects never match that way unless they're the same object,
* arrays and objects are never considered repeats.
*
* ```typescript
* find_repeated<string>([ ]); // []
* find_repeated<string>([ "one" ]); // []
* find_repeated<string>([ "one", "two" ]); // []
* find_repeated<string>([ "one", "one" ]); // [ ["one", 2] ]
* find_repeated<string>([ "one", "two", "one" ]); // [ ["one", 2] ]
* find_repeated<number>([ 0, NaN, 0, NaN ]); // [ [0, 2] ]
* ```
*
*/
function find_repeated<T>(arr: T[]): [T, number][] {
const uniqued = unique<T>(arr);
if (uniqued.length !== arr.length) {
const residue_keys = new Map();
arr.forEach(k =>
residue_keys.set(
k,
residue_keys.has(k)
? (residue_keys.get(k)+1)
: 1
)
);
uniqued.forEach(k =>
residue_keys.set(
k,
residue_keys.get(k) - 1
)
);
return [ ... residue_keys.entries() ]
.filter( (e) => ( (e[1] > 0) && (!(Number.isNaN(e[0]))) ) )
.map( (e) => [e[0], e[1]+1] );
} else {
return [];
}
}
export {
seq,
unique, find_repeated,
arr_uniq_p,
histograph, weighted_histo_key,
weighted_rand_select, weighted_sample_select,
array_box_if_string,
name_bind_prop_and_state, hook_name, named_hook_name,
gen_splitmix32
};