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

102
LINES

< > BotCompany Repo | #1005488 // Develop "unrollOptions"

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

Libraryless. Click here for Pure Java version (5642L/38K/121K).

1  
!752
2  
3  
concepts.
4  
5  
concept Yo {
6  
  new Ref a;
7  
  
8  
  *() {}
9  
  *(Concept c) { a.set(c); }
10  
}
11  
12  
concept YoYo {
13  
  new Ref a;
14  
  new Ref b;
15  
  
16  
  *() {}
17  
  *(Concept a, Concept b) { this.a.set(a); this.b.set(b); }
18  
}
19  
20  
p {
21  
  Concept a = new Yo, b = cstr("yo");
22  
  check(ll(a), a);
23  
  
24  
  check(ll(), new Options);
25  
  check(ll(a), new Options(a));
26  
  check(ll(a, b), new Options(a, b));
27  
  
28  
  check(ll(), new Yo(new Options()));
29  
  check(ll(new Yo(a)), new Yo(new Options(a)));
30  
  check(ll(new Yo(a)), new Yo(new Options(new Options(a))));
31  
  
32  
  check(ll(new YoYo(a, b)), new YoYo(a, b));
33  
  check(ll(new YoYo(a, b), new YoYo(b, b)),
34  
    new YoYo(new Options(a, b), b));
35  
  check(ll(new YoYo(a, b), new YoYo(a, a)),
36  
    new YoYo(a, new Options(b, a)));
37  
    
38  
  check(ll(new YoYo(a, b), new YoYo(a, a), new YoYo(b, b), new YoYo(b, a)),
39  
    new YoYo(new Options(a, b), new Options(b, a)));
40  
}
41  
42  
static L<Concept> unrollOptions(Concept c) {
43  
  if (c << Options)
44  
    ret concatMap("unrollOptions", c/Options);
45  
  if (!conceptHasOptions(c))
46  
    ret ll(c);
47  
  else { // c has Options hidden somewhere within - recurse & "multiply"
48  
    new Map<S, O> simples;
49  
    Map<S, L<Concept>> multiples = new TreeMap;
50  
  
51  
    for (S field : conceptFields(c)) {
52  
      O val = cget(c, field);
53  
      if (!val << Concept)
54  
        simples.put(field, val);
55  
      else {
56  
        L<Concept> l = unrollOptions(val/Concept);
57  
        if (empty(l)) ret ll();
58  
        else if (l(l) == 1)
59  
          simples.put(field, first(l));
60  
        else
61  
          multiples.put(field, l);
62  
      }
63  
    }
64  
    
65  
    final Concept d = unlisted(c.getClass());
66  
    csetMap(d, simples);
67  
    
68  
    if (empty(multiples))
69  
      ret ll(d);
70  
    else {
71  
      if (l(multiples) > 2) warn("todo johnny");
72  
      new L out;
73  
      L<S> fields = asList(keys(multiples));
74  
      
75  
      S field0 = fields.get(0);
76  
      for (Concept value0 : multiples.get(field0)) {
77  
        Concept e = unlisted(c.getClass());
78  
        copyConceptFields(d, e);
79  
        cset(e, field0, value0);
80  
        if (l(multiples) == 1) out.add(e); else {
81  
          S field1 = fields.get(1);
82  
          for (Concept value1 : multiples.get(field1)) {
83  
            Concept f = unlisted(c.getClass());
84  
            copyConceptFields(e, f);
85  
            cset(f, field1, value1);
86  
            out.add(f);
87  
          }
88  
        }
89  
      }
90  
      ret out;
91  
    }
92  
  }
93  
}
94  
95  
svoid check(L l, Concept in) {
96  
  L<Concept> l2 = unrollOptions(in);
97  
  /*assertEquals(l(l), l(l2));
98  
  for i over l:
99  
    assertEquals(conceptStructure((Concept) l.get(i)), conceptStructure(l2.get(i)));
100  
  print("ok");*/
101  
  assertEqualsVerbose(conceptStructure(unlistedConceptL(l)), conceptStructure(unlistedConceptL(l2)));
102  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005488
Snippet name: Develop "unrollOptions"
Eternal ID of this version: #1005488/1
Text MD5: dde6ce0fb8b02a11ee85497df0045341
Transpilation MD5: ec2d3423d090a64d06601ce8116682d9
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-23 14:50:33
Source code size: 2740 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 469 / 522
Referenced in: [show references]