diff options
author | Colin Okay <cbeok@protonmail.com> | 2020-07-10 13:03:35 -0500 |
---|---|---|
committer | Colin Okay <cbeok@protonmail.com> | 2020-07-10 13:03:35 -0500 |
commit | 2a2517531c53cae6ce428cd8027af52bf5a5c807 (patch) | |
tree | 35192f5cfb4bfb25e54456f440120683a5d512f4 | |
parent | d4908b6a0eafa20c6dd6bb40f3e115b7ef2154bc (diff) |
short section on extending gtwiwtg with new generators
-rw-r--r-- | README.md | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -505,6 +505,46 @@ The `pick-out` consumer is interesting enough to see a quick example of: (#\r #\e #\e #\r #\n) ``` +### Making New Generators + +Generators are subclasses of `gtwiwtg::generator!` that have at least +two methods specialized on them: + +- `(gtwiwtg::next gen)` : advances the generator and its next value +- `(gtwiwtg::nas-next-p gen)` : checks whether or not the generator has a next value + +Additionally, if your generator needs to perform cleanup after it is +consumed, you can implement the `:after` method combination for the method + +- `(gtwiwtg::stop gen)` : is called by consumers to mark the generator + as stopped. + +None of the above are meant to be called by users of the library, +which is why they are not exported symbols. But if you want to make +your own generators you can. + +A silly example: + +``` lisp + +(defclass countdown (gtwiwtg::generator!) + ((value :reader countdown-value + :initarg :value + :initform 0))) + +(defmethod gtwiwtg::next ((g cowntdown)) + (decf (countdown-value g))) + +(defmethod gtwiwtg::has-next-p ((g countdown)) + (plusp (countdown-value g))) + +``` + +You can see that `next` ASSUMES that there is a next value. This is +one of the reasons you are not ment to call `next` manually. The +`for` consumer automatcially checks that there is a next value before +trying to get it. + ## The Permutations Example One final example to show you what you can do. Here is a function that |