Skip to content

Instantly share code, notes, and snippets.


Jonas Moss JonasMoss

View GitHub Profile
klmr /
Last active Jul 18, 2021
Python-like generators in R

A little experiment using restarts.

(And while we’re at it, let’s torture R’s syntax a little.)


In the following we will be using R’s “restarts” feature to implement the state machine that drives generators in languages such as Python. Generators allow lazily generating values on demand: a consumer invokes a generator, and consumes values as they are produced. A new value is only produced once the previous one has been consumed.