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

74
LINES

< > BotCompany Repo | #1035386 // BStack experimentation

JavaX (incomplete)

1  
// Goal: Java code with backtracking, like this, to return 4 different values:
2  
3  
// Color color = Color.black or Color.white;
4  
// S shape = "circle" or "square";
5  
// return "A " + color + " " + shape;
6  
7  
// 1. Simple version without backtracking
8  
9  
embedded S functionSimple() {
10  
  S color = "black";
11  
  S shape = "circle";
12  
  return "A " + color + " " + shape;
13  
}
14  
//ret functionSimple();
15  
16  
// 2. Convert to virtual stack (VStack)
17  
18  
/*class FunctionSimple extends VStackComputableWithStep<String> {
19  
  String color, shape;
20  
21  
  void step(VStack stack) {
22  
    if (step == 0) {
23  
      color = "black";
24  
      step = 1;
25  
    } else if (step == 1) {
26  
      shape = "circle";
27  
      step = 2;
28  
    } else
29  
      stack.return("A " + color + " " + shape);
30  
  }
31  
}
32  
return vstackCompute(new FunctionSimple);*/
33  
34  
// 3. Add backtracking with BStack. (BacktrackableStack, BacktrackingStack, VStackWithBacktracking?)
35  
36  
global { sclass FunctionSimple extends VStackComputableWithStep<String> {
37  
  String color, shape;
38  
39  
  void step(VStack stack) {
40  
    cast stack to BStack;
41  
42  
    if (step == 0) {
43  
      step = 1;
44  
      stack.options(this,
45  
        f -> f.color = "black",
46  
        f -> f.color = "white");
47  
    } else if (step == 1) {
48  
      step = 2;
49  
      stack.options(this,
50  
        f -> f.shape = "circle",
51  
        f -> f.shape = "square");
52  
    } else
53  
      stack.return("A " + color + " " + shape);
54  
  }
55  
}}
56  
57  
// Make a stack with our computation
58  
BStack stack = new BStack(new FunctionSimple);
59  
60  
repeat 100 { // 100 steps max for safety
61  
  // Get first or next result of function
62  
  print("Result: " + stack.nextResultWithPrintStruct(10));
63  
64  
  // Ask the stack to backtrack to get another result
65  
  stack = stack.backtrack();
66  
67  
  // If there is no more result, print message and exit
68  
  if (stack == null) {
69  
    print("No further result");
70  
    return;
71  
  }
72  
73  
  // There is another result, so continue loop
74  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 1 computer(s): mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035386
Snippet name: BStack experimentation
Eternal ID of this version: #1035386/1
Text MD5: c69334631678260a4714c4777af3375d
Author: stefan
Category: javax
Type: JavaX (incomplete)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-05 15:20:35
Source code size: 1926 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 65 / 72
Referenced in: [show references]