1
$\begingroup$

This question is about generators, which are functions that return an item from a (usually very large or infinite) set on demand, an example from Python is:

def all_natural_evens():
    n = 0
    
    while True:
        yield n
        n += 2

As can be seen, the above function yields the even number n in an infinite loop, in other words, it yields all positive even numbers, but since they are infinite and computer memory is finite, they generate the numbers one by one, on demand (using next(gen) to get the next number of the generator gen, where gen = all_natural_evens()).

I am using generators to build an algorithm and when writing it in Latex, I didn't know how to express this in the algorithm, for ordinary return values there is return, but with generators in Python there is yield and yield from (yield from works like an intermediary generator, it generates from a generator...clearly). Is there any notation anywhere for dealing with this kind of generators?

At the moment I'm using keywords $\text{yield}$ and $\text{yield from}$ in the algorithm's pseudocode and explaining what they do.

$\endgroup$
1
$\begingroup$

There is no standard way of writing pseudocode. Your main goal when writing pseudocode is that the reader understands it. If you are unsure that the reader will understand yield or yield from, explain what these keywords mean for you, and mention that they are borrowed from Python, to give due credit.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.