diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-12-03 14:46:22 -0500 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-12-03 15:05:47 -0500 |
| commit | fff1c8c635ca370f5ab306936a97b4c3f21a8619 (patch) | |
| tree | 397a56b0a7501b3f5d443e554807cb5010cb5080 | |
| parent | f58a6ee111fc572435094fcd3929baccb7975148 (diff) | |
Derived function and train formatting
| -rw-r--r-- | docs/bqn.js | 23 | ||||
| -rw-r--r-- | md.bqn | 2 | ||||
| -rw-r--r-- | src/f.bqn | 27 |
3 files changed, 34 insertions, 18 deletions
diff --git a/docs/bqn.js b/docs/bqn.js index 667b3885..d63627f4 100644 --- a/docs/bqn.js +++ b/docs/bqn.js @@ -128,7 +128,7 @@ let scan = f => setrepr(()=>[4,table,f], (x,w) => { } return arr(r,s); }); -let cases = (f,g) => setrepr(()=>[5,cases,f,g], +let cases = (f,g) => setrepr(()=>[5,f,cases,g], (x,w)=>has(w)?call(g,x,w):call(f,x,w)); table.m=scan.m=1; cases.m=2; let group_len = (x,w) => { // ≠¨⊔ for a valid list argument @@ -189,17 +189,18 @@ runtime.map((r,i) => { let bqn = src => run.apply(null,compile(str(src),runtime)); // Formatter -let fmt0 = x => - typeof x === "number" ? - (x==Infinity?"∞":x==-Infinity?"¯∞":(""+x).replace(/-/g,"¯")) - : typeof x === "function" ? - (x.glyph||(x.m?"*"+x.m+"-modifier*":"*function*")) - : ""+x; +let fmtnum = x => str(x==Infinity ? "∞" : x==-Infinity ? "¯∞" + : (""+x).replace(/-/g,"¯")); +let type = x => typeof x === "function" ? 3+(x.m||0) + : x.sh ? 0 : 1+(typeof x === "number"); +let decompose = x => list(typeof x !== "function" ? [-1,x] + : x.glyph ? [0,x] : x.repr ? x.repr() : [1,x]); +let glyph = x => x.glyph; let fmt1 = run( - [15,1,25,21,0,1,22,0,2,11,14,0,60,0,14,0,17,0,17,19,0,36,0,61,8,3,2,0,39,0,9,0,36,0,52,8,8,21,0,2,3,2,0,39,0,52,0,37,0,7,8,8,0,18,9,22,0,3,11,14,15,2,22,0,4,11,14,0,10,0,7,0,46,19,0,41,15,3,8,22,0,5,11,14,15,4,22,0,6,11,14,15,5,22,0,7,11,14,15,6,15,7,3,2,0,39,15,8,0,45,3,2,0,39,0,9,0,37,0,9,0,36,0,45,8,0,35,0,6,8,8,8,8,22,0,8,11,14,0,70,0,18,0,69,17,15,9,15,10,3,2,0,39,0,9,0,10,0,42,19,0,6,0,10,0,9,0,46,19,19,8,3,2,0,39,0,13,0,13,0,42,0,42,3,2,19,8,22,0,9,11,14,15,11,22,0,10,11,14,15,12,22,0,11,11,14,15,13,15,14,15,15,3,3,0,39,0,26,0,52,7,0,7,0,16,19,0,34,0,31,0,0,7,0,11,0,0,0,7,0,36,0,42,8,19,0,9,19,8,8,22,0,12,11,14,21,0,3,21,0,12,21,0,11,3,3,0,39,0,12,0,3,0,46,19,8,22,0,13,11,14,21,0,13,0,34,0,28,0,51,0,0,0,52,17,0,37,0,17,8,7,8,0,16,9,0,20,0,43,19,25,0,45,0,29,0,19,7,21,0,1,0,1,16,17,0,22,0,38,0,33,0,27,0,30,0,2,7,7,7,8,16,0,31,0,43,0,44,3,2,0,40,0,0,8,7,21,0,2,17,0,16,16,0,25,0,36,0,43,8,0,38,0,42,8,16,25,21,0,1,0,13,16,0,20,0,43,17,22,0,3,11,0,4,0,45,17,21,1,4,16,22,0,4,11,14,15,16,22,0,5,11,14,21,0,1,0,16,21,0,1,0,13,16,0,19,0,43,17,0,17,21,0,3,0,31,0,2,7,16,17,17,0,19,0,36,0,31,0,0,7,8,21,0,5,3,2,0,39,0,15,0,34,0,9,8,0,7,0,42,19,8,21,0,4,17,25,21,0,1,0,16,16,0,29,0,26,0,53,7,0,16,0,13,0,25,0,43,19,0,37,0,17,8,19,0,17,0,15,19,7,21,0,1,0,13,16,21,1,4,0,45,17,17,0,17,16,25,0,53,0,16,21,0,1,0,9,16,21,0,2,3,2,17,22,0,3,11,14,21,0,3,21,0,1,21,0,3,3,3,0,18,16,0,17,16,25,0,64,0,17,21,0,1,0,24,16,0,20,0,45,17,0,20,0,43,17,17,0,17,0,63,17,0,18,16,25,21,0,1,0,13,16,0,25,0,43,17,22,0,3,11,14,0,65,21,0,2,0,15,0,3,0,45,19,0,27,0,24,7,0,66,19,0,16,9,21,1,2,3,2,0,39,0,7,0,36,0,47,8,8,16,3,2,0,17,16,0,19,21,0,3,17,0,18,16,21,0,1,0,25,0,38,0,67,0,25,0,45,0,1,21,0,2,17,0,3,0,48,17,0,4,0,42,17,17,8,16,0,68,0,19,0,36,0,1,8,21,0,3,17,0,18,16,3,3,0,17,16,25,21,0,1,0,24,16,0,30,0,10,7,0,62,17,0,32,0,1,7,16,0,33,0,0,7,16,0,10,0,46,17,0,31,0,6,7,16,25,21,0,1,0,29,0,71,7,16,21,1,7,0,45,17,21,1,8,0,46,17,0,25,0,36,0,42,0,43,3,2,8,0,38,0,54,8,16,25,21,0,1,0,13,16,0,62,0,29,21,1,2,7,0,29,0,17,0,36,0,72,8,7,9,0,17,9,0,25,0,38,0,55,8,9,3,2,0,39,0,9,0,7,0,45,19,8,16,0,18,16,25,21,0,1,0,29,0,13,7,16,22,0,3,11,14,21,0,3,0,29,0,25,7,0,45,17,0,16,0,26,0,45,7,0,1,0,10,19,0,41,0,32,0,4,7,8,3,2,0,39,0,10,0,7,0,42,19,8,16,22,0,4,11,14,21,0,3,0,29,0,25,7,16,0,10,0,7,0,42,19,0,41,0,45,0,40,0,32,0,4,7,8,8,16,22,0,5,11,14,21,0,1,0,29,0,19,7,0,45,0,19,21,0,4,0,9,16,0,2,21,0,2,17,0,1,16,17,0,5,21,0,4,17,0,2,21,0,2,17,0,30,0,17,7,21,0,5,17,17,0,10,0,1,0,46,19,0,4,0,42,19,0,41,0,18,8,16,0,46,0,40,0,17,0,34,0,7,8,8,16,22,0,6,11,14,21,0,6,21,1,6,0,8,0,34,21,1,5,8,3,2,0,39,21,0,5,0,16,16,0,12,0,36,0,22,0,36,0,45,8,8,16,8,16,21,1,7,0,46,17,25,21,0,1,0,29,21,1,13,7,16,21,1,10,0,45,0,14,21,0,2,17,17,21,1,8,21,0,1,0,10,16,17,25,21,0,1,0,13,16,0,20,0,43,17,22,0,3,11,0,9,16,0,1,16,0,10,0,36,0,42,8,0,41,0,44,8,16,22,0,4,11,14,0,56,22,0,5,11,14,15,17,22,0,6,11,14,21,0,1,0,23,0,36,0,15,0,10,21,0,5,19,0,0,0,45,19,8,21,0,6,3,2,0,39,0,10,0,9,0,45,19,8,16,22,0,1,12,14,21,0,1,0,10,16,22,0,7,11,14,21,0,5,0,19,0,36,0,1,8,21,0,3,17,21,0,4,0,40,0,17,8,21,0,1,17,21,0,4,0,40,0,17,8,0,59,0,19,21,0,3,0,17,0,45,17,0,25,0,43,17,17,0,16,21,0,3,17,0,25,0,38,21,0,5,8,16,17,0,18,16,21,0,7,0,9,0,45,17,0,41,21,1,5,21,1,7,0,45,19,21,1,8,21,0,7,19,8,16,25,21,0,1,21,1,11,21,1,9,3,2,0,39,0,13,0,10,0,42,19,0,31,0,6,7,9,8,16,25,21,0,1,21,1,11,0,43,17,25,21,0,1,0,17,0,36,0,24,0,34,0,29,0,53,7,8,8,16,0,24,21,0,2,0,0,0,45,17,0,17,0,42,17,0,23,16,0,2,0,36,0,7,0,36,0,21,8,8,16,17,25,0,50,0,0,0,52,17,0,7,21,0,1,17,0,2,0,52,0,1,0,58,17,17,0,0,0,49,0,0,0,52,17,0,15,0,1,0,57,19,0,2,0,10,19,21,0,1,17,17,0,0,21,0,1,17,25] - ,[runtime[0],runtime[1],runtime[2],runtime[6],runtime[7],runtime[9],runtime[11],runtime[12],runtime[13],runtime[14],runtime[15],runtime[16],runtime[18],runtime[19],runtime[20],runtime[21],runtime[22],runtime[23],runtime[24],runtime[25],runtime[26],runtime[29],runtime[30],runtime[32],runtime[35],runtime[36],runtime[43],runtime[44],runtime[45],runtime[46],runtime[47],runtime[49],runtime[50],runtime[51],runtime[52],runtime[53],runtime[54],runtime[55],runtime[56],runtime[58],runtime[59],runtime[61],0,-1,Infinity,1,2,5,4,127,32,10,' - ,[[0,1,0,0],[1,1,3,14],[0,0,311,3],[0,0,377,6],[0,0,477,3],[0,0,528,4],[0,0,567,3],[0,0,597,4],[0,0,720,3],[0,0,758,3],[0,0,797,3],[0,0,850,7],[0,0,1080,3],[0,0,1113,8],[0,0,1319,3],[0,0,1348,3],[0,0,1358,3],[0,0,1410,3]] -)(a=>str(fmt0(a))); + [15,1,25,21,0,1,22,0,2,22,0,3,22,0,4,22,0,5,4,4,11,14,0,64,0,14,0,17,0,17,19,0,37,0,65,8,3,2,0,40,0,9,0,37,0,54,8,8,21,0,5,3,2,0,40,0,54,0,38,0,7,8,8,0,18,9,22,0,6,11,14,15,2,22,0,7,11,14,0,10,0,7,0,47,19,0,42,15,3,8,22,0,8,11,14,15,4,22,0,9,11,14,15,5,22,0,10,11,14,15,6,15,7,3,2,0,40,15,8,0,46,3,2,0,40,0,9,0,38,0,9,0,37,0,46,8,0,36,0,6,8,8,8,8,22,0,11,11,14,0,74,0,18,0,73,17,15,9,15,10,3,2,0,40,0,9,0,10,0,43,19,0,6,0,10,0,9,0,47,19,19,8,3,2,0,40,0,13,0,13,0,43,0,43,3,2,19,8,22,0,12,11,14,15,11,22,0,13,11,14,15,12,22,0,14,11,14,15,13,15,14,15,15,3,3,0,40,0,27,0,54,7,0,7,0,16,19,0,35,0,32,0,0,7,0,11,0,0,0,7,0,37,0,43,8,19,0,9,19,8,8,22,0,15,11,14,21,0,6,21,0,15,21,0,14,3,3,0,40,0,12,0,3,0,47,19,8,22,0,16,11,14,15,16,22,0,17,11,14,0,15,0,15,21,0,16,9,21,0,17,0,26,0,46,19,0,18,9,3,2,0,40,0,14,0,11,0,52,19,8,21,0,2,19,22,0,18,11,14,21,0,18,0,35,0,29,0,53,0,0,0,54,17,0,38,0,17,8,7,8,0,16,9,0,20,0,44,19,25,0,46,0,30,0,19,7,21,0,1,0,1,16,17,0,23,0,39,0,34,0,28,0,31,0,2,7,7,7,8,16,0,32,0,44,0,45,3,2,0,41,0,0,8,7,21,0,2,17,0,16,16,0,26,0,37,0,44,8,0,39,0,43,8,16,25,21,0,1,0,13,16,0,20,0,44,17,22,0,3,11,0,4,0,46,17,21,1,7,16,22,0,4,11,14,15,17,22,0,5,11,14,21,0,1,0,16,21,0,1,0,13,16,0,19,0,44,17,0,17,21,0,3,0,32,0,2,7,16,17,17,0,19,0,37,0,32,0,0,7,8,21,0,5,3,2,0,40,0,15,0,35,0,9,8,0,7,0,43,19,8,21,0,4,17,25,21,0,1,0,16,16,0,30,0,27,0,55,7,0,16,0,13,0,26,0,44,19,0,38,0,17,8,19,0,17,0,15,19,7,21,0,1,0,13,16,21,1,7,0,46,17,17,0,17,16,25,0,55,0,16,21,0,1,0,9,16,21,0,2,3,2,17,22,0,3,11,14,21,0,3,21,0,1,21,0,3,3,3,0,18,16,0,17,16,25,0,68,0,17,21,0,1,0,25,16,0,20,0,46,17,0,20,0,44,17,17,0,17,0,67,17,0,18,16,25,21,0,1,0,13,16,0,26,0,44,17,22,0,3,11,14,0,69,21,0,2,0,15,0,3,0,46,19,0,28,0,25,7,0,70,19,0,16,9,21,1,5,3,2,0,40,0,7,0,37,0,48,8,8,16,3,2,0,17,16,0,19,21,0,3,17,0,18,16,21,0,1,0,26,0,39,0,71,0,26,0,46,0,1,21,0,2,17,0,3,0,49,17,0,4,0,43,17,17,8,16,0,72,0,19,0,37,0,1,8,21,0,3,17,0,18,16,3,3,0,17,16,25,21,0,1,0,25,16,0,31,0,10,7,0,66,17,0,33,0,1,7,16,0,34,0,0,7,16,0,10,0,47,17,0,32,0,6,7,16,25,21,0,1,0,30,0,75,7,16,21,1,10,0,46,17,21,1,11,0,47,17,0,26,0,37,0,43,0,44,3,2,8,0,39,0,56,8,16,25,21,0,1,0,13,16,0,66,0,30,21,1,5,7,0,30,0,17,0,37,0,76,8,7,9,0,17,9,0,26,0,39,0,57,8,9,3,2,0,40,0,9,0,7,0,46,19,8,16,0,18,16,25,21,0,1,0,30,0,13,7,16,22,0,3,11,14,21,0,3,0,30,0,26,7,0,46,17,0,16,0,27,0,46,7,0,1,0,10,19,0,42,0,33,0,4,7,8,3,2,0,40,0,10,0,7,0,43,19,8,16,22,0,4,11,14,21,0,3,0,30,0,26,7,16,0,10,0,7,0,43,19,0,42,0,46,0,41,0,33,0,4,7,8,8,16,22,0,5,11,14,21,0,1,0,30,0,19,7,0,46,0,19,21,0,4,0,9,16,0,2,21,0,2,17,0,1,16,17,0,5,21,0,4,17,0,2,21,0,2,17,0,31,0,17,7,21,0,5,17,17,0,10,0,1,0,47,19,0,4,0,43,19,0,42,0,18,8,16,0,47,0,41,0,17,0,35,0,7,8,8,16,22,0,6,11,14,21,0,6,21,1,9,0,8,0,35,21,1,8,8,3,2,0,40,21,0,5,0,16,16,0,12,0,37,0,23,0,37,0,46,8,8,16,8,16,21,1,10,0,47,17,25,21,0,1,0,30,21,1,18,7,16,21,1,13,0,46,0,14,21,0,2,17,17,21,1,11,21,0,1,0,10,16,17,25,21,0,1,0,13,16,0,20,0,44,17,22,0,3,11,0,9,16,0,1,16,0,10,0,37,0,43,8,0,42,0,45,8,16,22,0,4,11,14,0,58,22,0,5,11,14,15,18,22,0,6,11,14,21,0,1,0,24,0,37,0,15,0,10,21,0,5,19,0,0,0,46,19,8,21,0,6,3,2,0,40,0,10,0,9,0,46,19,8,16,22,0,1,12,14,21,0,1,0,10,16,22,0,7,11,14,21,0,5,0,19,0,37,0,1,8,21,0,3,17,21,0,4,0,41,0,17,8,21,0,1,17,21,0,4,0,41,0,17,8,0,61,0,19,21,0,3,0,17,0,46,17,0,26,0,44,17,17,0,16,21,0,3,17,0,26,0,39,21,0,5,8,16,17,0,18,16,21,0,7,0,9,0,46,17,0,42,21,1,8,21,1,10,0,46,19,21,1,11,21,0,7,19,8,16,25,21,0,1,21,1,14,21,1,12,3,2,0,40,0,13,0,10,0,43,19,0,32,0,6,7,9,8,16,25,21,0,1,21,1,14,0,44,17,25,0,77,0,78,0,79,0,80,3,4,0,30,0,14,0,17,0,17,19,7,0,62,17,22,0,3,11,14,0,15,0,15,0,35,21,1,16,0,35,21,0,3,0,26,16,0,25,3,2,0,40,0,9,0,10,0,46,19,8,8,8,0,7,0,36,0,18,8,0,43,19,21,1,17,3,2,0,40,0,14,0,11,0,52,19,8,21,1,2,19,22,0,4,11,14,21,0,1,21,1,3,16,22,0,5,11,0,26,16,22,0,6,11,14,0,63,0,1,0,81,17,0,26,21,0,6,17,22,0,7,11,14,0,83,0,38,0,17,8,0,17,0,82,19,22,0,8,11,14,0,15,0,30,21,0,4,0,38,0,26,0,37,0,46,8,0,14,0,11,21,0,7,0,3,0,47,17,19,0,42,0,15,0,35,21,0,8,8,8,0,26,0,38,0,0,8,19,8,7,0,9,0,35,0,21,8,0,10,0,43,19,0,1,9,0,46,0,8,21,0,7,17,0,42,0,23,8,9,19,0,17,9,22,0,9,11,14,21,0,5,0,20,0,46,17,0,27,21,0,1,7,21,1,4,0,35,0,16,8,9,21,0,3,0,40,0,47,0,1,21,0,2,17,8,21,0,9,3,3,0,40,21,0,6,0,3,0,47,17,8,16,22,0,10,11,14,21,0,7,21,0,10,3,2,25,21,0,1,0,17,0,37,0,25,0,35,0,30,0,55,7,8,8,16,0,25,21,0,2,0,0,0,46,17,0,17,0,43,17,0,24,16,0,2,0,37,0,7,0,37,0,22,8,8,16,17,25,0,51,0,0,0,54,17,0,7,21,0,1,17,0,2,0,54,0,1,0,60,17,17,0,0,0,50,0,0,0,54,17,0,15,0,1,0,59,19,0,2,0,10,19,21,0,1,17,17,0,0,21,0,1,17,25] + ,[runtime[0],runtime[1],runtime[2],runtime[6],runtime[7],runtime[9],runtime[11],runtime[12],runtime[13],runtime[14],runtime[15],runtime[16],runtime[18],runtime[19],runtime[20],runtime[21],runtime[22],runtime[23],runtime[24],runtime[25],runtime[26],runtime[27],runtime[29],runtime[30],runtime[32],runtime[35],runtime[36],runtime[43],runtime[44],runtime[45],runtime[46],runtime[47],runtime[49],runtime[50],runtime[51],runtime[52],runtime[53],runtime[54],runtime[55],runtime[56],runtime[58],runtime[59],runtime[61],0,-1,Infinity,1,2,5,4,127,32,3,10,' + ,[[0,1,0,0],[1,1,3,19],[0,0,369,3],[0,0,435,6],[0,0,535,3],[0,0,586,4],[0,0,625,3],[0,0,655,4],[0,0,778,3],[0,0,816,3],[0,0,855,3],[0,0,908,7],[0,0,1138,3],[0,0,1171,8],[0,0,1377,3],[0,0,1406,3],[0,0,1416,11],[0,0,1721,3],[0,0,1773,3]] +)(list([type, decompose, glyph, fmtnum])); let fmt = x => fmt1(x).map(c=>c===0?" ":c).join(""); if(typeof module!=='undefined'){module.exports=bqn;} @@ -622,7 +622,7 @@ GetMultiHighlights ← { # Format an array to a character matrix # Won't work on functions until we can catch errors fmtm ← ⟨"src/f.bqn"⟩ •Import "dzref" -Fmt ← ⍕ _fmtm +Fmt ← ⟨•Type,•Decompose,•Glyph,⍕⟩ _fmtm ################################ @@ -1,7 +1,7 @@ # Format an array to a string including newlines { - F0←𝔽 # Format a number or operation to a string - FmtAtom ← ≍ <⟜@◶⟨@⊸≠◶⟨"@","'"⊸(∾∾⊣)⟩,F0⟩ + ⟨Type,Decomp,FF,FN⟩←𝕗 # Format a number or operation to a string + FmtAtom ← ≍ <⟜@◶⟨@⊸≠◶⟨"@","'"⊸(∾∾⊣)⟩,FN⟩ # Vertical padding for arrays of rank greater than 2 PadCount ← { @@ -34,7 +34,7 @@ # General case l ← ¯1 ⊑ ≢𝕩 ∾ ⟨ - ≍l↑∾⟨"┌",(5⊸<)◶⟨⥊"·─"⊏˜1⌊⊢,F0⟩𝕨⟩ + ≍l↑∾⟨"┌",(5⊸<)◶⟨⥊"·─"⊏˜1⌊⊢,FN⟩𝕨⟩ ((0⌈4⌊𝕨-1)⊑"·╵╎┆┊")⌾⊑ 𝕩 ≍l-⊸↑"┘" ⟩ @@ -43,7 +43,7 @@ FmtEmpty ← (0‿0≢≢)◶("┌┐"≍"└┘")‿(((2≠=)∨0=≠)◶{ '┐'⌾(0‿¯1⊸⊑) 2 Enframe 1 PadH " "¨𝕩 }‿{ - ≍(1<≠)◶⟨"⟨⟩",'↕'⌾⊑·∾·"‿"⊸∾¨F0¨⟩≢𝕩 + ≍(1<≠)◶⟨"⟨⟩",'↕'⌾⊑·∾·"‿"⊸∾¨FN¨⟩≢𝕩 }) PaddingJoin ← { @@ -76,8 +76,23 @@ ¯1 FmtMixed 𝕩 } - # Format to character matrix - Fmt ← (2⌊≡)◶FmtAtom‿FmtSimple‿FmtMixed + # Format data type + FmtDat ← (2⌊≡)◶FmtAtom‿FmtSimple‿FmtMixed + + # Format part of a compound operation; return precedence‿string + FmtOp ← { + tn ← '*'(∾∾⊣)¨"array"‿"function"‿"1-modifier"‿"2-modifier" + FmtComp ← Type (3≤⊣)◶⟨0≍○<(1=≠)◶⟨⊑tn,⊏⟩∘FmtDat∘⊢, FmtOp⟩ ⊢ + k ← ⊑ d ← Decomp 𝕩 + p ← k ⊑ "00321111"-'0' + Paren ← "("∾∾⟜")" + FromComp ← ∾(⌽⍟(p>1)·-0=↕∘≠)(+⟜⊑Paren∘⊢⍟((2⌊p)≤⊣)1⊸⊑)⟜FmtComp¨⊢ + s ← (2⌊k)◶⟨⥊∘FF𝕩˙,(𝕨-2)◶tn,FromComp⟩ 1↓d + p‿s + } + + # Format any value to character matrix + Fmt ← Type (3≤⊣)◶⟨FmtDat⊢,≍1⊑FmtOp⟩ ⊢ # Convert to string ¯1↓·⥊ ∾⟜(@+10)˘∘Fmt |
