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

71
LINES

< > BotCompany Repo | #1003040 // Vessel

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

Libraryless. Click here for Pure Java version (901L/6K/22K).

!752

// A vessel is a list of strings (sentences).

sclass Vessel {
  new L<S> data;
  int index;

  *() {}
  *(L<S> *data, int *index) {}

  void add(S s) {
    data.add(simplify(s));
  }

  bool consume(S s) {
    if (end()) ret false;
    
    s = simplify(s);
    
    /*if (same(next(), s)) {
      ++index;
      ret true;
    }
    ret false;*/
    
    //++index;
    
    if (!same(next(), s)) {
      --index;
      ret false;
    }
    ret true;
  }

  void restart() {
    index = 0;
  }

  // may be null if index after end
  S next() {
    ret end() ? null : get(data, index++);
  }
  
  bool end() {
    ret index >= l(data);
  }

  public Vessel clone() {
    ret new Vessel(cloneList(data), index);
  }
}

static S simplify(S s) {
  ret s.trim().toUpperCase();
}

static bool same(S a, S b) {
  ret eq(a, b);
}

p {
  new Vessel v;
  v.add("hello");
  v.add("world");
  print("HELLO");
  assertTrue(v.consume("HELLO"));
  assertEquals(1, v.index);
  print("world? " + v.next());
  print("end? " + v.end());
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003040
Snippet name: Vessel
Eternal ID of this version: #1003040/1
Text MD5: 4b94832162aee331677196b9fa9b429b
Transpilation MD5: 31ba35fcb66a952a3b214efc00814ffb
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-24 03:03:23
Source code size: 1098 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 580 / 630
Referenced in: [show references]