|
From: | Stefan Monnier |
Subject: | Re: Closures in Emacs and their usage scenarios. |
Date: | Wed, 29 Sep 2021 00:04:49 -0400 |
User-agent: | Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
> (let ((direction 'up)) > (defun toggle-counter-direction () > (setq direction > (if (eq direction 'up) > 'down > 'up))) > > (defun counter-class () > (let ((counter 0)) > (lambda () > (if (eq direction 'up) > (incf counter) > (decf counter)))))) Note that in real life closures that share some of their environment with another closure are quite rare. And captured variables that are mutated are also quite rare (and in languages like Haskell or OCaml they can't exist since variables aren't mutable ;-). So I don't find such examples very useful to teach closures. Stefan
[Prev in Thread] | Current Thread | [Next in Thread] |