2 ms·
Are they similar in any way to Python generators? They are functions with a state, at least.
by sli 17y ago
Are they similar in any way to Python generators? They are functions with a state, at least.
- jrockway 17y agoMultiple closures can share the same state ("environment"). Consider: (let ((i 0)) (list (lambda () (incf i)) (lambda () (decf i)) (lambda () i))) If you evaluate the first function this form returns ("increment") and then the third form ("get"), the result is 1. If you evaluate the whole form again, you now have another "instance", with its own separate shared i. In Python, you would probably just make this a class. (But sometimes it's nice to have something equivalent to a class that's automatically built for you, and closures are that something.)