// Effect handlers in hot loops: a tail-resumptive `fn` operation, and a // `ctl` generator (stack switching). import std/io effect counter { fn tick :: n: Int -> Int } effect gen { ctl yield :: x: Int -> () } fn ticks :: i: Int acc: Int -> Int / { if i == 0 { acc } else { ticks (i - 1) (acc + tick i) } } fn count :: i: Int n: Int -> () / { if i < n { yield i count (i + 1) n } else { () } } fn total :: action: fn() -> () / -> Int / { handle { action () } with { yield x -> x + resume (), return _ -> 0 } } pub fn main :: -> () / { let fast = handle { ticks 2000000 0 } with { tick n -> n % 3 } let slow = total fn { count 0 100000 } io.println (show fast <> " " <> show slow) } //> 2000001 4999950000