From d8c3f29f788f6b508818d78d72975cb251ec9ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 10 Aug 2020 13:28:13 +0200 Subject: Got new timeslice limiter to work, document. --- module/srfi/srfi-41/util.scm | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'module/srfi/srfi-41/util.scm') diff --git a/module/srfi/srfi-41/util.scm b/module/srfi/srfi-41/util.scm index a6c6dc3d..51671985 100644 --- a/module/srfi/srfi-41/util.scm +++ b/module/srfi/srfi-41/util.scm @@ -1,8 +1,10 @@ (define-module (srfi srfi-41 util) #:use-module (srfi srfi-1) #:use-module (srfi srfi-41) + #:use-module ((ice-9 sandbox) :select (call-with-time-limit)) #:use-module (util) ; let*, find-min - #:export (stream-car+cdr interleave-streams with-streams)) + #:export (stream-car+cdr interleave-streams with-streams + stream-timeslice-limit)) (define (stream-car+cdr stream) (values (stream-car stream) @@ -103,20 +105,21 @@ (stream-paginate% stream page-size)) +;; stream cons, but eval arguments beforehand. (define (eager-stream-cons a b) (stream-cons a b)) -(use-modules (ice-9 sandbox) ) -(define (stream-timeslice-limit strm) +;; Wrap a stream in time limits. Each element has at most @var{timeslice} +;; seconds to produce a value, otherwise the stream ends. Useful for finding the +;; "final" element matching a predicate in an infinite stream. +(define-stream (stream-timeslice-limit strm timeslice) (call-with-time-limit - 0.1 + timeslice (lambda () (eager-stream-cons (stream-car strm) - (stream-timeslice-limit (stream-cdr strm)))) + (stream-timeslice-limit (stream-cdr strm) timeslice))) (lambda _ stream-null))) -(export stream-timeslice-limit) - ;; Evaluates @var{body} in a context where most list fundamentals are ;; replaced by stream alternatives. ;; commented defifinitions are items which could be included, but for -- cgit v1.2.3