From 83dcb72f6fa2527f48a1e64f15e1866bf674e7e4 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Thu, 30 Jul 2020 09:08:49 -0500 Subject: added to readme, documenting with-generator --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index da27d0b..c5616b1 100644 --- a/README.md +++ b/README.md @@ -574,6 +574,7 @@ the `gtwiwtg.anaphora` package. Here's an example: ``` + ### Making New Generators Generators are subclasses of `gtwiwtg::generator!` that have at least @@ -626,6 +627,30 @@ one of the reasons you are not ment to call `next` manually. The `for` consumer automatically checks that there is a next value before trying to get it. + +### The Naughty Consumer + +Now that the mysteries that make generators go has been explained in +the previous section, you may be tempted to manually call `next` and +`has-next-p` on your generators. If you must do this, you should use +the `with-generator` macro: + +```lisp + +> (with-generator (gen (seq "a1b2c3")) + (when (gtwiwtg::has-next-p gen) + (princ (gtwiwtg::next gen)) + (terpri))) +a + +``` + +The `with-generator` form will ensure that the generator is properly +closed. Could be useful with generators backed by input streams that +need a custom logic that is hard to build using the basic tools. I'm +not sure if you ever *will* need it, but the library provides it just +in case. + ## The Permutations Example One final example to show you what you can do. Here is a function that -- cgit v1.2.3