NB. This version was written before some of this NB. was added to the default JPlayground session. NB. I am no longer updating this version. NB. Since File and Page are now available to all users, NB. this self-installing file is no longer needed. 0!:100 jPlay=: 0 : 0 (<~.'plj'; 18!:2 <'base') 18!:2&> <"0 ;:'base pj' cocurrent'plj' NB. Attempt to make wcsize relevant to browser window size wcsize=: {{<.17 12%~ ". 2!:0 'window.innerWidth+" "+window.innerHeight'}} NB. J-Playground verbs for using JavaScript in the browser Say=: {{ NB. Should display an intermediate value y 1!:2 [2 }} Ask=: {{ NB. Web replacement for 1!:1 [1 '' Ask y : 3}. Say ' ', 2!:0 'prompt("', y, '", "', '")',~ jsEnc_pj_ x [Say y }} File=: {{ 2!:0 'window.localStorage.getItem("', y, '")' : y [2!:0 'window.localStorage.setItem("', y, '","', '")',~ jsEnc_pj_ x }} Files=: {{ if. -.y-: myItems=. '' do. [:'y must be empty' end. for_index. i.". 2!:0 '""+ window.localStorage.length' do. myItems=. myItems, <2!:0 'window.localStorage.key(', ')',~ ":index end. Across (/:Keys) myItems : 2!:0 'window.localStorage.removeItem("', '")',~ jsEnc_pj_ x }} Edit=: {{ if. -.y-: '' do. [:'y must be empty' end. 2!:0 'ecm.getDoc().getValue()' : 2!:0 'ecm.getDoc().setValue("', '")',~ jsEnc_pj_ x }} Link=: {{ 2!:0 'window.open("', '")',~ y }} Page=: {{ URL_httpget_=: y 2!:0 httpgetJS_jpacman_ RESPONSE_httpget_ }} NB. Handle error or stop with JavaScript prompt and alert debug=: {{ if. '".'-: 5!:5 <'u' do. NB. Debugging if. '*'e. {."1 [13!:18 '' do. line_pj_=: 13!:12 '' else. 13!:0 [0 return. end. while. '*'e. {."1 [13!:18'' do. 9!:29 [1 [9!:27 '". debug' if. #line_pj_ do. 2!:0 'alert ("', '")',~ jsEnc_pj_ line_pj_ end. try. select. ;2{. part_pj_=: ;:line_pj_=: Ask 'Enter a debug request' case. ']debug' do. case. ']help' do. more_pj_ HELP_pj_ case. ']alert' do. more_pj_ 13!:12 '' case. ']stack' do. more_pj_ 2}. _2}. 13!:18 '' case. ']locals' do. more_pj_ (2 2$ 0 3 7 8{ ])"1 }.13!:13 '' case. ']resume' do. 13!:4 '' return. case. ']next' do. 13!:5 '' return. case. ']return' do. 13!:6 u. ]arg_pj_ return. case. ']stop' do. 13!:3 []arg_pj_ case. ']stops' do. more_pj_ 13!:2 '' case. ']quit' do. 13!:0 [0 [9!:29 [0 return. case. do. if. #part_pj_ do. resp_pj_=: u. line_pj_ if. #resp_pj_ do. more_pj_ resp_pj_ elseif. _1 e. 4!:0 part_pj_ do. 0!:101 line_pj_ else. more_pj_ '' end. end. end. catch. more_pj_ 13!:12 '' end. end. else. NB. Setup line_pj_=: '' if. -.13!:17 '' do. 13!:0 [1 end. NB. Suspend on an error 9!:29 [1 [9!:27 '". debug' end. }} Cr=: {{ NB. Displays lines with line numbers used during debugging mon=. y Cr~ <1 [dia=. y Cr~ <2 [y=. ,y cr=. y, (>{:mon), (,':'), >{:dia ln=. 'p<[>q<] >0'8!:2 ,.(>{.mon), _, >{.dia ln=. ' ', ln (<0;~ #>{.mon)}~ ' ' ln,. cr : cr=. md (5!:7) x if. 0= #cr do. (i.0); 0 0$ '' return. end. line=. {:&> 1{"1 cr lines=. i.>:{:line lines=. lines#~ lines e. line cr=. ' ' Join~ (lines =/line)#"1 [2{"1 cr if. md~: x do. lines; cr else. (y, cr),.~ (3": md), 'p<[>q<] >0'8!:2 ,.lines end. }} Local=: {{ NB. Run u with local definition y on stack '' u Local y : find=. {{if. (#y)~: in=. ({."1 y)i. ('1234'i. {.d){ ')a'; ')c'; ''; ')d'; 'X' [d=. 5!:5 :d i: '''')}. d=. 4}. d elseif. ' : ('-: e do. d=. 4}. d if. (;:')"(')+./@E. ;:d do. d=. d}.~ -#e=. d}.~ >:1 i:~ ') " ('E. d else. d=. d}.~ -#e=. (>:d i: ')')}. d end. d=. LF,LF,~ LF Join~ ". d else. ^'DOMAIN ERROR' end. NB. Unrecognized LF,~ y, '=: {{', t, d, '}}', e else. LF,~ y, '=: ', d end. end. }} displayLines_pj_ =: {{ NB. Format an array as a vector if. 0~: L. y do. NB. Correct appearance of LF in first level boxes fix=. LF&e. &> rav=. ,y y=. ($y)$ (,y) (I.fix)}~ ([:>;._2 (10{a.),~ ])&.> rav#~ fix end. y=. ": y if. 0< r=. _1+ #$ y do. NB. {{,"2 y,"1 x}} (-r)}. LF ([:,"2 ,"1~)^: r y else. ,y end. }} NB. String verbs with .Net names Split=: {{ NB. Split x into boxes at each y if. ''-: x do. 0# a: return. end. if. ''-: y do. <@,"0 x return. end. if. 1= #y do. <;._2 x, y else. if. #now=. x noOverlap_pj_ y do. (x{.~ {.now); (#y)}. &.> now indexCut_pj_ x else. ,<,x end. end. }} " 1 NB. Put y between each box of x NB. ( ; }. ,x ,.~ y do. x (x I.@e. old)}~ {.new else. new Join~ x Split old end. }} " 1 ForEach=: {{ NB. Usage: left verb ForEach eachSize multipleEachSizeArgs if. _ -.@-: n=. {.,n do. NB. When n is _, y must already be grouped y=. |. <"1 (n,~ n%~ #y) $ y=. ,y NB. Box arguments n at a time end. > u~ &.>/ y, <"1 x,.~ <'⍕' end. x 8!:2 y }} Keys=: {{ keys=. ((' """""""""""""""##########"""""""abcdefghijklmnopqrstuvwxyz""""""abcdefghijklmnopqrstuvwxyz""""'{~ ]), '000000000000000000000000000000000b3pqmv256lkg1ds0123456789efihjcu111111111111111111111111118t7n04000000000000000000000000009oar0'{~ ]) ([:keys a.i. ])"1 >y }} Unbox=: {{ NB. Remove boxing characters from result was=. 9!:6 '' 9!:7 [11# rep=. 30{ a. 9!:7 was [now=. ":