// error trap on \e 0 // define cK in the .ck namespace \d .ck // wrappers o:{:[z~0;x;z~1;y z;z~2;y 0;~7=4:y;E[z;y];y z]} / ck wrapper u:{o[x;get x;y]} / user wrapper f:{o[`$mm d x;{E[y;x]}[x]]} / function wrapper {{@[_d;`$y,$x;:;z[x]]}'[x;"das";({:[@z;(x;y)z;x>#z;pro[y;z];{y,,dot[x;z]}[y]. cut[x;z]]} / stack wrappers {:[@z;(x;y)z;x>#z;pro[y;z];{y,x z}[y]. cut[x;z]]} {:[@z;(-x+1;y)z;x>#z;pro[y;z];{dot[x[y];z]}[y]. cut[x;z]]})]}'!6; cut:{(0,(#y)-x)_ y} / cut stack for apply dot:{:[-3=4:y;{x[y]}/[x;y];x . y]} / handle f3 ."abc" pro:{if[_n~t:k@$x;t:j x];:[#y;f[y,t];t]} / projection name:{:[-4=t:4:x;x;~t;_f'x;4=t;x;7=t;x 0;`]} / name of primitive one:{:[1=#x;*x;x]} / ,x -> x idot:{:[0>t:4:x;x;~t;_f'x;~7=t;x;~x~K[0;G?"."];x;.]} / index w/dot // k optimization prim:{:[1<#y;func y;~7=4:p:*y;func y;~_n~f:kp[x;p 1];f;jp[x;p 0;p 1]]} / primitive kp:{:[(#*k)>i:(k:I x=1)?y;k[0;i]]} / k primitive jp:{:[y _in F x-1;z]} / j primitive p1:prim[1];p2:prim[2];p12:{:[~_n~p:p1 x;p;p2 x]};p1v:{:[0>4:x;x;p1 x]} / primitives of valence n func:{[f]:[~-1=4:,//n:oper[2;f];;val[n]_in 1 2;{x[y]}/[comp@|n;|oper[1;f]]]} / construct function val:{val2/|:[4:x;x;@[x;&~@:'x;_f]]} / resultant valence val2:{:[y<0;-1;y=0;:[x=2;1;-1];y=1;:[x=1;1;-1];y=2;:[x=1;2;-1];-1]} / pairwise valence / resultant valence oper:{:[~t:4:y;x _f'y;7=t;y x;1=x;y;0]} / operator|valence comp:{[n]."{[",(1_,/";",'v),";x]",expr[n;v:"x",'$!#n],"}"} / composition expr:{[n;v] / expression: v[0],1!"][",:[1=#n;"x" / f -> f[x] 2=#n;"x",:[1=*n;"";";",v 1] / f -> f[x;g] 1=*n;_f[1_ n;1_ v] / m -> m[..] _f[2_ n;2_ v],";",v 1]} / d v -> d[..;v] each:{[e;p;a](.{"{[e;p;",x,"]e[(",x,");p]}"}1_,/";x",/:$!#a)[e;p]} / (f'). iter:{[e;p;q;a](.{"{[e;p;q;",x,"]e[(,q),(",x,");p]}"}1_,/";x",/:$!#a)[e;p]} / (f/). // errors reserved:{'"reserved word error"} / reserved word error syntax:{'"syntax error: unbalanced delimiters ()[]{}"} / syntax error stop:{'"program stopped -- s = stack, : to resume"} / program stop // stack split/fuse vs:{((-x)_ y),/:,:'(-x)#y} / stacks <- stack sv:{((-x)_*y),,/(-x)#'y} / stack <- stacks // assignment set:{:[4=4:y;.[chk y;();:;];:["."_in y;y;y,".cl"]1:]x z;y} / set|def var or file chk:{:[j[x]_in!J;reserved[];x]} / name = primitive get:{:[4=t:4:x;exe x;3=_abs t;2::["."_in x;x;x,".cl"]]} / get var or file exe:{:[X=*$x;x;H~_n;. x;H . x]} / execute symbol run:{[f]console[()]'1!'(&blk a)_ a:Y[0],bbt@6::["."_in f:"",f;f;f,".ck"];} / run .ck script blk:{(~depth x)&unquote[x;*Y]} / mark script blocks bbt:{:[#i:_ss[x;"\\\\"];i[0]#x;x]} / take before \\ // lambda, let, shuffle let:{[s;m]:[(#s)#p;s;-3=4:p;diagram[s;p];index[s;p]]} / shuffle index:{[s;p]((-#p)_ s),{i:&b:@:'y;@[@[y;i;:;x(#x)-1+y i];j;:;_f'[x j;y j:&~b]]}[(-#p)#s;p]} / shuffle by index diagram:{[s;p](r _ s),fromto[(r:-#*q)#s]. q:({x nest x}1_)'(&p=":")_ p:":",p} / shuffle by diagram fromto:{[s;f;t]to[asym t;. from[asym f;s]]} / nested shuffle asym:{:[@x;`$x;_f'x]} / symbolize each atom into:{[p;d]:[~@p;scope[p;d];(t:name p)_in!d;d t;p]} / map vals into list from dictionary scope:{[p;d]@[p;{&~x|1!x}p~'J`let_;into;d]} / skip nested lets from:{[f;s]:[f~`;();@f;,(`$$f;s);0>4:f;f_match[f;s];,/f _f's]} / map vals from list to dictionary f_match:{[f;s],/f from' :[~|/b:uc0'f;s;1=#f;,s;f_cut[b;s]]} / from-pattern-match f_cut:{[b;s]{,/@[x,||:'(0,(|b)?1)_|y;~*b;,:]}.(0,b?1)_ s} / from-pattern-cut to:{[t;d]:[~@t;,/t _f'd;uc0 t;,d t;d t]} / map vals to list from dictionary ia_:{[i;p]@[1+!#p;_n;{:[@z;,x,y*1 -1 uc0 z;,/ia_[x,y;z]]}[i];p]};ia:{,/ia_[!0;name x]} / ia p:(`a;(`b;(`c`d`E))) pick:{[s;i]((-k)_ s),{:[0>k:*|y;(-k+1)_ x .-1_ y-1;x . y-1]}'[s j+-1+(#s)-k:|/j:*:'i;1_'i]} / pick[s;ia p] uc0:{(*$x)_in 26_*A} / upper-case at 0 // K + V:":.";G:"~!@#$%^&*_-+=|<,>?:.";P:.:''++(G;G,'V 0;" ") / glyphs, primitives K:();{K,:o[`$x;d2[y]];K,:o[`$x,V 0;d1[z]];K,:o[`$x,V 1;d2[{x[z;y]}[y]]]}'[G;*P;P 1]; / v v: v. (commute) G,:"/";P[],:(;;);K,:(o[`"/";d2[_%]];o[`$"/",V 0;d1[_%:]];o[`$"/",V 1;d2[{_ y%x}]]) / joy division is / G,:"\\";P[],:(;;);K,:(o[`"\\";d2[{y;}]];o[`$"\\",V 0;s0[{x,k@*V}]];o[`$"\\",V 1;s0[{x,k V 1}]]) / escape ... K:+((#G),-1)#K / (dyad;monad;commute) K[0;G?/:"@."]:{o[`$x;d2[{x[y;idot z]}[. x]]]}'"@."; / redefine @ . k:{:[(#G)>i:G?*x;K[" :."?*1_ x," ";i]]} / "+" -> + j:{:[(#j:!J)>i:I[2]?x;J j i]} / f -> J.f_ jn:{:[@x;`$:["_"=*|x$:;-1_ x;x,"_"];_f'x]} / `x <-> `x_ // System functions, I/O: F:(`log`exp`abs`sqr`sqrt`floor`dot`mul`inv`sin`cos`tan`asin`acos`atan`sinh`cosh`tanh`ci`ic`bd`db`hash `draw`in`lin`bin`binl`dv`di`dvl`sv`vs`sm`ss ,`ssr) F{J[jn x]:y[."_",$x]}\:'(d1;d2;d3); {J[jn x]:d1[.($y),V 0 0]}'[{F[0],:x;x}`read0`read1`read2`connect`disconnect`type`read6;0 1 2 3 3 4 6]; {J[jn x]:d2[.($y),V 0]}'[{F[1],:x;x}`write0`write1`write2`async`sync`write6;0 1 2 3 4 6]; Z[jn"IFCSN"]:(!0;0#0.;"";0#`;);J[!Z]:{s0[{Z x}[x]]}'!Z / empty vectors, null // Joy + J.all_: s2[{[s;a;p]s,,&/{E_[s,,x;p]}'a}] J.amend3_: d3[{[v;i;f]@[v;idot i;:[_n~p:p1 f;{E_[,x;f]};p]]}] J.amend4_: d4[{[v;i;f;d]@[v;idot i;:[_n~p:p2 f;{E_[(x;y);f]};p];d]}] J.and_: d2[&] J.appl_: s2[{[s;a;p]s,,E_[s,,a;p]}] J.app11_: s3[{[s;a;b;p]s,,E_[s,(a;b);p]}] J.app12_: s4[{[s;a;b;c;p]s,(E_[s,(a;b)];E_[s,(a;c)])}] J.apply_: s2[{[s;a;p]s,a{E_[s,,x;y]}' :[1=#p;(#a)#p;p]}] J.at_: d2[@] J.b_: s2[{[s;p;q]s,s E_/:(p;q)}] J.binary_: s3[{[s;x;y;p]s,,E_[s,(x;y);p]}] J.binrec_: s4[{[s;i;t;e;f]:[E_[s;i];E[s;t];E[sv[1;_f[;i;t;e;f]'vs[2;E[s;e]]];f]]}] J.body_: d1[{[n]:[~4=4:n;();_n~r:get n;();r]}] J.branch_: s3[{[s;b;t;f]E[s;:[b;t;f]]}] J.callcc_: s1[{[s;p]E[s,,*C;p]}] J.callc_: s1[{[s;p]E[s,,C;p]}] J.case_: s2[{[s;x;y]E[s;y[y[;0]?x;1]]}] J.choice_: d3[{:[x;y;z]}] J.ck_: s1[{[s;x]E[s;p x,""]}] J.CK_: s2[{[s;x;y]h:H;H::x;r:E[s;p y,""];H::h;r}] J.cleave_: s3[{[s;a;p;q]s,{E_[s,,a;x]}'(p;q)}] J.code_: d1[{ll top x}] J.compare_: d2[{{(-x<0)+00;0;2>#x]}] J.some_: s2[{[s;a;p]s,,|/{E_[s,,x;p]}'a}] J.space_: s1[{[s;x]`0:,"s:",#_bd x;s}] J.split_: s2[{[s;a;t]s,a@/:(&::;&~:)@\:{E_[s,,x;t]}'a}] J.stack_: s0[,:] J.step_: s2[{[s;a;p]s{E[y,,z;x]}[p]/a}] J.stop_: s0[{[s]stop[];s}] J.succ_: d1[1+] J.swap_: a2[|:] J.swapd_: a3[@[;1 0 2]] J.swoncat_: d2[{y,x}] J.swons_: d2[{(,y),x}] J.sysin_: s0[{[s]s,,0:`}] J.sysout_: s1[{[s;a]`0:a;s}] J.tailrec_: s3[{[s;p;t;r]E[(~E_[;p]@)E[;r]/s;t]}] J.take_: d2[{y#x}] J.ternary_: s4[{[s;x;y;z;p]s,,E_[s,(x;y;z);p]}] J.this_: s0[{[s]s,(:[~*r:.[.:;"_v";:];`$1_'(&v=".")_ v:".",$r 1];_i)}] J.time_: s0[{[s]`0:,"t:",$100000*_T-T_;T_::_T;s}] J.times_: J`do_ J.trace_: s1[{[s;b]:[b~_n;s,,T;s,0#T::b]}] J.transit_: d3[{x z/y}] J.Transit_: d3[{x z\y}] J.transpose_: d1[+:] J.trap_: s1[{[s;e]."\\e ",$~e;s}] J.treemap_: s2[{[s;t;p]s,,{[s;t;p]:[@t;E_[s,t;p];_f[s;;p]'t]}[s;t;p]}] J.treemap2_: s3[{[s;t;u;p]s,,{[s;t;u;p]:[(@t)&@u;E_[s,t,u;p];_f[s;;;p]'[t;u]]}[s;t;u;p]}] J.treestep_: s2[{[s;t;p]:[@t;E[s,t;p];_f[;;p]/[s;t]]}] J.treerec_: s3[{[s;t;o;c]:[@t;E[s,t;o];E[s,(t;(o;c;J`treerec_));c]]}] J.treegenrec_: s4[{[s;t;o;p;c]:[@t;E[s,t;o];E[E[s,,t;p],,(o;p;c;J`treegenrec_);c]]}] J.true_: a0[{1}] J.unary_: s2[{[s;x;p]s,,E_[s,,x;p]}] J.unary2_: s3[{[s;x;y;p]s,{E_[s,,x;p]}'(x;y)}] J.unary3_: s4[{[s;x;y;z;p]s,{E_[s,,x;p]}'(x;y;z)}] J.unary4_: s5[{[s;x;y;z;w;p]s,{E_[s,,x;p]}'(x;y;z;w)}] J.uncons_: s1[{[s;x]s,(*x;1_ x)}] J.unit_: d1[,:] J.unlist_: s1[{[s;n]((-n)_ s),,/(-n)#s}] J.unstack_: s0[{*-1#x}] J.unswons_: s1[{[s;x]s,(1_ x;*x)}] J.while_: s3[{[s;c;a;b]m:_n~p:p1 a;n:_n~q:p1v b;:[m&n;(E_[;a]@)E[;b]/s,,c;m;(E_[;a]@)s,,q/c;n;(p@*|:)E[;b]/s,,c;s,,p q/c]}] J.While_: s3[{[s;c;a;b]m:_n~p:p1 a;n:_n~q:p1v b;:[m&n;(E_[;a]@)E[;b]\s,,c;m;(E_[;a]@)s,,q\c;n;(p@*|:)E[;b]\s,,c;s,,p q\c]}] J.y_: s1[{[s;p]E[s,,(p;J`y_);p]}] J.x_: s1[{[s;p]E[s,,p;p]}] J.zip_: d2[{+(x;y)}] J:@[J;_n;{o[jn y;x]};!J] / word -> operator I:{(x;_hash x)}'((,/K 0 2)@'1;K[1]@'1;J[]@'1) / hash K, J // Tokenize A:("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";"0123456789") / alpha L:"[]" / list D:"()dictionary" / dictionary M:"{}function" / function Q:"\"" / double quote R:"'" / single quote X:"\\" / escape quote, comment U:"`" / symbol quote B:" " / blank Y:";\n\r\t" / = blank O:(Q,R,B,V,L,(2#D),(2#M),".-0vn";(Q,R,B,V,L,(2#D),(2#M),".-"),(A 1;G)) / (types;tokens) token:{[s] / string -> token-list f:_ci 255 / fill k:string decomment dex s;s:*k;q:k 1 / a"xyz"b -> (a"b;"xyz") s:_ssr[;;B]/[s;Y] / ;\n\r\t -> " " s:_ssr[s;U;B,U] / `a`b -> `a `b s:_ssr[s;R;B,R] / 'a'b -> 'a 'b t:O[0;(-1++\#:'O 1)_binl(,/O 1)?/:s] / types t:_ssr[t;R,"n";"nn"] / 'n -> nn t:_ssr[_ssr[t;"-.0";"nnn"]_ssr[;;"nn"]/("-0";".0");"0";"n"] / numbers t:_ssr[t;"-";"v"] / - t:_ssr[_ssr[t;"n.";"nn"];".n";"nn"] / dot notation t:t{_ssr[;y,"n";y,f]/x}/"n",f / nn -> n,f t:t{_ssr[x;".",y;"v",f]}/V / .: -> vf, .. -> vf t:t{_ssr[x{_ssr[y;z,*x;z,x 1]}[y,f]/"v",y;y;"v"]}/V / vV -> vf, VV -> Vf, V -> v t:_ssr[t;".";"v"] / . -> v i:&~t=B;t@:i;s@:i / blank -> s:s{_ssr/[x;2#y;@[L;1;,;2_ y]]}/(D;M) / ()|{} -> []dictionary|[]function t:t{_ssr/[x;2#y;@[L;1;,;"n",(-3+#y)#f]]}/(D;M) / ()|{} -> []nfffffffff|[]nfffffff t:_ssr[t;L;"n",f] / L -> n if[~balanced s;syntax[]] / check balanced [] p:&~t=f;t@:p;s:p _ s / x,f -> x i:&t=Q;s[i]:q;t[i]:"n" / (a;"";b) -> (a;"xyz";b) {x nest y}. symbol[s;t]} / ` "abc" -> `"abc", nest balanced:{[s](~*|k)&&/-1 n nest:{*{y . x}/[(();,0;0) / nest on L ({(x;@[-1_ y;-2+#y;+;1];z+1)} / L 1 {(.[x;-1_ y;,;z];@[y;-1+#y;+;1];z+1)} / x {(.[x;-1_ y;,;,!0];y,0;z+1)})1+-':0,depth x]} / L 0 depth:-/+\'L=\: / depth of L // Type type:{ffunc fesc map x} / map strings to values fesc:{@[x;&~4::'x;eesc]} / find nested escapes eesc:{:[0>t:4:x;x;~t;_f'[x]_dv k X;x~k X,*V;k@*V;x~k X,V 1;k V 1;x]} / \ -> (), \: -> :, \. -> . ffunc:{:[@x;x;~#x;x;efunc@_f'x]} / find function_ efunc:{:[~#i:_dv[&x~'J`function_;0];x;@[x;-1+i;{f[x]}]_di i@:&~@:'x i-1]} / eliminate function_ map:{:[~4:x;_f'x;&/x _lin G;k x;word x]} / strings -> values word:{:[Q~*x;"",. x;lit x;. char x;cons jn`$x]} / word value char:{:[R _in x;_ssr[x;R;Q],Q;x]} / 'x -> "x" cons:{:[x _in!Z;Z x;x _in!J;J x;u[jn x]]} / constant value lit:{:[(*x)_in R,U;1;~|/x _lin A 1;0;(x[0]_in A[1],".-")&&/x _lin(,/A),".-"]} / literal // Display T:0 / trace W:-100 0I / display width, display count display:{if[x;line out[W;y]];y} / display line:{if[#x;`0:,nul x]} / line out nul:{@[x;&x="\000";:[;" "]]} / can't print \000 out:{wid[*x;top trc[x;rev[*x;y]]]} / show stack trc:{:[@x;y;~(#y)>x 1;y;~x 1;();(x[1]*(-0>*x)+0<*x)#y]} / truncate stack top:{:[-3=4:x;,/R,'x;unl rep x]} / vector -> list unl:{:[L[0]=*x;1_-1_ x;x]} / "[x]" -> "x" rev:{:[x>0;|y;y]} / reverse stack display wid:{:[_abs[x]>#y;y;(2*x>0)!".. ",x#y]} / maximum display width rep:{:[7=t:4:x;self;t<0;vec;t>0;atom;list]x} / represent stack self:{:[(#p)>i:(p:,/+P)?x;ll(,/+G,\:/:(,"";V))i;$x[0]]} / primitive or operator vec:{:[~#x;$jn(!Z)Z[]?x;-3=t:4:x;qq x;ll@5:one x]} / vector display atom:{:[3=t:4:x;R,x;5=t;dict x;6=t;"N";5:x]} / atom display dict:{dd 1_-1_ rep[. x]} / dictionary display list:{ll bx 1_,/(B,rep@)'x} / list display bx:{_ssr[x;X," ";X]} / A \ B -> A \B dd:{-1!x,|2#D} / x -> (x) mm:{-1!x,|2#M} / x -> {x} ll:{-1!x,|L} / x -> [x] qq:{Q,_ssr[x;Q;X,Q],Q} / x -> "x" // Evaluation S:C:() / stack, continuations T_:_T / base time N:0 / self-evaluating stack? console:{T_::_T;display[~Y[0]=*-1#y _dv B;eval[x]. esc p y,""]} / console: evaluate, display esc:{(~-1!b;x)_di\:&b:x~\:k X} / eliminate \, mark \-ed elements eval:{C::(,z),C;r:a/[x;y;z];C::1_ C;r} / evaluate E:eval[;1] / recursive evaluation p:type token@ / parse a:{t[x;z];C[0]::1_*C;:[y&7=4:z;b/n z x;x,,z]} / apply b:{:[N;()a[;1]/x;x]} / resolve stack if N t:{if[T;line@$[3+__abs T;out[T;x]],"\t",out[T;,y]]} / trace n:{:[#x;(),x;()]} / null E_:{*-1#E[x;y]} / .. x e:E[()] / evaluate on () d:{out[W;x]} / monadic display r:{C::();if[#x;:[x~"\\\\";_exit 0;x~,"\\";.m[`p`r]:_n;S::console[S;x]]]} / respond i:{S::x;.m.p:" ";.m.r:r} / interpret .m[`s`g]:x:e p"", / ck execute (external) \d .k `0:,"cK 2004-05-10\n\\ for k, \\\\ to exit, .ck.i() for cK\n";.ck.run'_i;.ck.i()