-
Notifications
You must be signed in to change notification settings - Fork 0
/
ari.goal
88 lines (88 loc) · 4.42 KB
/
ari.goal
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
/ Shape
islst:{(@x)¿"ANSI"}
istbl:{and["d"=@x;&/"s"=@'!x;&/{(@'x)¿"ANSI"}x;&/(*ls)=ls:#'x]} / is x a dictionary-as-table
mktbl:{ks:!*x; vs:@[;ks]'x; ks!+vs} / given list of dicts, create dictionary-as-table
shape:{-1_#:'*:\x} / Implementation by John Earnest, shared on k-tree
depths:{[ind;l]?[(@l)~"A";,/o[ind+1]'l; (@l)¿"NSI";(#l)#ind; ind-1]} / list depths
zero:..[i:0;n:0.0;s:"";r:rx//;d:()!();f:(:);A:()] / handle, error don't fill
/ Names
munge:{[s]sub[rx/^[0-9]|[^a-zA-Z0-9]/;""]s}
/ DuckDB
duckdb.read:{db:sql.open""; r:db sql.q "select * from '$x';"; sql.close db; r}
/ Output formats
pp.tbl:fmt.tbl[;0i-1;0i-1;"%.2f"]; pp.dict:fmt.dict[;"%.2f"]
md.tbl:{[t;fmt] / helper
k:!t; v:(..?[(@x)¿"nN";p.fmt$x;$'x])'.t; w:(-1+""#k)|(|/-1+""#)'v
(k;v):(-w)!'´(k;v); "|"+("|\n|"/,/"|"/(k;"-"*w;+v))+"|"}
md.lst:{[l;fmt] / helper
ds:depths[l;0]; v:(..?["n"=@x;p.fmt$x;$'x])',//l
ind:..x*" "; pfx:(ind'ds)+"- "
"\n"/v;"\n"/pfx+v}
sprintf.md:{[x;fmt]?[istbl x;md.tbl[x;fmt]; (@x)¿"ANSI";md.lst[x;fmt]; "n"=@x;fmt$x; $x]} / String print to Markdown
ltx.lq:{sub[rq/ "/;" ``"]sub[rx/(?m)^"/;"``"]x}; ltx.rq:{sub[rq/" /;"'' "]sub[rx/(?m)"$/;"''"]x} / helpers
ltx.tbl:{[t;fmt] / helper
algn:!"r l"; algns:""/algn["S"=@'t]
k:!t; v:(..?[(@x)¿"nN";p.fmt$x;$'x])'.t; w:(-1+""#k)|(|/-1+""#)'v
(k;v):(-w)!'´(k;v); rs:" \\\\\n"/,/" & "/(k;"\\hline %";+v)
[email protected]@qq`\\begin{tabular}{|$algns|}\n\\hline
$rs \\\\\n\\hline\n\\end{tabular}`}
ltx.be:"\\begin{enumerate}"; ltx.ee:"\\end{enumerate}" / helpers
ltx.lstenv:{ / helper
(n;sig):(abs x;sign x);
?[sig=-1;"\n"+(""/(n-1)#"$ltx.be\n\\item\n")+"$ltx.be\n\\item %v\n"
sig=0;"\\item %v\n"
"\n"+(""/n#"$ltx.ee\n")+"\n\\item %v\n"]}
ltx.lst:{[l;fmt] / helper
v:(..?["n"=@x;p.fmt$x;$'x])',//l;
ds:depths[l;0]; cs:(»ds)-ds; fs:,/ltx.lstenv'cs
"$ltx.be\n"+(""/fs$'v)+"$ltx.ee"}
sprintf.ltx:{[x;fmt]?[istbl x;ltx.tbl[x;fmt]; (@x)¿"ANSI";ltx.lst[x;fmt]; "n"=@x;fmt$x; $x]} / LaTeX output
csv.tbl:{(*'x)!(1_'x)} / table from csv parsing, assumes header
json.tbl:{ / table from parsing json array of like objects
?["d"~@x
?[istbl x;:x;:error"dict is not a table: $x"]
mktbl@x]}
/ Interactive Tools
ac:{gs:,/rt.get'!"! kw";ms:?["s"=@x;(..p.x%p.gs)#gs;[([email protected])#gs]]; ms@<_ms } / auto-complete
/ Test Framework
tt.suite:"global"; tt.suitestate:..[es:();fs:();ps:();ss:()]; tt.state:..[global:tt.suitestate]
tt.st:tt.state; tt.ffast:0; tt.clear:{tt.st::tt.state}
tt.record:{[k;f;r] / key in test state; function tested; return value
suite:@[.;"FILE";:[;tt.suite]]
or[suite¿!tt.st;tt.st[suite]:tt.suitestate] / ensure starting suite state
?["ps"~k;(m:"\n"/" "+=r"msg"
d:..[f:p.f;p:p.r;msg:"function panicked with:\n$p.m"]
tt.st[suite]:@[tt.st[suite];"ps";..x,p.d])
"es"~k;(d:..[f:p.f;e:p.r;msg:"function returned error: $p.r\n $p.f"]
tt.st[suite]:@[tt.st[suite];"es";..x,p.d])
"fs"~k;(d:..[f:p.f;r:p.r;msg:"function returned $p.r instead of 1\n $p.f"]
tt.st[suite]:@[tt.st[suite];"fs";..x,p.d])
"ss"~k;(d:..[f:p.f]; tt.st[suite]:@[tt.st[suite];"ss";..x,p.d])
:error"tt.record k must be one of ps, es, fs, ss, but received $k"]}
tt.tf:{[f]
r:rt.try[f;0;{pmsg:x"msg";f:y; error[..[f:p.f;msg:p.pmsg;pnc:1]]}[;f]]
?["e"~@r;?[("d"[email protected])and(.r)..pnc;tt.record["ps";f;r];tt.record["es";f;r]]
1~r;tt.record["ss";f;r]
tt.record["fs";f;r]]
r}
tt.t:{?[("f"~@x);:tt.tf@x;error"tt.t expects f, received %s: %v"$(@x;x)]}
tt.fs:{sfx:"test.ari""test.goal"; pfx:"**/*""*"; fs:,/glob',/pfx+`sfx}
tt.file:{orig:@[.;"FILE";0]; ::["FILE";x]; ?[tt.ffast; 'eval 'read x;eval 'read x]; ::["FILE";or[orig;tt.suite]]; x}
tt.chkall:{
fs:tt.fs 0; rs:tt.file'fs; es:"e"=@'rs
?[|/es
[errors:(..p.es)#rs; files:(..p.es)#fs; error[(,"msg")!,"\n"/(files+": ")+(..msg)'errors]]
rs]}
tt.saymsg:{say " "+x"msg"}
tt.repsut:{[suite]
(ss;fs;es;ps):tt.st[suite][!"ss fs es ps"];(css;cfs;ces;cps):#'(ss;fs;es;ps)
bad:~+/(cfs;ces;cps); or[bad;say qq/Suite "$suite" has problems:/]
and[cfs;tt.saymsg'fs]; and[ces;tt.saymsg'es]; and[cps;tt.saymsg'ps]
s:qq/Suite "$suite" $css succeeded, $cfs failed, $ces errored, $cps panicked/
?[and[tt.suite~suite;+/#'tt.st[tt.suite]];say s; ~tt.suite~suite;say s;0]}
tt.report:{
tt.repsut'!tt.st; (tes;tfs;tps;tss):+/#''. .'tt.st; say "Total $tss succeeded, $tfs failed, $tes errored, $tps panicked"
and[|//@[;!"es fs ps"]'#''tt.st;error["Tests failed."]]}
/ Documentation
"http.serve"help"hostAndPort http.serve handlerFn"
1