SHARE
TWEET

sample.q

a guest Sep 11th, 2017 210 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \d .q
  2. /each: +-*%&|^<>=$ <= >= <> @ ? in within bin div abs log exp sqrt sin cos tan  f' f\: f/:
  3. neg:-:;not:~:;null:^:;string:$:;reciprocal:%:;floor:_:;ceiling:-_-:;signum:{(x>0)-x<0}
  4. mod:{x-y*x div y};xbar:{x*y div x:$[16h=abs[@x];"j"$x;x]};xlog:{log[y]%log x};and:&;or:|;each:{x'y};scan:{x\y};over:{x/y};prior:{x':y}
  5. mmu:$;lsq:!;inv:!:;md5:-15!;ltime:%:;gtime:{t+x-%t:x+x-%x}; /xnull:{$[0>@y;(,y)@~x=y;x=y;y 0N;y]}
  6.  
  7. /aggr: last sum prd min max avg wsum wavg f/  /beta:{cov[x;y]%var x}
  8. count:#:;first:*:;svar:{(n*var x)%-1+n:(#x)-+/^x};sdev:{sqrt svar x};scov:{(n*cov[x;y])%-1+n:(#x)-+/^x+y};med:{avg x(<x)@_.5*-1 0+#x,:()};all:min"b"$;any:max"b"$;rand:{*1?x}
  9.  
  10. /unif: f': f\
  11. sums:+\;prds:*\;mins:&\;maxs:|\;fills:^\;deltas:-':;ratios:%':;avgs:{(+\x)%+\~^x};differ:"b"$~~':;prev: :':;next:{$[0h>@x;'`rank;1_x,,x 0N]};
  12. rank:{$[0h>@x;'`rank;<<x]};reverse:|:;iasc:{$[0h>@x;'`rank;<x]};idesc:{$[0h>@x;'`rank;>x]}
  13. asc:{$[99h=@x;(!x)[i]!`s#r i:<r:. x;`s=-2!x;x;0h>@x;'`rank;`s#x@<x]};desc:{$[99h=@x;(!x)[i]!r i:>r:. x;0h>@x;'`rank;x@>x]}
  14.  
  15. msum:{$[99h=@y;(!y)!.z.s[x;. y];y-(-x)_(0i*x#y),y:+\y]};mcount:{msum[x;~^y]};mavg:{msum[x;0.0^y]%mcount[x;y]};mdev:{sqrt mavg[x;y*y]-m*m:mavg[x;y:"f"$y]}
  16. xrank:{$[0h>@y;'`rank;_y*x%#y:<<y]};mmin:{(x-1)&':/y};mmax:{(x-1)|':/y};xprev:{$[0h>@y;'`rank;y(!#y)-x]};rotate:{$[0h>@y;'`rank;98h<@y;'`type;#y;,/|(0;mod[x;#y])_y;y]};ema:{(*y)(1f-x)\x*y}
  17.  
  18. /other: ~,#_ !.   getenv exit
  19. distinct:?:;group:=:;where:&:;flip:+:;type:@:;key:!:;til:{$[0>@x;!x;'`type]};value:get:.:;attr:-2!;cut:{$[0h>@x;x*!-_-(#y)%x;x]_y}
  20. set:{$[@x;.[x;();:;y];-19!((,y),x)]};upsert:.[;();,;] / :: ,: files?
  21. raze:,/;union:?,;inter:{x@&x in y};except:{x@&~x in y};cross:{n:#m:&(#x)##y;$[99h=@x;((!x)[m],'n#!y)!(. x)[m],'n#. y;((),x)[m],'n#y]} /extant:{x@&~^x}
  22. sv:{x/:y};vs:{x\:y};sublist:{$[99h=@y;sublist[x;!y]!sublist[x;. y];~0h>@x;$[.Q.qp y;.Q.ind[y];y]i+!"j"$0|x[1]&(#y)-i:*x;abs[x]<#y;x#y;y]}
  23.  
  24. /file&comm
  25. read0:0::;read1:1::;hopen:<:;hclose:>:;hdel:~:;hsym:"s"$-1!';hcount:-7!;peach:{x':y};system:."\\",
  26.  
  27. /string:  like ss
  28. ltrim:{$[~t&77h>t:@x;.z.s'x;" "=*x;(+/&\" "=x)_x;x]};rtrim:{$[~t&77h>t:@x;.z.s'x;" "=last x;|ltrim@|x;x]};trim:{ltrim rtrim x}
  29. lower:{$[~t&77h>t:abs@@x;.z.s'x;19<t;.z.s@. x;~t in 10 11h;'`type;_x]}
  30. upper:{$[~t&77h>t:abs@@x;.z.s'x;19<t;.z.s@. x;~t in 10 11h;'`type;[c:"c"$@[!256;97+!26;-;32];$[11=t;`$c@$x;c x]]]}
  31. ssr:{,/@[x;1+2*!_.5*#x:(0,/(0,{n:x?"[";$[n=#x;n;n+.z.s$[(#x)=p:x?"]";'"unmatched ]";p]_x:(n+2+"^"=x n+1)_x]}y,"")+/:x ss y)_x;$[100h>@z;:[;z];z]]}
  32.  
  33. /select insert update delete exec  / fkeys[&keys] should be eponymous, e.g. order.customer.nation  
  34. /{keys|cols}`t `f's{xasc|xdesc}`t n!`t xcol(prename) xcols(prearrange)  FT(xcol xasc xdesc)
  35. view:{*|.`. .`\:x};tables:{."\\a ",$$[^x;`;x]};views:{."\\b ",$$[^x;`;x]}
  36. cols:{$[.Q.qp x:.Q.v x;.Q.pf,!+x;98h=@x;!+x;11h=@!x;!x;!+0!x]} /cols:{!.Q.V x}
  37. xcols:{(x,f@&~(f:cols y)in x)#y};keys:{$[98h=@x:.Q.v x;0#`;!+!x]};xkey:{(#x)!.[0!y;();xcols x]};
  38. xcol:{.Q.ft[{+(x,(#x)_!y)!. y:+y}x]y};xasc:{$[$[#x;~`s=-2!(0!.Q.v y)x;0];.Q.ft[@[;*x;`s#]].Q.ord[<:;x]y;y]};xdesc:{$[#x;.Q.ord[>:;x]y;y]}
  39. fkeys:{(&~^x)#x:.Q.fk'.Q.V x};meta:{([!c].Q.ty't;f:.Q.fk't;a:-2!'t:. c:.Q.V x)}
  40.  
  41. / R uj R(union join) R lj K(left(equi/asof)join)   trade asof`sym`time!(`IBM;09:31:00.0)
  42. lj:{.Q.ft[,\:[;y];x]} /;la:{$[&/j:z>-1;x,'y z;+.[+ff[x]y;(!+y;j);:;.+y z j:&j]]}{la[x;. y](!y)?(!+!y)#x}[;y]]x} /lj:,\:;aj:{lj[y]`s#xkey[x]z};aj0:{lj[y]`s#(x#z)!z}; /;bn:{@[i;&0>i:x bin y;:;#x]}
  43. ljf:{$[`s=-2!y;aj[!+!y;x;0!y];.Q.ft[{$[&/j:(#y:. y)>i?:(!+i:!y)#x;.Q.fl[x]y i;+.[+x;(f;j);:;.+.Q.fl[((f:!+y)#x:.Q.ff[x]y)j]y i j:&j]]}[;y]]x]}
  44. aj:{.Q.ft[{d:x_z;$[&/j:-1<i:(x#z)bin x#y;y,'d i;+.[+.Q.ff[y]d;(!+d;j);:;.+d i j:&j]]}[x,();;0!z]]y}
  45. aj0:{.Q.ft[{d: z;$[&/j:-1<i:(x#z)bin x#y;y,'d i;+.[+.Q.ff[y]d;(!+d;j);:;.+d i j:&j]]}[x,();;0!z]]y}
  46. ij:{.Q.ft[{x[j],'(. y)i j:&(#y)>i:(!y)?(!+!y)#x}[;y]]x}
  47. ijf:{.Q.ft[{.Q.fl[x j]y i j:&(#y:. y)>i?:(!+i:!y)#x}[;y]]x}
  48. pj:{.Q.ft[{x+0i^y(!+!y)#x}[;y]]x};asof:{f:!$[99h=@y;y;+y];(f_x)(f#x)bin y}
  49. uj:{$[()~x;y;98h=@x;x,(!+x:.Q.ff[x;y])#.Q.ff[y;x];lj[(?(!x),!y)#x]y]}
  50. ujf:{$[()~x;y;98h=@x;x,(!+x:.Q.ff[x;y])#.Q.ff[y;x];ljf[(?(!x),!y)#x]y]}
  51.  
  52. /wj[-1000 2000+\:trade`time;`sym`time;trade;(quote;(max;`ask);(min;`bid))]  (given `sym`time xasc quote)
  53. ww:{[a;w;f;y;z]f,:();e:+1_z;z:*z;y,'+f!+{[e;d;a;b]e@'d@\:a+!b-a}[*e;z f:*|e]/'+$[#g;(g#z)?g#y;0]|/:a+$[#g:-1_f;(f#z)bin@[f#y;*|f;:;]@;z[*f]bin]'w}
  54. wj:{[w;f;y;z]ww[0 1;w;f;y;z]};wj1:{[w;f;y;z]ww[1;w-1 0;f;y;z]}
  55.  
  56. fby:{$[(#x 1)=#y;@[(#y)#x[0]0#x 1;g;:;x[0]'x[1]g:.=y];'length]};xgroup:{x,:();a:x#y:0!y;$[#x_:y;+:'x@=a;a!+f!(#f:!+x)#()]};ungroup:{$[#x:0!x;,/+:'x;x]}
  57. ej:{x,:();y[&#:'i],'(x_z)(!0),/i:(=x#z:0!z)x#y:0!y} /{ungroup lj[z]xgroup[x]y}
  58.  
  59. /`[:../]t[.{csv|txt}]
  60. save:{$[1=#p:`\:*|`\:x:-1!x;set[x;. *p];   x 0:.h.tx[p 1]@.*p]}'
  61. load:{$[1=#p:`\:*|`\:x:-1!x;set[*p;. x];set[*p].h.xt[p 1]@0:x]}'
  62. rsave:{x:-1!x;.[`/:x,`;();:;.*|`\:x]}'
  63. rload:{x:-1!x;.[*|`\:x;();:;.     x]}'
  64. dsave:{.[*x;1_x,y,`;:;@[;*!+a;`p#].Q.en[*x]a:. y];y}/:
  65.  
  66. show:{1 .Q.s x;};csv:"," / ";"  also \z 1 for "D"$"dd/mm/yyyy"
  67.  
  68. parse:{$["\\"=*x;(system;1_x);-5!x]};eval:-6!;reval:-24!
  69. \d .Q  /def[`a`b`c!(0;0#0;`)]`b`c!(("23";"24");,"qwe")
  70. k:3.5;host:-12!;addr:-13!;gc:-20!;w:{`used`heap`peak`wmax`mmap`mphy`syms`symw!(."\\w"),."\\w 0"}  / used: dpft en par chk ind fs fu fc
  71. res:`abs`acos`asin`atan`avg`bin`binr`cor`cos`cov`delete`dev`div`do`enlist`exec`exit`exp`getenv`if`in`insert`last`like`log`max`min`prd`select`setenv`sin`sqrt`ss`sum`tan`update`var`wavg`while`within`wsum`xexp
  72. addmonths:{("d"$m+y)+x-"d"$m:"m"$x}
  73. Xf:{y 1:0xfe20,("x"$77+@x$()),13#0x00;(`$($y),"#")1:0x};Cf:Xf`char
  74. f:{$[^y;"";y<0;"-",f[x;-y];y<1;1_f[x;10+y];9e15>j:"j"$y*/x#10;(x_j),".",(x:-x)#j:$j;$y]}
  75. fmt:{$[x<#y:f[y;z];x#"*";(-x)$y]}
  76.  
  77. /DO NOT USE ROUTINES PAST HERE. SUBJECT TO CHANGE
  78. ff:{$[&/(!+y)in f:!+x;x;x,'(f_y)(#x)#0N]}
  79. fl:{$[98h=t:@x;+fl[+x;+y];99h=t;@[x,y;f;:;x[f]fl'y f@:&(f:!x)in!y];t&t<78h;x^y;@[y;i;:;x i:&(0<#:'x)&~#:'y]]}
  80. opt:{[o]x::$[#i:&o like"-[^0-9]*";i[0]#o;o];((`$1_*:)'o)!1_'o:i_o};def:{x,((!y)#x){$[0h>@x;*:;::](@*x)$y}'y}
  81. qt:{$[99h=@x;(98h=@!x)&98h=@. x;98h=@x]}; v:{$[-11h=@x;.$[":"=*t:$x;`$t,"/";x];x]};
  82. qp:{$[~98h=@x;0;@x:.+x;~":"=*$x;0]};      V:{$[qp x:v x;((,pf)!,. pf),+$[(~x in!d0[])&(x:.+x)in !*. .Q.vt;.Q.vp x;d0[]@x];0h>@*x:+0!x;x@'!x;x]}
  83. ft:{$[$[99h=@t:v y;98h=@. t;0];[n:#+!y;n!x 0!y];x y]};
  84. ord:{if[~&/b:(y,:())in .q.cols z;'y b?0b];ft[@[;!+t;@[;x@+y!(t:0!v z)y]]]z}
  85.  
  86. tx:{$[(77h<t)|~t:@x;$[0<t:@*x;-t;0];20h>t;t;11h=t:@x:.!x;t;98h=t;6h;1=#x:+!x;tx@*x;6h]};tt:0|tx'. V@
  87. fk:{$[(20h>t)|77h<t:@x;`;98h>@. t:!x;`;t]};t:" bg xhijefcspmdznuvts";ty:{$[0h>x:tx x;.q.upper t@-x;t x]}
  88. nct:{$[`~x;,/nct'.q.tables`.;([]n:x;c:.q.cols x;t:tt x)]}
  89.  
  90. fu:{[f;x]$[0h>@x;f x;f[u](u:?x)?x]}        /uniques
  91. fc:{$[1<n:(#y)&."\\s";,/x':(n;0N)#y;x y]} /parallel on cut
  92. A:"c"$65+!26;a:"c"$97+!26;n:"0123456789";nA:n,A;an:a,A,"_",n;b6:A,a,n,"+/"
  93. id:{$[0h>@x;`${$[(*x)in n;"a",x;x]}x@&(x:$x)in an;ft[{s[i]:`$($s i:&(s in`i,res,!`.q)|~(s?s)=!#s:id'!x:+x),'"1";+s!. x}]x]}
  94. j10:64/:b6?;x10:b6@0x40\: /base64 J from char10
  95. j12:36/:nA?;x12:nA@0x24\: /base36 J from char12(cusip)
  96.  
  97. l:{if[$[1>@d:!f:-1!x;1;`.d~*d];:.[$[qt d;*|`\:f;`.];();:;d:. f]];d@:&~d like"*$";p:(d=`par.txt)|d like"[0-9]*";
  98.  ."\\cd ",$x;{.q.set[x]$[0h>@!a:-1!x;. a;a`]}'d@&~(d=`html)|p|s:"."in'$d;if[|/p;L d@&p];if[~`.=x;(."\\l ",$:)'d@&s&~p];}
  99.  
  100. sw:{.q.sublist[_.5**|."\\C"]x};tab:{" "/:'(x*|/#:''y)$/:y};t0:{$[x>#y;((#*y)#'" -"),y;y]}
  101. s1:{-3!x} /{$[0>t:@x;$x;99h<t;-3!x;1h=t;"01"x;4h=t;,/$sw x;10h=t;x;t&t<98h;" "/:$sw x;-3!x]}
  102. s2:{$[99h=t:@x;((|/#:'x)$x:t0[#d;x]),'"| ",/:t0[#x:s2@!x]d:s2@. x;98h=t;(1#x),(,(#*x)#"-"),1_x:tab[1](,$!x),$[#*r:s2'. x:sw@+x;+r;()]
  103.  ~#x;();t>77h;s1'x;t;$x;|/(97h<t)|0h>t:?@:'x;s1'x;(1<#?#:'x)|$[1=#t;(*t)in 1 4 10h;0];s1'x;tab[1]@s2'sw'x]}
  104. S:{x[1]{$[x<#y;((x-2)#y),"..";y]}'$[t&77h>t:@z;,s1 z;99h<t;"\n"\:$z;$[s;,[;,".."];::]s2 .q.sublist[(y;n-s:(y+n:x[0]-2*.Q.qt z)<#z)]z]}
  105. s:{$[(::)~x;"";`/:$[10h=@r:@[S[(."\\c")-2 1;0];x;::];,-3!x;r]]}
  106. hap:{@[(0;h;(h:1+i 1)^(1+*&"@"=x)&e;e:(#x)^e)_x,(^e:(i:&"/"=x:1_$x)2)#"/";1;-1_]}
  107. hmb:{p:^/`$getenv`$_:\("HTTP";"NO"),\:"_PROXY";u:hap@-1!x;t:~(^*p)||/(*":"\:u 2)like/:{(("."=*x)#"*"),x}'","\:$p 1;a:$[t;p:hap@-1!*p;u]1;(4+*r ss d)_r:(-1!`$,/($[t;p;u]0 2))($y)," ",$[t;1_$x;u 3]," HTTP/1.1",s,(s/:("Connection: close";"Host: ",u 2),((0<#a)#,$[t;"Proxy-";""],"Authorization: Basic ",((-c)_.Q.b6@,/64\:'256/:'"i"$0N 3#a,c#0),(c:.q.mod[-#a;3])#"="),$[#z;("Content-type: ",z 0;"Content-length: ",$#z 1);()]),(d:s,s:"\r\n"),$[#z;z 1;""]}
  108. hg:hmb[;`GET;()];hp:{hmb[x;`POST;(y;z)]}
  109.  
  110. a0:(#:;*:;last;sum;prd;min;max;.q.all;.q.any;?:),a1:(avg;wsum;wavg;var;dev;cov;cor),`.q `svar`sdev`scov`med
  111. IN:{$[99h<@x;x in y;0]};qa:{$[qb x;0;IN[*x;a0];1;|/qa'1_x]};qb:{(2>#x)|(@x)&~11=@x}
  112.  
  113. / CAN EXIT HERE FOR SMALL Q
  114. / pt(tables) pf(date/month/year/int) pd(dirs) pv(values) pn(count)   pt::0#pf::`
  115. vt:(,`)!,()!();
  116. bv:{g:$[(::)~x;max;min];x:`:.;d:{`/:'x,'d@&(d:!x)like"[0-9]*"}'P:$[`par.txt in!x;-1!'`$0:`/:x,`par.txt;,x];
  117.  t:?,/!:'.Q.vt:{(&#:'x)(=,/. x)}'{({("DMJJ"`date`month`year`int?.Q.pf)$$last@x:`\:x}'x)!!:'x}'d;
  118.  d:{`/:'x[(. y)[;0]],'(`$$(. y)[;1]),'!y}[P]@{i:y@&:x=y x:@[x;&x~\:();:;*0#`. pf];(i;x i)}[;g]'+:t#/:g''.Q.vt:t#/:.Q.vt;.Q.vt:P!.q.except[. .Q.pf]''.Q.vt;
  119.  .Q.vp:t!{(+(,.Q.pf)!,0#. .Q.pf),'+(-2!'.+x)#'+|0#x:?[x;();0b;()]}'d;.Q.pt,:{.[x;();:;+.q.except[!+.Q.vp x;.Q.pf]!x];x}'.q.except[t;.Q.pt];}
  120.  
  121. pt:pm:();MAP:{{$[0>@a:.+0!. x;.q.set[x]@.`$-1_$a;]}'a@&~(a:."\\a")in pt;pm::();if[#pt;pm::pt!{(`u#pd,'pv)!p2[(x;();0b;())]'[pd;pv]}'pt]}
  122. dd:{`/:x,`$$y};d0:{dd[*|pd;*|pv]};p1:{$[#pm;pm[x](y;z);z in vt[y;x];vp x;+(!+. x)!`/:dd[y;z],x]};p2:{0!(?).@[x;0;p1[;y;z]]}
  123.  
  124. p:{$[~#D;p2[x;`:.]':y;(,/p2[x]'/':P[i](;)'y)@<,/y@:i:&0<#:'y:D{x@&x in y}\:y]}
  125. view:{pd::PD x:$[(::)~x;x;&PV in x];u~:?u::..[pf;();:;pv::PV x];x{.[y;();:;+(x . y,`.d)!y]}'pt::!x:d0[];pn::pt!(#pt)#()}
  126. L:{D::();if[x~,`par.txt;if[~#x:,/D::!:'P::`$":",'0:*x;'`empty]];if[^*PV::x@:<x:(t:"DMJJ"i:10 7 4?#$*x)$$x;'`part]
  127.  PD::$[#D::t$'$D;,/{P@&x in'D}'?PV;(#PV)#`:.];pf::`date`month`year`int i;view[];if[(0>."\\p")|."\\_";cn'.:'pt];}
  128. /L:{P::,`:.;D::,x;pf::`date;pt::!P[0]@**D;T::P!P{z!{x!(y . ,[;`]z,)'x}[x;y]'z}[pt]'D}
  129.  
  130. cn:{$[#n:pn x:.+x;n;pn[x]:{#p1[x;y;z]}'[x;pd;pv]]};pcnt:{+/cn x};dt:{cn[y]@&pv in x}
  131. ind:{,/i[j]{fp[pf;p]p1[x;pd y;p:pv y]z}[.+x]'(j:&~=':i)_y-n i:(n:+\0,cn x)bin y}
  132. fp:{+((,*x)!,(#z)#$[-7h=@y;y;(*|x)$y]),+z}
  133. foo:{[t;c;b;a;v;d]if[v;g:*|`\:b f:*!b;b:1_b];,/$[v|~#a;d fp[$[v;f,g;pf]]';::]p[(.+t;c;b;a)]d}
  134.  
  135. / select{u's|a's[by[date,]u's]}from t where[date..],[sym{=|in}..],..
  136. a2:({(%;(sum;("f"$;x));(sum;(~^:;x)))};{(sum;(*;("f"$;x);y))};{(%;(wsum;x;y);(sum;(*;x;(~^:;y))))};{(cov;x;x)};{(sqrt;(var;x))}
  137.  {(-;(avg;(*;("f"$;x);y));(*;(avg;x);(avg;y)))};{(%;(cov;x;y);(*;(dev;x);(dev;y)))};{(.q.scov;x;x)};{(sqrt;(.q.svar;x))};{(*;(%;(#:;`i);(+;-1;(#:;`i)));(cov;x;y))};{'`part})
  138.  
  139. / x0 translate;x1 aggrs;x2 translate  ?terminal (subselect/exec within partition)  unknown assumed uniform?
  140. qd:{$[(#:)~*x;(?:)~*x 1;0]};xy:{`$$*&x~/:y};         x1:{$[qb x;();IN[*x;a0];$[qd x;1_x;,x];,/x1'1_x]}
  141. x0:{$[qb x;x;IN[*x;a1];x0 a2[a1?*x]. 1_x;x0'x]};x2:{$[qb x; x;IN[*x;a0];$[qd x;(#:;(?:;(,/;xy[x 1]y)));[y:xy[x]y;$[(?:)~*x;(?:;(,/;y));(#:)~*x;(sum;y);(*x;y)]]];x2[;y]'x]}
  142. ua:{((`$$!#u)!u;x2[;u:?,/x1'x]'x:x0'x)};q0:{$[~qb x;,/q0'x;-11h=@x;*`\:x;()]};qe:{$[#x;99h=@x;1]}
  143. ps:{[t;c;b;a]if[-11h=@t;t:. t];if[~qe[a]&qe[b]|-1h=@b;'`nyi];d:pv;v:$[q:0>@b;0;~#b;0;-11h=@v:*. b;pf~*`\:v;0]
  144.  if[$[~#c;0;@*c;0;-11h=@x:c[0]1;pf~*`\:x;0];d@:&-6!*c;c:1_c]
  145.  if[$[#c;0;(g:(. a)~,pf)|(. a)~,(#:;`i)];f:!a;j:dt[d]t;if[q;:+f!,$[g;?d@&0<j;,+/j]];if[v&1=#b;:?[+(pf,f)!(d;j)[;&0<j];();b;f!,(sum;*f)]]]
  146.  if[~#d;d:pv@&pv=*|pv;c:,()];f:$[q;0#`;!b];g:$[#a;qa@*a;0]
  147.  $[(1=#d)|$[q;~g;u&pf~*. b];$[~q;.q.xkey[f];b;?:;::]foo[t;c;b;a;v]d;(?).(foo[t;c;$[q;()!();b];*a;v]d;();$[q;0b;f!f];*|a:$[g;ua a;(a;$[#a;(,/;)'k!k:!a;()])])]}
  148.  
  149. /if[$[#a;pf~*!a;0];:fp[pf'`pf];if[$[#.Q.pm;(=;`date)~2#*c;0];:p3(pm[.+t;-6!*|*c];1_c;b;a)]
  150. /dir part [`p#field] table [:,] x(dict) /group&index?  `:d/sym?x
  151. /.[dir;();,;.Q.en[dir]x];@[f xasc dir;f;`p#]
  152. enx:{[s;d;x]if[#f@:&{$[11h=@*x;&/11h=@:'x;11h=@x]}'x f:!+x;(`/:d,`sym)??,/?:'{$[0h=@x;,/x;x]}'x f];@[x;f;{$[0h=@y;(-1_+\0,#:'y)_x[`sym;,/y];x[`sym;y]]}s]};en:enx[?]
  153. /en:{[d;x]@[x;f@&11h=@:'x f:!+x;`sym?]}  init(`:sym set `u#0#`)  everyday (sym:get`:sym;.Q.en;.Q.en;..;`:sym set sym)
  154. par:{[d;p;t]`/:($[@!h:`/:d,`par.txt;`$":",h .q.mod[p;#h:0:h];d];`$$p;t)};qm:{$[(@x)|~#x;1;t:@*x;&/t=@:'x;0]}
  155. dpt:{[d;p;t]@[par[d;p;t];`;:;en[d]`. . `\:t]} / allows `a.b
  156. dpft:{[d;p;f;t]i:<t f;if[~&/qm'r:+enx[$;d]`. . `\:t;'`unmappable];{[d;t;i;u;x]@[d;x;:;u t[x]i]}[d:par[d;p;t];r;i;]'[(::;`p#)f=!r;!r];@[d;`.d;:;f,r@&~f=r:!r];t}
  157. hdpf:{[h;d;p;f](@[`.;;0#]dpft[d;p;f]@)'t@>(#.:)'t:.q.tables`.;if[h:@[<:;h;0];h"\\l .";>h]}
  158.  
  159. /loop through text /lc:{+/{+/0xa=1:(x;y*z;y)}[x;m]'!-_-(-7!x)%m:1048576} /line count of big file
  160. fsn:{[f;s;n]>[-7!s]{[f;s;x;n]i:(#r)^1+last@&"\n"=r:1:(s;x;n);f@`\:i#r;x+i}[f;s;;n]/0};fs:fsn[;;131000]
  161. fpn:{{r:.Q.trp[y;h;{>x;'(y;z)}h:<`$":fifo://",1_$x];>h;r}[y
  162.  {[f;h;n]b:"x"$();while[#s:1:h;if[~n>#b,:s;v:`\:b,0x0a;if[1<#v;f@-1_v];b:"x"$last v]];if[#b;f@`\:b]}[x;;z]]};fps:fpn[;;131000]
  163. /e.g. p:` sv(d:`:dir;`2007.02.12;`t);@[;`sym;`p#]p{@[x;`;,;`sym xasc .Q.en[d]get y]}/rdb's
  164. dsftg:{[dpt;xom;f;tw;g]d:*dpt;dpt:par . dpt;x:*xom;if[~0<m:xom 2;m+:-7!x]  //(loop load/save)
  165.  if[a>m:_a:(m-:o:xom 1)%b:+/tw 10=@*tw;'`length];0N!($dpt),": ",$m&:M;i::0;n::1000000&_1e8%b
  166.  do[-_-m%n;@[dpt;`;$[i;,;:];en[d]@+g f!tw 1:(x;o+b*i;b*n&:m-i)];0N!i+:n];dpt};M:0W
  167.  
  168. / fill in empty partitions
  169. chk:{if[x~(::);'"missing dir argument"];f:{`/:'x,'d@&(d:!x)like"[0-9]*"};d@:>.:'$last'`\:'d:$[`par.txt in!x;,/f'-1!'`$0:`/:x,`par.txt;f x]
  170.  {[e;u;d]u[i]{.[x;(y;`);:;?[z;();0b;()]]}[d]'e i:&~u in!d}[d[(+u in/:t)?\:1b](0#.)'u,'`;u:?,/t:!:'d]'d}
  171.  
  172. Ll:-100!;Lp:-101!;Lx:-102!;Lu:-103!;Ls:-104!;fqk:{("<"~*x)|x~*-3#.{}};btx:{r:,Lp x;while[@x:Lu(0b;x);r,:,Lp x];r};bt:{1'pl'1_btx Ll`;};sbt:{,/pl'x};trp:{-105!(x;,y;z)}
  173. dr:{0:0};dw:{2@x;};pl0:{.[{[f;x;y;z]g:{$[x>0;x&#y;x|-#y]#y};m:1;n:0|(b:+\0,1+#:'s:`\:s)bin y:(0|y)+2*"k)"~2#s:x 3;sf:$[#x 1;x[1],$[0>x 2;"";":",($n+x 2)],": ";""];sn:{$[#x;((2*".."~2#x)_x),":";x]}@*x;h:("  ",5$"[",($z),"]")
  174.  if[("locked"~x 3)|$[f;0b;fqk x 1];:h,"(",($[#sn;-1_sn;-2_sf]),")\n\n"];h,:sf,sn;h,`/:((n>m)#,""),g[-m+1;(n+1)#s],(,(" \t"(((#h)*n<1)#0),(y-b n)#s[n]="\t"),"^"),g[m](n+1)_s}x;1_y;{dw"pl0\n";}]};pl:pl0 0
  175. jl8:{$[$[#x;~x[3]in y[;3];0b];,x;()],y};srr:{@[{if["abort"~*x;:""];"'",$[.z.q;($.z.Z)," ";""],(x[0],"\n"),/pl'jl8[x 3;x 2]};x;"srr\n"]};prr:{dw srr x};lu:{$[@a:Lu(x;*y);$[.Q.dbg~**v:Lp a;y;(a;v)];y]}
  176. DL:0;dbg:{pm:{dw" q"[x],$[1<#d:$."\\d";d;""]," )"[x],(1|DL)#")"};dq:{x $[l:#*y;*(.**y)3;`];~"k)"~2#y[1;3]}ds:{."\\d ",((~"."=*x)#"."),x:$x};de:@[{DL-:1;x y;{."\\x .z.e",x}'"xy"}ds;;::]
  177.  @[{DL+:1;.z.ex:*x 1;.z.ey:x[1]1};x;::];b:$[#*v:Lp y;0xff~(*. bf:**v)bi:v[0;1];0b];if[b;dw "#",($bi),"\n"];w:(y;v:Lp y);if[e:@*x;x[2;;3]+:1+v 3;prr x];d0:."\\d"
  178.  y:*w:{$[~fqk x[1;1]1;x;lu[0b]x]}/w;q:dq v:w 1;dw pl v;while[*(~(,"\\")~l:dr[];pm[q]);$[l~"\\ ";q:~q;l in+,".`";(q:dq v;dw pl0[1]@*(v:w 1;y:*w;w:lu["."=*l](y;v)))
  179.   (~e)&l in+,":>";:*($[b;bc[bf;bi];]0 0N(":"=*l);de d0);"'"=*l;:*((1_l;());de d0);c&~#*r:Lx(y;"kq"[q],")",(c:":"=*l)_l);:*(r;de d0);dw .Q.s r 1]];de d0;("abort";())}
  180. err:{(*x;jl8[x 3;$[@y;btx y;()],x 2])}; BP:(,"")!,();bp:{*|(Ls(x;(),y;(#y)#z);(*. x)y)};bs:{BP[$x],:y!bp[x;y,:();0xff];};bu:{bp[x;y;BP[$x;y,:()]]}
  181. bd:{bu[x;y];@[`BP;$x;_;y]};bc:{bu[x;y];dbg::{[d;f;i;r;u;v]dbg::d;bs[f;i];$[#*u;d[u;v];r]}[dbg;x;y;z];-1}
  182.  
  183. \d .h / <t a>c</t>
  184. htc:{,/("<";x;y;"</";x:($x),">")};hta:{,/("<";$x;,/" ",'($!y),'"=",'{$[10h=@x;"\"",x,"\"";$x]}'. y;">")};htac:{,/(hta[x]y;z;"</";$x;">")}
  185. ha:{htac[`a;(,`href)!,x]y};hb:{htac[`a;`target`href!`v,,x]y};pre:{htc[`pre]@`/:x};xmp:{htc[`xmp]@`/:x}
  186. cd:{.q.csv 0:x};td:{"\t"0:x};hc:{"&lt;"/:"<"\:x};xs:.q.ssr/[;"&<>";("&amp;";"&lt;";"&gt;")]
  187. xd:{g:{(#*y)#'(,,"<",x),y,,,"</",x:($x),">"};(,"<R>"),(,/'+g[`r]@,/(!x)g'{,xs'$[11h=@x;$x;t&78h>t:@x;$x;x]}'x:+0!x),,"</R>"}
  188.  
  189. ex:("<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?>";);iso8601:{$[^x;"";@[$"p"$x;4 7 10;:;"--T"]]}
  190. eb:{htac[`Workbook;(`$"xmlns",/:$``:ss`:o`:x`:html)!("urn:schemas-microsoft-com:office:",/:$(2#`spreadsheet),`office`excel),,"http://www.w3.org/TR/REC-html40"]x}
  191. es:{htac[`Worksheet;(,`ss:Name)!,$x]htc[`Table]@,/(htc[`Row]@,/ec')'(,!+y),+.+y:0!y};ed:{ex eb es[`Sheet1]x};edsn:{ex eb@"\r\n"/:(!x)es'. x}
  192. ec:{htc[`Cell]htac[`Data;(,`ss:Type)!,$`String`Number`String`DateTime`DateTime`String i](xs;$:;xs@$:;iso8601;iso8601 1899.12.31+"n"$;xs@$:)[i:-10 1 10 12 16 20h bin -@x]x}
  193.  
  194. \d .j    /[]{} Cbg*xhijefcspmdznuvt
  195. e:{"c"$$[x<128;x;0b/:'@[;0;(1b,i#1b),]10b,/:((5 10;0 4 10)i:x>2047h)_0b\:"h"$x]}@256/:
  196. q:"\"";s:{q,x,q};es:{s@,/{$[x in r:"\t\n\r\"\\";"\\","tnr\"\\"r?x;x]}'x};J:(($`0`1)!$`false`true;s;{$[#x;x;"null"]};es;{s@[x;&"."=8#x;:;"-"]};s)1 2 5 11 12 16h bin
  197. j:{$[10=abs t:@x;es x;(::)~x;"null";99=t;"{",(","/:({$[q~*x;x;s x]}'j'!x),'":",'j'. x),"}";-1<t;"[",($[98=t;",\n ";","]/:.Q.fc[j']x),"]";J[-t]@$x]}
  198. / c:{$["{"=*x;(`$c'n#'x)!c'(1+n:(u'x)?'":")_'x:d x;"["=*x;.Q.fc[c']d x;q=*x;$[1<+/v x;'`err;"",@[.:;x;. .q.ssr[.q.ssr[x;"\\\\";"\\134"];"\\u????";{1_-1_-3!e@"X"$2 2#2_x}]]];"a">*x;"F"$x;"n"=*x;0n;"t"=*x]}
  199. / k:{c x@&~v[x]&x in" \t\n\r"};v:{~0(0 1 0;1 0 2;1 1 1)\x*3>x:" \"\\"?x};u:{?[v x;x;" "]};d:{$[1<n:(s:+\1 -1 1 -1"{}[]"?t:u x)?0;1_'(0,&(","=n#t)&1=n#s)_x:n#x;()]}
  200. k:-29!
  201.  
  202. \d .h
  203. tx:`raw`json`csv`txt`xml`xls!(,:;.j.j';cd;td;xd;ed);xt:{$[`json=x;.j.k'y;'`nyi]} /;+(`$*y)!`$+1_y:"\t"\:'y} / *bifcsmdzuvt  /$[x~`xml;"<?xml version='1.0' encoding='UTF-8'?>"
  204.  
  205. c0:`024C7E;c1:`958600;logo:"kx.com" /logo:,/(c0;c1;c0){htac[`font;(,`color)!,x]y}'("[kx";"systems";"]")  /808000
  206. logo:,/(c0;c1){htac[`font;(,`color)!,x]htc[`b]y}'("kx";".com")
  207. sa:"a{text-decoration:none}",/`link`visited`active{"a:",($x),"{color:",y,"}"}'$(c0;c0;c1)
  208. html:{htc[`html]htc[`head;htc[`style]sa,sb],htc[`body]x};sb:"body{font:10pt verdana;text-align:justify}"
  209. fram:{htc[`html]htc[`head]htc[`title;x],htac[`frameset;(,`cols)!,($116|316&43+(7+"?"~*z)*|/#:'y),",*"]@,/hta[`frame]'((,`src)!,*z;`name`src!`v,,*|z)}
  210. jx:{[j;x]x:. x;$[$[.Q.qt[x];(N:(*."\\C")-4)<n:#x;0];(" "/:ha'["?[",/:$(0;0|j-N),|&\(n-N;j+N);$`home`up`down`end],,($n),"[",($j),"]";"");()],hc'.Q.S[."\\C";j]x}
  211. uh:{(*x),/{("c"$"X"$2#x),2_x}'1_x:"%"\:x} /uh:ssr[;"%??";{,"X"$1_x}];hu:ssr/[;" %";{"%",$"x"$*x}]
  212. sc:"$-.+!*'(),",.Q.an;hug:{,/@["%",'$"x"$!256;x;:;x]@};hu:hug sc /rfc1738 safe
  213. ty:{(`$*x)!*|x:+" "\:'x}("htm text/html";"html text/html";"csv text/comma-separated-values";"txt text/plain";"xml text/plain";"xls application/msexcel"
  214.  "gif image/gif";"jpg image/jpeg";"png image/png";"bmp image/bmp";"ico image/x-icon";"svg image/svg+xml";"pdf application/pdf";"css text/css"
  215.  "zip application/x-compressed";"js application/x-javascript";"doc application/msword";"swf application/x-shockwave-flash";"json application/json")
  216. hn:{"HTTP/1.1 ",x,"\r\nContent-Type: ",$[#t:ty y;t;"application/octet-stream"],"\r\nConnection: close\r\nContent-Length: ",($#z),"\r\n\r\n",z}
  217. HOME:"html";hy:hn"200 OK";hp:hy[`htm]html pre@;he:hn["400 Bad Request";`txt]"'",  /GET /[r.{txt|csv|xml|xsl}]?..  or ?[J
  218. .z.ph:{x:uh$[@x;x;*x];tf:$[2=."\\e";{.Q.trp[y;z;{he y,"\n",.Q.sbt@(-x)_z}x]};{x;@[y;z;he]}];$[~#x;hy[`htm]fram[$.z.f;x]("?";"?",*x@<x:$(."\\v"),."\\b")
  219.   x~,"?";hp@{hb["?",x]x}'n@<n:$(."\\v"),."\\b";"?["~2#x;@[{hp jx["J"$2_x]R};x;he];"?"=*x;tf[5;{hp jx[0j]R::1_x};x];"?"in x;tf[4;{hy[t]@`/:tx[t:*|`\:`$n#x]@.(1+n:x?"?")_x};x]
  220.   #r:@[1::;`$":",p:HOME,"/",x;""];hy[`$last@"."\:x]"c"$r;hn["404 Not Found";`txt]p,": not found"]}
  221.  
  222. / (notepad;.h.ht`kdb;browse) t/t0/t1 .head;para(http://.. \tcode\t); code(\t align)  <img <a#link print"all frames"
  223. br:"<br>";hr:{(#x)#"-"};nbr:htc[`nobr];code:{" "/:@[x;1+2*!_.5*#x:"\t"\:x;{htc[`code]nbr hc x}]}
  224. http:{$[#i:ss[x]"http://";(i[0]#x),/{ha[x;x:i#x],(i:&/x?") ")_x}'i_x;x]};text:{`/:{htc[`p]http code x}'x@&0<#:'x}
  225. data:{if[100<#*x:.Q.tab[1]n#'x,\:(n:|/#:'x:"\t"\:'x:1_'x)#,"";0N!#*x;0N!'x];xmp$["- "~2#x 1;@[x;1;hr];x]}
  226. ht:{[t]x:0:`$"src/",(T:t:$t),".txt";if[~"."=**x;T:*x;x:1_x];h:.q.trim(n@:i)_'x i:&0<n:("."=x)?'0b
  227.  x[i]:h{htac[`h5;(,`id)!,x;y]}'H:("."/:'$(){$[y>#x;x,1;(y#x),1+x y-:1]}\n),'" ",/:h
  228.  x:`/:("Copyright &copy; ",logo;htc[`h5]T),((#x)#(text;data))@'x:(&~=':~(0<#:'x)&" "=*:'x)_x
  229.  c:h{hb[x,"#",y;z],br}[a:t,".htm"]'H;c:nbr@`/:$[&/n=:1;c;1_,/(,br),/:(&n)_c]
  230.  (`$a)1:fram[T;H]b:"a/",/:("_",a;a);(`$b)1:'html'(($.z.D),htc[`h5;"Contents"],c;x);}
  231.  
  232. \d .o /odbc   timedec/cs could be 3(23,12)
  233. ex:{$[.Q.qt x:."s)",x;0!x;x]} / char(1+) binary(display2*) -1 longvarchar -4 longvarbinary sql_no_total(-4)SAS/4096
  234. T:`text`bit```tinyint`smallint`int`bigint`real`float`char`varchar``date`date`datetime``time`time`time
  235. T0:  -1 -7 0 0 -6 5  4 -5  7  8 1  12 0  9  9 11 0 10 10 10h
  236. B0:4096  1 0 0  1 2  4  8  4  8 1 255 0  6  6 16 0  6  6  6
  237. C0:4096  1 0 0  3 5 10 19  7 15 1 255 0 10 10 23 0  8  8  8
  238. PS:   1  2 0 0  2 2  2  2  2  2 3   3 0  2  2  2 0  2  2  2h /char basic both
  239. t:0 1 4 5 6 7 8 9 10 11 14 15 19;Columns:{$[#x;Cols`$x;,/Cols'.q.tables`.]}
  240. q)TI:`DATA_TYPE xasc([]TYPE_NAME:T t;DATA_TYPE:T0 t;COLUMN_SIZE:C0 t;LITERAL_PREFIX:`;LITERAL_SUFFIX:`;CREATE_PARAMS:`;NULLABLE:1h;CASE_SENSITIVE:1h;SEARCHABLE:PS t;UNSIGNED_ATTRIBUTE:0h;FIXED_PREC_SCALE:0h;AUTO_INCREMENT:0h;LOCAL_TYPE_NAME:`;MINIMUM_SCALE:0h;MAXIMUM_SCALE:0h)
  241. q)TypeInfo:{$[count x;select from TI where DATA_TYPE="H"$x;TI]} /msqry32 uses special e.g. select p.i from p p
  242. q)Special:{([]SCOPE:2h;COLUMN_NAME:(0&0<count x)#`i;DATA_TYPE:4h;TYPE_NAME:`int;COLUMN_SIZE:10;BUFFER_LENGTH:4;DECIMAL_DIGITS:0h;PSEUDO_COLUMN:2h)}
  243. q)o:{flip(`TABLE_CAT`TABLE_SCHEM!``),flip x};Tables:{Ts[x 0;$[count t:1_x:`$","vs x except"'";t;enlist`TABLE]]}
  244. q)Ts:{y:raze{([]TABLE_NAME:(tables`.;views`.)`TABLE`VIEW?x;TABLE_TYPE:x;REMARKS:`)}each y;o$[`~x;y;y where x=y`TABLE_NAME]}
  245. q)Stats:{o([]TABLE_NAME:1#`$x;NON_UNIQUE:0Nh;INDEX_QUALIFIER:`;INDEX_NAME:`;TYPE:0h;ORDINAL_POSITION:0h;COLUMN_NAME:`;ASC_OR_DESC:`;CARDINALITY:count value x;PAGES:0N;FILTER_CONDITION:`)}
  246. q)Cols:{o([]TABLE_NAME:x;COLUMN_NAME:cols x;DATA_TYPE:T0 t;TYPE_NAME:T t;COLUMN_SIZE:C0 t;BUFFER_LENGTH:B0 t:.Q.tt x;DECIMAL_DIGITS:0h;NUM_PREC_RADIX:10h;NULLABLE:1h;REMARKS:`)}
  247. q)Key:{o([]TABLE_NAME:x;COLUMN_NAME:r;KEY_SEQ:"h"$1+til count r:keys x:`$x;PK_NAME:`)}
  248. q)FG:{o([]PKTABLE_NAME:x;PKCOLUMN_NAME:"s"$(first keys@)each x;FKTABLE_CAT:`;FKTABLE_SCHEM:`;FKTABLE_NAME:y;FKCOLUMN_NAME:z;KEY_SEQ:1h;UPDATE_RULE:0h;DELETE_RULE:0h;FK_NAME:`;PK_NAME:`)}
  249. Fkey:{FG[. r;x;!r:.q.fkeys x:`$x]};Gkey:{FG[x;"s"$*r;"s"$*|r:+,/(x:`$x){y,'&x=.q.fkeys y}'.q.tables`.]}
  250.  
  251. \d .
  252. {@[.:;"\\l ",$[#e:getenv`QINIT;e;"q.q"];::]}[];
  253.  
  254. \
  255. clib: strlen strcat strcmp    regexp  str[str|chr] str[n]cpy  (strmat strind substr trim)
  256. java: length concat compareTo matches indexOf      substring trim to{Lower|Upper}Case
  257. sql3: char_length || < == >   similar position     substring trim   {lower|upper}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top