1 ! Copyright (C) 2023 Keldan Chapman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: help.markup help.syntax kernel math quotations sequences generators coroutines effects ;
11 { $description "Creates a generator object from an input quotation." } ;
16 { "val/f" { $maybe object } } { "end?" boolean }
18 { $description "A safe version of " { $link next } ". Also returns a boolean indicating whether the end of the generator was reached." } ;
22 { "v" object } { "gen" generator }
23 { "val/f" { $maybe object } } { "end?" boolean }
25 { $description "A safe version of " { $link next } ". Also returns a boolean indicating whether the end of the generator was reached." } ;
28 { $syntax "GEN: word ( stack -- generator ) definition... ;" }
29 { $description "Creates a generator word in the current vocabulary. When executed, the word will capture its inputs from the stack and a generator object will be returned. The output of generator words must always be only a single value (the generator)." } ;
32 { $syntax "GEN:: word ( stack -- generator ) definition... ;" }
33 { $description "Creates a generator word in the current vocabulary. When executed, the word's inputs will be captured from the stack and bound to them to lexical input variables from left to right. A generator object will be returned. The output of generator words must always be only a single value (the generator)." } ;
36 HELP: assert-no-inputs
40 { $description "Throws a " { $link has-inputs } " error if the input quotation accepts inputs. Otherwise does nothing." } ;
42 HELP: catch-stop-generator
44 { "try" quotation } { "except" quotation }
46 { $description "Attempts to run the " { $snippet try } " quotation. If a " { $link stop-generator } " error is thrown, then the " { $snippet except } "quotation will be run instead." } ;
50 { "gen" generator } { "?" boolean }
52 { $description "Check whether a generator has already been exhausted." } ;
56 { "quot" quotation } { "gen" generator }
59 { $description "Builds a coroutine from a quotation for use in a generator object. On termination of the quotation, the generator object will be marked as complete." } ;
62 { $class-description "A simplified coroutine which produces values as needed. " { $link stop-generator } " is thrown when no more values can be produced." } ;
65 { $description "Throws a " { $link has-inputs } " error." }
66 { $error-description "Indicates that a quotation incorrectly expects inputs." } ;
70 { "quot" quotation } { "effect" effect }
72 { $description "Prepares a quotation for use as a generator word." } ;
79 { $description "Resume computation in the generator until the next value is produced." } ;
83 { "v" object } { "gen" generator }
86 { $description "Pass a value into the generator, resuming computation until a value is produced." } ;
92 { $description "Resume computation until a value is produced. The value is discarded." } ;
96 { "v" object } { "gen" object }
98 { $description "Pass a value into the generator, resuming computation until a value is produced. The value is discarded." } ;
101 { $description "Throws a " { $link stop-generator } " error." }
102 { $error-description "Indicates that the generator has run out of values to produce" } ;
106 { "gen" generator } { "n" integer }
109 { $description "Takes the next " { $snippet n } " values from the generator, collecting them into a sequence." } ;
116 { $description "Runs the generator until completion, collecting all yielded values into a sequence." } ;
122 { $description "Pause computation and yield a value to the caller." } ;
129 { $description "Pause computation and yield a value to the caller, with the expectation for a value to be returned before computation is resumed." } ;
135 { $description "Delegate computation to the specified generator until it is exhausted, before resuming computation in the current generator." } ;
137 ARTICLE: "generators" "Generators"
138 { $vocab-link "generators" }