all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Marcin Borkowski <mbork@mbork.pl>
To: Emanuel Berg <moasenwood@zoho.eu>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Closures in Emacs and their usage scenarios.
Date: Sun, 10 Oct 2021 20:25:15 +0200	[thread overview]
Message-ID: <87mtngu48k.fsf@mbork.pl> (raw)
In-Reply-To: <87r1ct3qzf.fsf@zoho.eu>


On 2021-10-10, at 16:16, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Hongyi Zhao wrote:
>
>> I'm learning "Advising Emacs Lisp Functions" now.
>> According to my current superficial understanding, it seems
>> that both closure and advice function are intended to
>> provide a clean and concise method to patch/repair/adapt the
>> existing function/macros with a most consistent way.
>
> That should be one of many use cases for advising functions,
> I don't know how one does that with closure tho ...
>
> I've still only seen two use cases for closures, one is the
> persistent variable (in C you'd use a static variable, in
> Python just a global one) and the other one is the sharing of
> one "almost global" variable between two or more functions (in
> both C and Python, that would be a real global variable
> instead).
>
> And the second use case is a version of the first, or
> extension perhaps, since that variable (or set of variables)
> would also be persistent. It looks a lot like OOP to me -
> I say it in that order because I learned the OOP basics/theory
> before I heard of closures, but I expect closures were
> actually first, right? - and it is even the very core of OOP
> (the coupling/enclosure of data and functions/methods that
> operate that data) - so we can say not without reason that
> Lisp is the original OOP - with the core stuff implemented in
> such as simple way - but without all the other stuff that no
> one uses anyway :)

Yet another use (which of course - technically - is again a variant of
the same thing) is generating a closure whose behavior depends on the
argument of the function that defines it.

<shameless plug>

A simple example from my book:

(defun negate (fun)
  "Return a function returning the logical opposite of FUN."
  (lambda (&rest args)
    (not (apply fun args))))

</shameless plug>

so that (negate #'zerop) behaves like a function testing its argument
for "non-zeroness" (i.e., returning t unless its argument is 0, when it
returns nil).

As a homework, try to use it under dynamic binding and see why it won't
work.  (See also this thread:
https://lists.gnu.org/archive/html/help-gnu-emacs/2021-09/msg00267.html .)

Best,

-- 
Marcin Borkowski
http://mbork.pl



  reply	other threads:[~2021-10-10 18:25 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
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 [this message]
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

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

  git send-email \
    --in-reply-to=87mtngu48k.fsf@mbork.pl \
    --to=mbork@mbork.pl \
    --cc=help-gnu-emacs@gnu.org \
    --cc=moasenwood@zoho.eu \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.