diff options
-rw-r--r-- | gtwiwtg-test.lisp | 9 | ||||
-rw-r--r-- | gtwiwtg.asd | 2 | ||||
-rw-r--r-- | gtwiwtg.lisp | 34 | ||||
-rw-r--r-- | package.lisp | 2 |
4 files changed, 46 insertions, 1 deletions
diff --git a/gtwiwtg-test.lisp b/gtwiwtg-test.lisp index be79e23..80c3c86 100644 --- a/gtwiwtg-test.lisp +++ b/gtwiwtg-test.lisp @@ -106,6 +106,15 @@ '(#\r #\e #\e #\r #\n)) + (let ((res (make-resumable! + (concat! (seq "hello") + (indexed! (filter! 'alpha-char-p (seq "a b c 1 2 3 easy as"))))))) + (take 1 res) + (setf res (resume! res)) + (is '(#\e #\l #\l #\o (0 #\a) (1 #\b) (2 #\c) (3 #\e) (4 #\a) (5 #\s) (6 #\y) + (7 #\a) (8 #\s)) + (collect res))) + ) diff --git a/gtwiwtg.asd b/gtwiwtg.asd index 462d3ac..41f87eb 100644 --- a/gtwiwtg.asd +++ b/gtwiwtg.asd @@ -4,7 +4,7 @@ :description "Lazy-ish iterators" :author "Colin Okay <okay@toyful.space>" :license "GPLv3" - :version "0.1.2" + :version "0.2.0" :serial t :components ((:file "package") (:file "gtwiwtg") diff --git a/gtwiwtg.lisp b/gtwiwtg.lisp index 5394224..42e7731 100644 --- a/gtwiwtg.lisp +++ b/gtwiwtg.lisp @@ -148,6 +148,15 @@ (stop (slot-value gen 'source-generator))) +(a-generator-class resumable-generator! () + (wrapped (error "Resumable generators must wrap another generator"))) + +(defmethod next ((gen resumable-generator!)) + (next (slot-value gen 'wrapped))) + +(defmethod has-next-p ((gen resumable-generator!)) + (gtwiwtg::has-next-p (slot-value gen 'wrapped))) + ;;; CONSTRUCTORS (defun range (&key (from 0) to (by 1) inclusive) @@ -360,6 +369,31 @@ The last generated value of the returned generator will be NIL. ;;; MODIFIERS and COMBINATORS +(defun make-resumable! (gen) + "Makes a generator resumable. + +> (defvar *foobar* (make-resumable! (range))) +*FOOBAR* + +> (take 10 *foobar*) +(0 1 2 3 4 5 6 7 8 9) + +> (setf *foobar* (resume! *foobar*)) + +> (take 10 *foobar*) +(10 11 12 13 14 15 16 17 18 19) +" + (sully-when-clean (list gen)) + (make-instance 'resumable-generator! :wrapped gen)) + +(defun can-be-resumed-p (gen) + (and (typep gen 'resumable-generator!) + (stopped-p gen))) + +(defun resume! (resumable) + (assert (can-be-resumed-p resumable)) + (make-instance 'resumable-generator! :wrapped (slot-value resumable 'wrapped))) + (defun map! (map-fn gen &rest gens) "Maps a function over a number of generators, returning a generator that produces values that result from calling MAP-FN on those diff --git a/package.lisp b/package.lisp index 848146d..b6efe86 100644 --- a/package.lisp +++ b/package.lisp @@ -15,6 +15,8 @@ #:file-lines #:file-chars #:file-bytes + #:make-resumable! + #:resume! #:map! #:filter! #:inflate! |