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)

1  
// for stuff like #1000478
2  
3  
static class LIntersperse extends LearnerImpl {
4  
  static boolean debug;
5  
  ReversibleFunction preprocessor; // e.g. JavaTok
6  
  L<S> outPrefix, outSuffix, filler;
7  
8  
  *(ReversibleFunction *preprocessor) {}
9  
  
10  
  public void processInOut(Object _in, Object _out) {
11  
    if (debug)
12  
      debug("i/o " + _in + " " + _out);
13  
    L<S> in = cast preprocessor.process(_in);
14  
    L<S> out = cast preprocessor.process(_out);
15  
    int i = out.indexOf(in.get(1));
16  
    outPrefix = out.subList(0, i);
17  
    int j = out.lastIndexOf(in.get(in.size()-2));
18  
    outSuffix = out.subList(j+1, out.size());
19  
    int newLength = j-i+1;
20  
    int realOldLength = (in.size()-1)/2;
21  
    
22  
    /*int intersperse = 0;
23  
    if (realOldLength > 1)
24  
      intersperse = newLength-realOldLength*3-1;*/
25  
    int intersperse = 3; // just assume this for now
26  
    if (realOldLength > 1)
27  
      filler = out.subList(i+1, i+1+intersperse);
28  
    
29  
    if (debug)
30  
      debug("oldLength= " + in.size() + ", newLength=" + newLength +", realOldLength=" + realOldLength + ", intersperse=" + intersperse + ", filler length=" + (filler == null ? 0 : filler.size()));
31  
  }
32  
  
33  
  public Object processIn(Object _in) {
34  
    L<S> l = cast preprocessor.process(_in);
35  
    if (debug)
36  
      debug("i " + structure(l));
37  
    new L<S> list;
38  
    list.addAll(outPrefix);
39  
    if (debug)
40  
      debug("ranging from " + 1 + " to " + (l.size()-2));
41  
    for (int i = 1; i < l.size()-1; i += 2) {
42  
      list.add(l.get(i));
43  
      if (i < l.size()-2)
44  
        list.addAll(filler);
45  
    }
46  
    list.addAll(outSuffix);
47  
    Object out = preprocessor.unprocess(list);
48  
    if (debug)
49  
      debug("output: " + structure(list) + " = " + structure(out));
50  
    return out;
51  
  }
52  
}

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: 639 / 1559
Referenced in: [show references]