+(define (reduce-generator func init gen)
+ ;; Call FUNC for each item in the generator GEN.
+ ;;
+ ;; We maintain a STATE, which is initially INIT. For each ITEM produced by
+ ;; the generator, we replace the state by (FUNC ITEM STATE); finally, we
+ ;; return the final state.
+
+ (let loop ((state init))
+ (with-values (item any?) (resume gen)
+ (if any?
+ (loop (func item state))
+ state))))
+