Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1000481 // LIntersperse (learner, works)

JavaX fragment (include)

// for stuff like #1000478

static class LIntersperse extends LearnerImpl {
  static boolean debug;
  ReversibleFunction preprocessor; // e.g. JavaTok
  L<S> outPrefix, outSuffix, filler;

  *(ReversibleFunction *preprocessor) {}
  
  public void processInOut(Object _in, Object _out) {
    if (debug)
      debug("i/o " + _in + " " + _out);
    L<S> in = cast preprocessor.process(_in);
    L<S> out = cast preprocessor.process(_out);
    int i = out.indexOf(in.get(1));
    outPrefix = out.subList(0, i);
    int j = out.lastIndexOf(in.get(in.size()-2));
    outSuffix = out.subList(j+1, out.size());
    int newLength = j-i+1;
    int realOldLength = (in.size()-1)/2;
    
    /*int intersperse = 0;
    if (realOldLength > 1)
      intersperse = newLength-realOldLength*3-1;*/
    int intersperse = 3; // just assume this for now
    if (realOldLength > 1)
      filler = out.subList(i+1, i+1+intersperse);
    
    if (debug)
      debug("oldLength= " + in.size() + ", newLength=" + newLength +", realOldLength=" + realOldLength + ", intersperse=" + intersperse + ", filler length=" + (filler == null ? 0 : filler.size()));
  }
  
  public Object processIn(Object _in) {
    L<S> l = cast preprocessor.process(_in);
    if (debug)
      debug("i " + structure(l));
    new L<S> list;
    list.addAll(outPrefix);
    if (debug)
      debug("ranging from " + 1 + " to " + (l.size()-2));
    for (int i = 1; i < l.size()-1; i += 2) {
      list.add(l.get(i));
      if (i < l.size()-2)
        list.addAll(filler);
    }
    list.addAll(outSuffix);
    Object out = preprocessor.unprocess(list);
    if (debug)
      debug("output: " + structure(list) + " = " + structure(out));
    return out;
  }
}

Author comment

Began life as a copy of #1000468

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000481
Snippet name: LIntersperse (learner, works)
Eternal ID of this version: #1000481/1
Text MD5: 9462da931c096b0d360b03a9018b75dd
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-09 02:04:33
Source code size: 1753 bytes / 52 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 636 / 1555
Referenced in: [show references]