aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorColin Okay <cbeok@protonmail.com>2020-07-08 13:18:01 -0500
committerColin Okay <cbeok@protonmail.com>2020-07-08 13:18:01 -0500
commit77472695f794e7e8b98fc6cfbe58dca1cbed377e (patch)
tree8a209cbe92f0342d6a9969897eda4b736ada4ccf
parent7e9d644f2133639b5deb9dfdb6196d78517a2d8a (diff)
renamed from-thunk zoo
-rw-r--r--gtwiwtg.lisp30
-rw-r--r--package.lisp1
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