-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.ts
74 lines (57 loc) · 1.62 KB
/
helpers.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
import { Mark } from 'vega-lite/build/src/mark';
import { Channel } from 'vega-lite/build/src/channel';
export function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
let id: number = 0;
export function assert(s: string, name?: string) {
// return `(assert ${s})\n\n`;
return `(assert (! ${s} :named c${name || id++}))\n\n`;
}
export function assertSoft(s: string, weight: number) {
return `(assert-soft ${s} :weight ${weight})\n\n`;
}
export function implies(a: string, b: string) {
return `(=> ${a} ${b})\n`;
}
export function and(...exprs: string[]) {
return `(and
${exprs.join("\n")}
)`;
}
export function or(...exprs: string[]) {
return `(or
${exprs.join("\n")}
)`;
}
export function eq(a: string, b: string) {
return `(= ${a} ${b})`;
}
export function not(s) {
return `(not ${s})`;
}
export function mark(m: Mark) {
return eq("mark", capitalizeFirstLetter(m) + "Mark");
}
// TODO: make e Encoding
export function channel(e: string, c: Channel) {
return eq(`(channel ${e})`, capitalizeFirstLetter(c) + "Channel");
}
// TODO: add aggregate, type, ...
export function iteFromDictFlipKeyValue(getValueExpr, dict, lastElseValue = "10000"){
// dict should be exhaustive
// todo: lowp check values in dict are proper
/*
* Recurse through dict
* */
const helper = ([head, ...tail]) => {
if (head === undefined)
return `${lastElseValue}`;
else {
const [key, value] = head;
return `(ite (= ${getValueExpr} ${value} ) ${key}
${helper(tail)})`;
}
};
return helper((Object as any).entries(dict));
}