Koka has nice ways to model control flow with ctl
effects:
pub fun main() var n := 20 var sum := 0 (handler { ctl yield(j) { println(j) sum := sum + j n := n - 1 if n > 0 then resume(()) } }){ fibonacci() } println("sum=" ++ sum.show)effect yield ctl yield(i: int): ()fun fibonacci() : <div,yield> () var i := 0 var j := 1 yield(j) while { True } val k = i + j i := j j := k yield(j)
(I'm sure this is not good Koka code but hopefully the question is still answerable)
What I want to achieve is to create an interator that can be polled to the next number. Something like:
fun next(iter: iterator): <console> maybe<int>
Essentially to invert the control flow and poll values, so I can do other logic without putting it in the effect handler.