|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (srfi srfi-171 gnu) |
|
#:use-module (srfi srfi-171) |
|
#:use-module (srfi srfi-171 meta) |
|
#:export (tbatch tfold)) |
|
|
|
|
|
(define tbatch |
|
(case-lambda |
|
((reducer) |
|
(tbatch identity reducer)) |
|
((t r) |
|
(lambda (reducer) |
|
(let ((cur-reducer (t r)) |
|
(cur-state (r))) |
|
(case-lambda |
|
(() (reducer)) |
|
((result) |
|
(if (equal? cur-state (cur-reducer)) |
|
(reducer result) |
|
(let ((new-res (reducer result (cur-reducer cur-state)))) |
|
(if (reduced? new-res) |
|
(reducer (unreduce new-res)) |
|
(reducer new-res))))) |
|
((result value) |
|
(let ((val (cur-reducer cur-state value))) |
|
(cond |
|
|
|
|
|
((reduced? val) |
|
(let ((unreduced-val (unreduce val))) |
|
(set! cur-reducer (t r)) |
|
(set! cur-state (cur-reducer)) |
|
(reducer result (cur-reducer unreduced-val)))) |
|
(else |
|
(set! cur-state val) |
|
result)))))))))) |
|
|
|
|
|
(define* (tfold reducer #:optional (seed (reducer))) |
|
(lambda (r) |
|
(let ((state seed)) |
|
(case-lambda |
|
(() (r)) |
|
((result) (r result)) |
|
((result value) |
|
(set! state (reducer state value)) |
|
(if (reduced? state) |
|
(reduced (reducer (unreduce state))) |
|
(r result state))))))) |
|
|