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).

!7

sS in = 'Input, out = 'Output;

static L<Map> data;
static JTable table;
static JComboBox cbFunction;
static L<HistoryEntry> history;
//static Var<Int> historyIdx;
static JLabel lblHistory;

concept HistoryEntry {
  L<Map> data;
  S functionApplied;
}

concept Code {
  S code;
}

p-subst {
  newCase();
  db();
  history = persistentList("History");
  //historyIdx = persistentVar("History index", 0);
  cbFunction = autoComboBox(asTreeSet(listPlus(standardFunctionNames(), "undo", "sort by input", "sort by output", "finish")));
  onEnter(cbFunction, f apply);
  JComponent controls = centerAndEast(withLabel("Function:", cbFunction), jbutton("Apply", f apply));
  data = mapToListOfOrderedMap(sentenceTypeExamples(), in, out);
  history.add(cnew(HistoryEntry, data := cloneList(data)));
  table = showTable(data);
  addToWindow(table, withMargin(controls));
  addToWindow(table, withMargin(lblHistory = jlabel()));
  update();
  makeAndCall_warmUp("lower");
}

svoid apply { q.add(r { apply_doIt(getTextTrim(cbFunction)) }); }
static Q q = startQ();

svoid apply_doIt(fS fName) {
  loading {
    if (eqic("undo", fName)) {
      if (l(history) < 2) { infoBox("Nothing to undo"); ret; }
      S f = popLast(history).functionApplied;
      infoBox("Undoing " + f);
      HistoryEntry e = last(history);
      data = e.data;
    } else try {
      if (eqic("sort by output", fName))
        data = sortedByMapElement(data, out);
      else if (eqic("sort by input", fName))
        data = sortedByMapElement(data, in);
      else if (eqic("finish", fName))
        makeCode();
      else
        data = map(data, func(Map map) {
          litorderedmap(in, makeAndCall(fName, map.get(in)), out, map.get(out)) });
          
      history.add(cnew(HistoryEntry, functionApplied := fName, data := cloneList(data)));
    } catch e {
      messageBox(e);
      ret;
    }
    table = showTable(table, data);
    update();
  }
}

svoid update {
  S dt = "Data type: " + join("/", new TreeSet(map className(collectMapElement(data, in))));
  setText(lblHistory, dt + ". History: " + (l(history) <= 1 ? "-" :
    join(" -> ", collect(dropFirst(history), 'functionApplied))));
}

svoid makeCode {
  new MultiMap mm;
  for (Map map : data) mm.put(map.get(in), map.get(out));
  L<S> functions = reversed(notStartingWith("sort by", collectNotNull(history, 'functionApplied)));
  S mapCode = structureToJava(mapValues(f listToTopTen, multiMapToMap(mm)));
  S code = "first(mapGet(" + mapCode + ", " + renderNestedFunctionCalls(functions, "input") + "))";
  cset(uniq(Code), +code);
  showText("Code", code);
}

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: 518 / 1198
Version history: 37 change(s)
Referenced in: