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

84
LINES

< > BotCompany Repo | #1010430 // Work on sentenceTypeExamples using standard functions [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (12413L/87K).

1  
!7
2  
3  
sS in = 'Input, out = 'Output;
4  
5  
static L<Map> data;
6  
static JTable table;
7  
static JComboBox cbFunction;
8  
static L<HistoryEntry> history;
9  
//static Var<Int> historyIdx;
10  
static JLabel lblHistory;
11  
12  
concept HistoryEntry {
13  
  L<Map> data;
14  
  S functionApplied;
15  
}
16  
17  
concept Code {
18  
  S code;
19  
}
20  
21  
p-subst {
22  
  newCase();
23  
  db();
24  
  history = persistentList("History");
25  
  //historyIdx = persistentVar("History index", 0);
26  
  cbFunction = autoComboBox(asTreeSet(listPlus(standardFunctionNames(), "undo", "sort by input", "sort by output", "finish")));
27  
  onEnter(cbFunction, f apply);
28  
  JComponent controls = centerAndEast(withLabel("Function:", cbFunction), jbutton("Apply", f apply));
29  
  data = mapToListOfOrderedMap(sentenceTypeExamples(), in, out);
30  
  history.add(cnew(HistoryEntry, data := cloneList(data)));
31  
  table = showTable(data);
32  
  addToWindow(table, withMargin(controls));
33  
  addToWindow(table, withMargin(lblHistory = jlabel()));
34  
  update();
35  
  makeAndCall_warmUp("lower");
36  
}
37  
38  
svoid apply { q.add(r { apply_doIt(getTextTrim(cbFunction)) }); }
39  
static Q q = startQ();
40  
41  
svoid apply_doIt(fS fName) {
42  
  loading {
43  
    if (eqic("undo", fName)) {
44  
      if (l(history) < 2) { infoBox("Nothing to undo"); ret; }
45  
      S f = popLast(history).functionApplied;
46  
      infoBox("Undoing " + f);
47  
      HistoryEntry e = last(history);
48  
      data = e.data;
49  
    } else try {
50  
      if (eqic("sort by output", fName))
51  
        data = sortedByMapElement(data, out);
52  
      else if (eqic("sort by input", fName))
53  
        data = sortedByMapElement(data, in);
54  
      else if (eqic("finish", fName))
55  
        makeCode();
56  
      else
57  
        data = map(data, func(Map map) {
58  
          litorderedmap(in, makeAndCall(fName, map.get(in)), out, map.get(out)) });
59  
          
60  
      history.add(cnew(HistoryEntry, functionApplied := fName, data := cloneList(data)));
61  
    } catch e {
62  
      messageBox(e);
63  
      ret;
64  
    }
65  
    table = showTable(table, data);
66  
    update();
67  
  }
68  
}
69  
70  
svoid update {
71  
  S dt = "Data type: " + join("/", new TreeSet(map className(collectMapElement(data, in))));
72  
  setText(lblHistory, dt + ". History: " + (l(history) <= 1 ? "-" :
73  
    join(" -> ", collect(dropFirst(history), 'functionApplied))));
74  
}
75  
76  
svoid makeCode {
77  
  new MultiMap mm;
78  
  for (Map map : data) mm.put(map.get(in), map.get(out));
79  
  L<S> functions = reversed(notStartingWith("sort by", collectNotNull(history, 'functionApplied)));
80  
  S mapCode = structureToJava(mapValues(f listToTopTen, multiMapToMap(mm)));
81  
  S code = "first(mapGet(" + mapCode + ", " + renderNestedFunctionCalls(functions, "input") + "))";
82  
  cset(uniq(Code), +code);
83  
  showText("Code", code);
84  
}

Author comment

Began life as a copy of #1010423

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: #1010430
Snippet name: Work on sentenceTypeExamples using standard functions [OK]
Eternal ID of this version: #1010430/38
Text MD5: b271cb9a37342096d7cbc7b4e30b39bb
Transpilation MD5: ee0630def17dccda5d8597f3877526d0
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-18 00:36:31
Source code size: 2683 bytes / 84 lines
Pitched / IR pitched: No / No
Views / Downloads: 408 / 916
Version history: 37 change(s)
Referenced in: [show references]