[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Mutation - do you understand it really? (was: Closures - do you understa
From: |
Stefan Monnier |
Subject: |
Mutation - do you understand it really? (was: Closures - do you understand them well?) |
Date: |
Sat, 10 Dec 2022 11:28:43 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Tassilo Horn [2022-12-10 10:34:24] wrote:
> Michael Heerdegen <michael_heerdegen@web.de> writes:
>>> Write an expression that returns a list of 100 functions accepting
>>> zero arguments. `funcall'ing the Nth function must return N. Use a
>>> `while' loop or whatever you like but please not `dolist' or any
>>> tool that already handles this "problem" specifically.
>>
>> Would this be a valid solution?
>>
>> #+begin_src emacs-lisp
>> (let ((i 0) (funs))
>> (while (<= (cl-incf i) 100)
>> (push (apply-partially #'identity i) funs))
>> (nreverse funs))
>> #+end_src
>
> Apparently it works although I don't see why.
BTW, for those people here who find this all confusing.
I'll point out that the culprit is not "closures" but `setq` (here
hidden within `cl-incf`).
Immutable variables are just a lot better behaved (and easier to work
with for the compiler, which is why GCC/LLVM/... use an SSA conversion
to replace those nasty mutable variables with immutable ones).
Stefan
- Re: Closures - do you understand them well?, (continued)
- Re: Closures - do you understand them well?, Emanuel Berg, 2022/12/22
- Re: Closures - do you understand them well?, tomas, 2022/12/23
- Re: Closures - do you understand them well?, Emanuel Berg, 2022/12/10
- Re: Closures - do you understand them well?, Michael Heerdegen, 2022/12/09
- Re: Closures - do you understand them well?, Tassilo Horn, 2022/12/10
- Re: Closures - do you understand them well?, Emanuel Berg, 2022/12/10
- Mutation - do you understand it really? (was: Closures - do you understand them well?),
Stefan Monnier <=
- Re: Mutation - do you understand it really?, Michael Heerdegen, 2022/12/10
- Re: Closures - do you understand them well?, Michael Heerdegen, 2022/12/10
- Re: Closures - do you understand them well?, Tassilo Horn, 2022/12/11
- Re: Closures - do you understand them well?, Emanuel Berg, 2022/12/10
- RE: [External] : Re: Closures - do you understand them well?, Drew Adams, 2022/12/10
- Iteration macros (was: [External] : Re: Closures - do you understand them well?), Stefan Monnier, 2022/12/10
- RE: Iteration macros (was: [External] : Re: Closures - do you understand them well?), Drew Adams, 2022/12/10
- Re: [External] : Re: Closures - do you understand them well?, Emanuel Berg, 2022/12/21
- Re: Closures - do you understand them well?, Emanuel Berg, 2022/12/08
- Re: Closures - do you understand them well?, Michael Heerdegen, 2022/12/08