Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonas Moss JonasMoss

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

A little experiment using restarts.

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

![screenshot][]

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.