traversal_loop.bend

stelliferous@0.0.2.0 · 16 lines · 2 definitions

1# One sequential traversal for affine runtime owners and reusable proof states.
2# The step is a compile-time template: no closure allocation in the loop.
3import Base
4
5def run(~State: Type,~step: State -> State,count: Nat,state: State) -> State:
6  match count:
7    case 0n: state
8    case 1n+rest: run(~State,~step,rest,step(state))
9
10# The same loop handing its final state to finish in the base case. A caller
11# that stores its accumulators in finish keeps them out of the loop's return
12# value, so an outlined loop never passes accumulators back by value.
13def run_then(~State: Type,~Result: Type,~step: State -> State,~finish: State -> Result,count: Nat,state: State) -> Result:
14  match count:
15    case 0n: finish(state)
16    case 1n+rest: run_then(~State,~Result,~step,~finish,rest,step(state))