unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Arthur Miller <arthur.miller@live.com>
To: Hongyi Zhao <hongyi.zhao@gmail.com>
Cc: Stefan Monnier via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>,
	Stefan Monnier <monnier@iro.umontreal.ca>
Subject: Re: Closures in Emacs and their usage scenarios.
Date: Fri, 01 Oct 2021 05:37:40 +0200	[thread overview]
Message-ID: <AM9PR09MB4977DA15F8BD24CC15EC337E96AB9@AM9PR09MB4977.eurprd09.prod.outlook.com> (raw)
In-Reply-To: <CAGP6POLf5kGcwCefZq-ti+x8NU=odDU7zewwEYVP0-+kr4d1sQ@mail.gmail.com> (Hongyi Zhao's message of "Tue, 28 Sep 2021 22:50:08 +0800")

Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> On Tue, Sep 28, 2021 at 7:53 PM Arthur Miller <arthur.miller@live.com> wrote:
>>
>> Stefan Monnier via Users list for the GNU Emacs text editor
>> <help-gnu-emacs@gnu.org> writes:
>>
>> >> I noticed the document on closure here [1] implemented in Emacs/Elisp currently.
>> >> But it's only a very concise and short introduction, and I want to
>> >> know more about the closures in Emacs and their usage scenarios.
>> >> Any helpful tips are welcome.
>> >
>> > Maybe a good starting point is
>> >
>> >     https://en.wikipedia.org/wiki/Closure_(computer_programming)
>>
>> Chapter 2 from "On Lisp" by P. Graham has also very nice and accessible intro to
>> functions and closures:
>>
>> https://sep.yimg.com/ty/cdn/paulgraham/onlisp.pdf?t=1595850613&
>>
>> So has also "Let Over Lambda" by D. Hoyte:
>>
>> https://letoverlambda.com/index.cl/guest/chap2.html
>>
>> Chapter 2 is an entire chapter on closures and using them; if one is not scared
>> by subtitles like: "Let Over Lambda Over Let Over Lambda" :).
>
> I have excerpted all relevant discussions as follows from there [1]:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> Let Over Lambda Over Let Over Lambda
>
> Users of object systems store values they want shared between all
> objects of a certain class into so-called class variables or static
> variables8. In lisp, this concept of sharing state between closures is
> handled by environments in the same way that closures themselves store
> state. Since an environment is accessible indefinitely, as long as it
> is still possible to reference it, we are guaranteed that it will be
> available as long as is needed.
>
> If we want to maintain a global direction for all counters, up to
> increment each closure's counter and down to decrement, then we might
> want to use a let over lambda over let over lambda pattern:
>
> (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))))))
>
> In the above example, we have extended counter-class from the previous
> section. Now calling closures created with counter-class will either
> increment its counter binding or decrement it, depending on the value
> of the direction binding which is shared between all counters. Notice
> that we also take advantage of another lambda inside the direction
> environment by creating a function called toggle-counter-direction
> which changes the current direction for all counters.
>
> While this combination of let and lambda is so useful that other
> languages have adopted it in the form of class or static variables,
> there exist other combinations of let and lambda that allow you to
> structure code and state in ways that don't have direct analogs in
> object systems9. Object systems are a formalisation of a subset of let
> and lambda combinations, sometimes with gimmicks like inheritance
> bolted on10. Because of this, lisp programmers often don't think in
> terms of classes and objects. Let and lambda are fundamental; objects
> and classes are derivatives. As Steele says, the "object" need not be
> a primitive notion in programming languages. Once assignable value
> cells and good old lambda expressions are available, object systems
> are, at best, occasionally useful abstractions and, at worst,
> special-case and redundant.
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
> But TBF, these contents are still not so easy for me to understand.

I don't think you need to, to understand closures. What he says there is that
closures are more powerful than some other, more manistream tools used in
programming, particulary object orineted programming. To understand that you
would obviously you will need to have some udnerstanding of how object orinted
languages work, what are static class members, inheritance and so on, and you
would probably need to knoow it bit mroe than just how to use those. If you not
familiar with such topics I sugget to learn some Java and than try to udnerstand
that part later on.

