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

65
LINES

< > BotCompany Repo | #1031753 // ForEach_vstack

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (8166L/46K).

1  
/*
2  
VStack-Computable Quick HowTo
3  
(more up-to-date version here: https://wiki.gaz.ai/doku.php?id=vstack_computation_how-to)
4  
5  
To make a function that can be run on a virtual (reified) stack, do this:
6  
7  
(This is the "with step" method where you automatically get an int step counter to use.)
8  
9  
-Subclass VStackComputableWithStep<A> (with A being your function's return type - or Void)
10  
11  
-Override void step(VStack stack)
12  
13  
-In step(), check the step field.
14  
 0 is step 1 of your function, 1 is step 2 etc.
15  
 After you have run the right code for the current step, update the
16  
 step field e.g. by step++.
17  
 
18  
-To return from the function, don't use ret normally, but write
19  
 "ret with stack.ret(myReturnValue);".
20  
 
21  
-To call a subroutine, write "ret with stack.push(new Subroutine(someArgs));".
22  
23  
 (Note that the subroutine is also an object implementing
24  
 VStack.Computable.) Before this line, make sure you update the step
25  
 field to the piece of code that is to be run when the subroutine is
26  
 complete.
27  
 
28  
-stack.call(...) is a synonym for stack.push(...).
29  
30  
-To get the result of a subroutine call, call result() aka subResult().
31  
 You will have to cast the object to the correct type, there was no way
32  
 to provide a generic signature in this instance.
33  
 
34  
-A special form of calling a subroutine is:
35  
   ret with stack.tailCall(new Subroutine(...));
36  
   
37  
 This calls the subroutine and passes its return value directly to the
38  
 current function's caller. Well, you know, tail calls. You don't have
39  
 to care about the step field in this case since a tail call means the
40  
 function is exiting anyway.
41  
 
42  
-Catching exceptions is currently not possible in VStack computations.
43  
44  
The simplest way to run your VStack-computable function is to call the
45  
global function vStackCompute on it. This one does have a proper return
46  
type thanks to the type parameter you provided right at the beginning…
47  
48  
*/
49  
50  
srecord noeq ForEach_vstack<A>(Iterable<A> l, IVF1<A> body) extends VStackComputableWithStep {
51  
  Iterator<A> it;
52  
  
53  
  void step(VStack stack) {
54  
    if (step == 0) {
55  
      if (l == null) ret with stack.ret();
56  
      it = iterator(l);
57  
      ++step;
58  
    }
59  
    
60  
    if (!it.hasNext())
61  
      ret with stack.ret();
62  
    
63  
    body.get(it.next());
64  
  }
65  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031753
Snippet name: ForEach_vstack
Eternal ID of this version: #1031753/10
Text MD5: b8d34d45ad450fdf94314605275b31ea
Transpilation MD5: 1c7852ffa6a80a132eabdb6a94c7e532
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-01 23:23:33
Source code size: 2279 bytes / 65 lines
Pitched / IR pitched: No / No
Views / Downloads: 255 / 505
Version history: 9 change(s)
Referenced in: [show references]