help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Closures - do you understand them well?


From: Michael Heerdegen
Subject: Re: Closures - do you understand them well?
Date: Sun, 11 Dec 2022 03:24:06 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Tassilo Horn <tsdh@gnu.org> writes:

> > 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.

I do think the above is a valid solution.

> Where's the difference to
> this inlined version which returns (101 ... 101)?
>
> (let ((i 0) (funs))
>   (while (<= (cl-incf i) 100)
>     (push (lambda (&rest args2)
>             (apply #'identity (append (list i) args2)))
>           funs))
>   (mapcar #'funcall (nreverse funs)))

Well - `apply-partially' is a function and I use a simple function call,
so it is precisely not just something inlined: The important point is
that only the _value_ of the variable i passed, not the variable or
a reference to it:

#+begin_src emacs-lisp
(let ((i 27))
  (apply-partially #'identity i))
==>
  (closure
      ((args . (27))
       (fun . identity))
      (&rest args2)
    (apply fun (append args args2)))
#+end_src


Michael.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]