Libraryless. Click here for Pure Java version (25414L/155K).
1 | scope test_PStack |
2 | |
3 | /* Goal: Allow probabilistic branching deep within nested function calls |
4 | |
5 | Like this, to return 4 different values: |
6 | |
7 | S color = "black" with probability 0.75 |
8 | or "white" with probability 0.25; |
9 | S shape = "circle" with probability 0.4 |
10 | or "square" with probability 0.6; |
11 | return "A " + color + " " + shape; |
12 | */ |
13 | |
14 | sclass #C1 extends PStackComputableWithStep<S> { |
15 | S color; |
16 | |
17 | public void step(IPStack stack) { |
18 | if (step == 0) { |
19 | ++step; |
20 | stack.options(this, ll( |
21 | o -> o.color = "red", |
22 | o -> o.color = "green")); |
23 | } else |
24 | stack.ret(color); |
25 | } |
26 | } |
27 | |
28 | please include function test_BStack. |
29 | |
30 | svoid test_PStack() { |
31 | test_PStack_oneBranch(); |
32 | test_PStack_twoBranches(); |
33 | test_PStack_oneBranch3Options(); |
34 | test_PStack_twoBranches3Options(); |
35 | test_PStack_noOptions(); |
36 | } |
37 | |
38 | svoid test_PStack_oneBranch() { |
39 | assertEqualsVerbose( |
40 | ll("Color is black", "Color is white"), |
41 | pstackComputeAll(new test_BStack_OneBranch)); |
42 | } |
43 | |
44 | svoid test_PStack_oneBranch3Options() { |
45 | assertEqualsVerbose( |
46 | ll("Color is black", "Color is white", "Color is gray"), |
47 | pstackComputeAll(new test_BStack_OneBranch3Options)); |
48 | } |
49 | |
50 | svoid test_PStack_twoBranches() { |
51 | assertEqualsVerbose( |
52 | ll("A black circle", "A black square", |
53 | "A white circle", "A white square"), |
54 | pstackComputeAll(new test_BStack_TwoBranches)); |
55 | } |
56 | |
57 | svoid test_PStack_twoBranches3Options() { |
58 | assertEqualsVerbose( |
59 | ll("A black circle", "A black square", "A black triangle", |
60 | "A white circle", "A white square", "A white triangle", |
61 | "A gray circle", "A gray square", "A gray triangle"), |
62 | pstackComputeAll(new test_BStack_TwoBranches3Options)); |
63 | } |
64 | |
65 | svoid test_PStack_noOptions() { |
66 | assertInnerExceptionOfTypeVerbose(PStack.NoOptionsException.class, |
67 | -> pstackComputeAll(new test_BStack_NoOptions)); |
68 | } |
Began life as a copy of #1035397
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035428 |
Snippet name: | test_PStack (OK) |
Eternal ID of this version: | #1035428/5 |
Text MD5: | 14bdfcb42edf4a7280f05e81f3a9c61f |
Transpilation MD5: | daf30db31239a6388630dc4776c01b7e |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-05-08 21:06:39 |
Source code size: | 1894 bytes / 68 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 143 / 229 |
Version history: | 4 change(s) |
Referenced in: | [show references] |