File: generator.el.html

This package implements generators for Emacs Lisp through a continuation-passing transformation. It provides essentially the same generator API and iterator facilities that Python and JavaScript ES6 provide.

iter-lambda and iter-defun work like lambda and defun, except that they evaluate to or define, respectively, generator functions. These functions, when called, return an iterator. An iterator is an opaque object that generates a sequence of values. Callers use iter-next to retrieve the next value from the sequence; when the sequence is exhausted, iter-next will raise the iter-end-of-sequence condition.

Generator functions are written like normal functions, except that they can invoke iter-yield to suspend themselves and return a value to callers; this value becomes the return value of iter-next. On the next call to iter-next, execution of the generator function resumes where it left off. When a generator function returns normally, the iter-next raises iter-end-of-sequence with the value the function returned.

iter-yield-from yields all the values from another iterator; it then evaluates to the value the sub-iterator returned normally. This facility is useful for functional composition of generators and for implementing coroutines.

iter-yield is illegal inside the UNWINDFORMS of an unwind-protect for various sordid internal reasons documented in the code.

N.B. Each call to a generator function generates a *new* iterator, and each iterator maintains its own internal state.

This raw form of iteration is general, but a bit awkward to use, so this library also provides some convenience functions:

iter-do is like dolist, except that instead of walking a list, it walks an iterator. cl-loop is also extended with a new keyword, iter-by, that iterates over an iterator.

Defined variables (4)

cps--dynamic-wrappersList of functions to apply to atomic forms.
cps-inhibit-atomic-optimizationWhen non-nil, always rewrite forms into CPS even when they don’t yield.
cps-standard-special-formsList of special forms treated just like ordinary function applications.
iter-emptyTrivial iterator that always signals the end of sequence.

Defined functions (31)

cps--add-binding(ORIGINAL-NAME)
cps--add-state(KIND BODY)
cps--advance-for(CONSCELL)
cps--atomic-p(FORM)
cps--define-unsupported(FUNCTION)
cps--find-special-form-handler(FORM)
cps--gensym(FMT &rest ARGS)
cps--handle-loop-for(VAR)
cps--initialize-for(ITERATOR)
cps--make-atomic-state(FORM NEXT-STATE)
cps--make-catch-wrapper(TAG-BINDING NEXT-STATE)
cps--make-close-iterator-form(TERMINAL-STATE)
cps--make-condition-wrapper(VAR NEXT-STATE HANDLERS)
cps--make-dynamic-binding-wrapper(DYNAMIC-VAR STATIC-VAR)
cps--make-unwind-wrapper(UNWIND-FORMS)
cps--replace-variable-references(VAR NEW-VAR FORM)
cps--special-form-p(DEFINITION)
cps--trace(FMT &rest ARGS)
cps--trace-funcall(FUNC &rest ARGS)
cps--transform-1(FORM NEXT-STATE)
cps--with-dynamic-binding(DYNAMIC-VAR STATIC-VAR &rest BODY)
cps--with-value-wrapper(WRAPPER &rest BODY)
cps-generate-evaluator(BODY)
iter-close(ITERATOR)
iter-defun(NAME ARGLIST &rest BODY)
iter-do((VAR ITERATOR) &rest BODY)
iter-lambda(ARGLIST &rest BODY)
iter-make(&rest BODY)
iter-next(ITERATOR &optional YIELD-RESULT)
iter-yield(VALUE)
iter-yield-from(VALUE)

Defined faces (0)