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

1  
!7
2  
3  
// an example
4  
static class E {
5  
  S in, out;
6  
}
7  
8  
// Note the use of the clever group function to initialize a bunch of
9  
// objects with minimal typing.
10  
static L<E> examples = group(E, splitAtSpace("in out"), ll(
11  
  "0", "00",
12  
  "1", "11",
13  
  "5", "55",
14  
  "a", "aa",
15  
  "b", "bb"));
16  
  
17  
p {
18  
  printStruct(examples);
19  
}
20  
21  
static <A> L<A> group(Class<A> theClass, L<S> fields, L data) {
22  
  new L<A> l;
23  
  try {
24  
  for (int i = 0; i < l(data); i += l(fields)) {
25  
    A o = newObject(theClass);
26  
    for (int j = 0; j < min(l(fields), l(data)-i); j++) {
27  
      S field = fields.get(j);
28  
      O item = data.get(i+j);
29  
      setOpt(o, field, item);
30  
    }
31  
    l.add(o);
32  
  }
33  
  } catch e { printStackTrace(e); }
34  
  ret l;
35  
}

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: 544 / 622
Version history: 5 change(s)
Referenced in: [show references]