-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
52 lines (44 loc) · 1.11 KB
/
generate.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
import { add, minus, times, pow } from './functions';
import { number } from './stack';
const randomFunction = (functions, stacks) => {
if (Math.random() > 0.5) {
const keys = Object.keys(stacks);
const random = keys[Math.floor(Math.random() * keys.length)];
return [random, stacks[random].random()];
}
const names = Object.keys(functions);
const random = names[Math.floor(Math.random() * names.length)];
return [random, ...functions[random].randomParams()];
};
const randomInstructions = (functions, stacks) => {
return randomFunction(functions, stacks);
};
export const generate = (functions, stacks) => length => {
const ret = [];
let i = 0;
while (i++ < length) {
ret.push(randomInstructions(functions, stacks));
}
return ret;
};
const num = number(['x']);
/*
console.log(generate(
{ add: add(num),
minus: minus(num),
times: times(num),
pow: pow(num) },
{ number: num },
10
))
*/
export const example = (depth = 10) =>
generate(
{
add: add(num),
// minus: minus(num),
times: times(num)
// pow: pow(num)
},
{ number: num }
)(depth);