Libraryless. Click here for Pure Java version (901L/6K/22K).
1 | !752 |
2 | |
3 | // A vessel is a list of strings (sentences). |
4 | |
5 | sclass Vessel {
|
6 | new L<S> data; |
7 | int index; |
8 | |
9 | *() {}
|
10 | *(L<S> *data, int *index) {}
|
11 | |
12 | void add(S s) {
|
13 | data.add(simplify(s)); |
14 | } |
15 | |
16 | bool consume(S s) {
|
17 | if (end()) ret false; |
18 | |
19 | s = simplify(s); |
20 | |
21 | /*if (same(next(), s)) {
|
22 | ++index; |
23 | ret true; |
24 | } |
25 | ret false;*/ |
26 | |
27 | //++index; |
28 | |
29 | if (!same(next(), s)) {
|
30 | --index; |
31 | ret false; |
32 | } |
33 | ret true; |
34 | } |
35 | |
36 | void restart() {
|
37 | index = 0; |
38 | } |
39 | |
40 | // may be null if index after end |
41 | S next() {
|
42 | ret end() ? null : get(data, index++); |
43 | } |
44 | |
45 | bool end() {
|
46 | ret index >= l(data); |
47 | } |
48 | |
49 | public Vessel clone() {
|
50 | ret new Vessel(cloneList(data), index); |
51 | } |
52 | } |
53 | |
54 | static S simplify(S s) {
|
55 | ret s.trim().toUpperCase(); |
56 | } |
57 | |
58 | static bool same(S a, S b) {
|
59 | ret eq(a, b); |
60 | } |
61 | |
62 | p {
|
63 | new Vessel v; |
64 | v.add("hello");
|
65 | v.add("world");
|
66 | print("HELLO");
|
67 | assertTrue(v.consume("HELLO"));
|
68 | assertEquals(1, v.index); |
69 | print("world? " + v.next());
|
70 | print("end? " + v.end());
|
71 | } |
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: | 784 / 880 |
| Referenced in: | [show references] |