Will 4 months ago
parent
commit
d20c7fac49
1 changed files with 39 additions and 25 deletions
  1. 39 25
      SS.k

+ 39 - 25
SS.k

@@ -1,3 +1,16 @@
+/ ====================================================================================
+/ Notes ==============================================================================
+/ ====================================================================================
+
+/ This code uses zipped lists in multiple occasions. Any hardcoded indexing in the code
+/ is most probably a result of that. 
+
+/ \d changes the namespace before import to add a prefix to the functions imported to
+/ avoid overlaps. It is used whe the imported file doesn't define a namespace itself.
+
+/ ====================================================================================
+/ Imports ============================================================================
+/ ====================================================================================
 / utils (ppr -> pretty print)
 \d u
 \l utils.k
@@ -20,13 +33,13 @@ split: {[l;di] idx: (-1,&(di~'l)),(#l); {slice[x;y[0]+1;y[1]+1]}[l]'(idx@(2':!#i
 s2n: {[s] `i$s;}; / string to number
 parseidx: {core: slice[x;1;#x]; split[core;";"]}; / parse a string containing k index
 str: `k@;
-gi: {[r;c;rb;cb] / generate indices of a rowxcol table starting from row=rb and col=cb
+gi: {[r;c;rb;cb] / generate indices of a rowxcol sheet starting from row=rb and col=cb
  i: r#(,!c)
  i: ((!r)+rb) {x,'y}'(i+cb);
  :i
 };
-hasloops: {deps:&'x; d:deps; #,/((#deps)(,/'deps@)/d)>0}; / determine if dependency table contains loops
-order: { / come up with a order of calculations for a dependency graph
+hasloops: {deps:&'x; d:deps; #,/((#deps)(,/'deps@)/d)>0}; / determine if dependency sheet contains loops
+order: { / come up with an order of calculations for a dependency graph
  deps: {$[+/x>0;&x;()]}'x;
  d: ?,/deps;
  all: !(#deps);
@@ -37,12 +50,12 @@ order: { / come up with a order of calculations for a dependency graph
 / excel formula handler ==============================================================
 / ====================================================================================
 mc: {[c;i] / generate indices for column-based indexing
- i: gi[(#table);i+1;0;c];
+ i: gi[(#sheet);i+1;0;c];
  :i
 };
 
 mr: {[r;i] / generate indices for row-based indexing
- i: gi[i+1;(#table[0]);r;0];
+ i: gi[i+1;(#sheet[0]);r;0];
  :i
 };
 
@@ -84,17 +97,19 @@ lr: {[f] / turn A3:C5 into left and right bits
 };
 
 de:{[idx;cell] / decode excel string in a cell
- ci: cell[0];
- cell: cell[1];
- idx: (*idx);
+ / unwrap input
+ ci: cell[0]; / cell index
+ cell: cell[1]; / cell string
+ idx: (*idx); / index of detected excel string in cell string
 
- f: slice[cell;idx[0];idx[1]];
- l: slice[cell;0;idx[0]];
- r: slice[cell;idx[1];#cell];
+ f: slice[cell;idx[0];idx[1]]; / excel string
+ l: slice[cell;0;idx[0]]; / what's before the string
+ r: slice[cell;idx[1];#cell]; / what's after the string
 
  (fl;fr): lr[f];
  range: dr[fl;fr];
 
+ / register dependencies
  i: (ci[0]*3) + ci[1];
  {
   i:(*y);
@@ -105,7 +120,7 @@ de:{[idx;cell] / decode excel string in a cell
  / compiled list
  cl: {i:(*x); "self[",(`k@x[0;0]),";",(`k@x[0;1]),"]"}'range;
  cl: l,"(",({x,";",y}/cl),")",r;
- table[ci[0];ci[1];1]:: cl;
+ sheet[ci[0];ci[1];1]:: cl;
 };
 
 / ====================================================================================
@@ -120,31 +135,30 @@ sectionkeys: (tstkey;cstkey;astkey);
 fname: "SS.csv";
 tname: "SS";
 lines: {x,"\n",y}/(0: fname);
-table:: csv.csv[lines];
-d: (#table)*(#table[0]);
+sheet:: csv.csv[lines];
+d: (#sheet)*(#sheet[0]);
 deps:: (d;d)#0;
 
 / excel patterns
-:pats: pat.run''table;
-u.ppr'table;
+:pats: pat.run''sheet;
+u.ppr'sheet;
 
-// start with last to maintain prior index accuracy
 lasts:{:((#x[0])-1)_x[0]}''pats;
 firsts:{:$[(#x[0])>1;((#x[0])-1)#x[0];0]}''pats;
 
+/ start with last to maintain prior index accuracy
 f: pats {
  :$[x[2]=5;y;0]
 }''lasts;
 
-ti: gi[#table;#table[0];0;0];
-ptable:: table;
-table::ti {(,x),,y}''table;
+ti: gi[#sheet;#sheet [0];0;0];
+sheet::ti {(,x),,y}''sheet; / zip sheet and indices
 
-table {
+sheet{
  :$[y~0;0;de[y;x]]
 }''f;
 
-table {
+sheet{
  :$[y~0;0;de[y;x]]
 }''firsts;
 
@@ -157,13 +171,13 @@ compstr:: "/ compiled ",fname,"\n\n";
 al: {[l;indt] tab: "" {[x;y] x,"\t"}/(indt#0); compstr:: compstr,tab,l,"\n"}; // add line with indentation
 
 al[(tname,":{[]");0];
-al["self:(", (str (#table)),";", (str (#table[0])), ")#0;\n";1];
+al["self:(", (str (#sheet)),";", (str (#sheet[0])), ")#0;\n";1];
 
 {
- d: #table;
+ d: #sheet;
  i0: `i$(_(x%d));
  i1: x-(i0*d);
- al["self[",(str(i0)),";",(str(i1)),"]: ", table[i0;i1;1], ";";1];
+ al["self[",(str(i0)),";",(str(i1)),"]: ", sheet[i0;i1;1], ";";1];
 }'ord;
 
 al[":self";1];