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

35
LINES

< > BotCompany Repo | #1001869 // Defining examples elegantly (OK)

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

Libraryless. Click here for Pure Java version (2533L/16K/56K).

!7

// an example
static class E {
  S in, out;
}

// Note the use of the clever group function to initialize a bunch of
// objects with minimal typing.
static L<E> examples = group(E, splitAtSpace("in out"), ll(
  "0", "00",
  "1", "11",
  "5", "55",
  "a", "aa",
  "b", "bb"));
  
p {
  printStruct(examples);
}

static <A> L<A> group(Class<A> theClass, L<S> fields, L data) {
  new L<A> l;
  try {
  for (int i = 0; i < l(data); i += l(fields)) {
    A o = newObject(theClass);
    for (int j = 0; j < min(l(fields), l(data)-i); j++) {
      S field = fields.get(j);
      O item = data.get(i+j);
      setOpt(o, field, item);
    }
    l.add(o);
  }
  } catch e { printStackTrace(e); }
  ret l;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001869
Snippet name: Defining examples elegantly (OK)
Eternal ID of this version: #1001869/6
Text MD5: fc83a99e4b024c0a41abbebb8139c7f5
Transpilation MD5: 481ecc89ec0cc1fe7fd1615d1d503d56
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-13 14:47:40
Source code size: 734 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 612 / 707
Version history: 5 change(s)
Referenced in: #1001873 - Compute one from the others (developing)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)