let unfoldi init ~f = let a = ref init in from (fun i -> match f i !a with | Some (b, a_next) -> (a := a_next; Some b) | None -> None )