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

!752

concepts.

concept Yo {
  new Ref a;
  
  *() {}
  *(Concept c) { a.set(c); }
}

concept YoYo {
  new Ref a;
  new Ref b;
  
  *() {}
  *(Concept a, Concept b) { this.a.set(a); this.b.set(b); }
}

p {
  Concept a = new Yo, b = cstr("yo");
  check(ll(a), a);
  
  check(ll(), new Options);
  check(ll(a), new Options(a));
  check(ll(a, b), new Options(a, b));
  
  check(ll(), new Yo(new Options()));
  check(ll(new Yo(a)), new Yo(new Options(a)));
  check(ll(new Yo(a)), new Yo(new Options(new Options(a))));
  
  check(ll(new YoYo(a, b)), new YoYo(a, b));
  check(ll(new YoYo(a, b), new YoYo(b, b)),
    new YoYo(new Options(a, b), b));
  check(ll(new YoYo(a, b), new YoYo(a, a)),
    new YoYo(a, new Options(b, a)));
    
  check(ll(new YoYo(a, b), new YoYo(a, a), new YoYo(b, b), new YoYo(b, a)),
    new YoYo(new Options(a, b), new Options(b, a)));
}

static L<Concept> unrollOptions(Concept c) {
  if (c << Options)
    ret concatMap("unrollOptions", c/Options);
  if (!conceptHasOptions(c))
    ret ll(c);
  else { // c has Options hidden somewhere within - recurse & "multiply"
    new Map<S, O> simples;
    Map<S, L<Concept>> multiples = new TreeMap;
  
    for (S field : conceptFields(c)) {
      O val = cget(c, field);
      if (!val << Concept)
        simples.put(field, val);
      else {
        L<Concept> l = unrollOptions(val/Concept);
        if (empty(l)) ret ll();
        else if (l(l) == 1)
          simples.put(field, first(l));
        else
          multiples.put(field, l);
      }
    }
    
    final Concept d = unlisted(c.getClass());
    csetMap(d, simples);
    
    if (empty(multiples))
      ret ll(d);
    else {
      if (l(multiples) > 2) warn("todo johnny");
      new L out;
      L<S> fields = asList(keys(multiples));
      
      S field0 = fields.get(0);
      for (Concept value0 : multiples.get(field0)) {
        Concept e = unlisted(c.getClass());
        copyConceptFields(d, e);
        cset(e, field0, value0);
        if (l(multiples) == 1) out.add(e); else {
          S field1 = fields.get(1);
          for (Concept value1 : multiples.get(field1)) {
            Concept f = unlisted(c.getClass());
            copyConceptFields(e, f);
            cset(f, field1, value1);
            out.add(f);
          }
        }
      }
      ret out;
    }
  }
}

svoid check(L l, Concept in) {
  L<Concept> l2 = unrollOptions(in);
  /*assertEquals(l(l), l(l2));
  for i over l:
    assertEquals(conceptStructure((Concept) l.get(i)), conceptStructure(l2.get(i)));
  print("ok");*/
  assertEqualsVerbose(conceptStructure(unlistedConceptL(l)), conceptStructure(unlistedConceptL(l2)));
}

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: 463 / 515
Referenced in: [show references]