From 77472695f794e7e8b98fc6cfbe58dca1cbed377e Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Wed, 8 Jul 2020 13:18:01 -0500 Subject: renamed from-thunk zoo --- gtwiwtg.lisp | 30 +++++++++++++++++++++++------- package.lisp | 1 + 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/gtwiwtg.lisp b/gtwiwtg.lisp index 202063a..c273680 100644 --- a/gtwiwtg.lisp +++ b/gtwiwtg.lisp @@ -96,15 +96,15 @@ the values passed as ARGS looped forever." (values (random arg) nil)))) -(defun from-thunk (thunk &optional times) - "Creates a generator that produces an inifinte series of values that -are the return value of (FUNCALL THUNK) -If TIMES is supplied, THUNK will only be called TIMES times." - (let ((thunk-proxy (lambda (ignore) (declare (ignore ignore)) (funcall thunk)))) - (map! thunk-proxy (range :to (when times (1- times)))))) -(defun from-thunk-until (thunk until) +(defun from-thunk-until (thunk &optional (until (constantly nil))) + "Creates a generator that produces a series of value by successively +calling (FUNCALL THUNK). The iterator stops whenever (FUNCALL UNTIL) +is non null. + +By default, UNTIL is the function (CONSTANTLY NIL). I.e. it will +generate forever." (make-instance 'generator! :state nil :next-p-fn (lambda (ignore) (declare (ignore ignore)) (not (funcall until))) @@ -112,6 +112,22 @@ If TIMES is supplied, THUNK will only be called TIMES times." (declare (ignore ignore)) (values (funcall thunk) nil)))) + +(defun from-thunk (thunk) + "Creates a generator that produces an inifinte series of values that +are the return value of (FUNCALL THUNK). + +If you need to create a stopping condition on your thunk-backed +generator, see FROM-THUNK-UNTIL." + (from-thunk-until thunk)) + + +(defun from-thunk-times (thunk times) + "Creates a generator that produces its values by calling + (FUNCALL THUNK) exactly TIMES times." + (let ((thunk-proxy (lambda (ignore) (declare (ignore ignore)) (funcall thunk)))) + (map! thunk-proxy (times times)))) + (defun from-recurrence (rec n-1 &rest n-m) "Creates a generator from a recurrence relation. diff --git a/package.lisp b/package.lisp index ec55c79..964b785 100644 --- a/package.lisp +++ b/package.lisp @@ -9,6 +9,7 @@ #:noise #:from-thunk #:from-thunk-until + #:from-thunk-times #:from-recurrence #:from-input-stream #:file-lines -- cgit v1.2.3