My persoal opinion here is that the author is presenting closures as the basic
building block upon which to build other language primitives, and I think he is
showing us how simple basic concepts of Lisp are more universal than some other
concepts favoured by more popular languages. But that might be just mine
interpretation of that last part.



  parent reply	other threads:[~2021-10-01  3:37 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-27  9:40 Closures in Emacs and their usage scenarios Hongyi Zhao
2021-09-28  2:50 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28  2:54   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28  6:46   ` Hongyi Zhao
2021-09-28  8:30     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28  8:54       ` Hongyi Zhao
2021-09-28 10:39         ` tomas
2021-09-28 11:29           ` Hongyi Zhao
2021-09-28 13:31             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28 13:50               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28 13:57             ` tomas
2021-09-28 14:31               ` Hongyi Zhao
2021-09-28 15:25                 ` tomas
2021-09-29  3:59                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29  6:43   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28  2:55 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-09-28  4:11   ` [External] : " Drew Adams
2021-09-28  4:17     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28 11:53   ` Arthur Miller
2021-09-28 14:50     ` Hongyi Zhao
2021-09-29  4:04       ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-09-29  6:10         ` Tomas Hlavaty
2021-09-29 12:28           ` Stefan Monnier
2021-09-29 22:11             ` Tomas Hlavaty
2021-09-29 22:25               ` [External] : " Drew Adams
2021-09-30 10:58                 ` Tomas Hlavaty
2021-09-30 14:55                   ` Drew Adams
2021-09-30 15:54                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-01  4:35                     ` Hongyi Zhao
2021-09-29 23:06               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30  0:59               ` Hongyi Zhao
2021-09-30  3:27                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30 11:58                 ` Tomas Hlavaty
2021-09-30 13:27                   ` Hongyi Zhao
2021-09-30 15:29                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-01 14:46               ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-09-29 23:26             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-01  3:37       ` Arthur Miller [this message]
2021-10-08 10:53         ` Hongyi Zhao
2021-10-10 14:16           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-10 18:25             ` Marcin Borkowski
2021-10-11 23:16               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-12  5:29                 ` Marcin Borkowski
2021-10-12  5:32                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-03  9:07     ` Lisp books (was: Re: Closures in Emacs and their usage scenarios.) Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-03 14:41       ` Lisp books Stefan Monnier via Users list for the GNU Emacs text editor
2021-10-05 10:08         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05 12:57           ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-10-05 14:18             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-06  1:43               ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-10-06  3:20                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-06  6:44                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-06  7:06                 ` tomas
2021-10-06 10:17                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-06 12:37                   ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-10-06 12:54                     ` tomas
2021-10-06 20:24                       ` [OFFPTOPIC] ACM digital library (was: Lisp books) Stefan Monnier via Users list for the GNU Emacs text editor
2021-10-06 20:56                         ` tomas
2021-10-07  6:29                           ` Yuri Khan
2021-10-07  9:10                             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-07 12:42                             ` [OFFPTOPIC] ACM digital library Stefan Monnier
2021-10-12  5:44                         ` [OFFPTOPIC] ACM digital library (was: Lisp books) Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-12 18:11                 ` Lisp books Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-03 15:39       ` [External] : Lisp books (was: Re: Closures in Emacs and their usage scenarios.) Drew Adams
2021-10-05 10:10         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05 14:32           ` Drew Adams
2021-10-05 14:51             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28  7:11 ` Closures in Emacs and their usage scenarios Eduardo Ochs
2021-09-28  7:23   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28  7:33     ` Eduardo Ochs
2021-09-28  8:13   ` Hongyi Zhao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM9PR09MB4977DA15F8BD24CC15EC337E96AB9@AM9PR09MB4977.eurprd09.prod.outlook.com \
    --to=arthur.miller@live.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=hongyi.zhao@gmail.com \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).