From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Hongyi Zhao Newsgroups: gmane.emacs.help Subject: Re: Closures in Emacs and their usage scenarios. Date: Thu, 30 Sep 2021 08:59:13 +0800 Message-ID: References: <87ilyjlxnn.fsf@logand.com> <87y27fjal0.fsf@logand.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35018"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs , Stefan Monnier To: Tomas Hlavaty Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Sep 30 02:59:54 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mVkQU-0008w3-6P for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 30 Sep 2021 02:59:54 +0200 Original-Received: from localhost ([::1]:42474 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVkQS-0005a3-R6 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 20:59:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39598) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVkQ5-0005XW-16 for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 20:59:29 -0400 Original-Received: from mail-vs1-xe34.google.com ([2607:f8b0:4864:20::e34]:43662) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mVkQ2-0007ok-G5 for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 20:59:28 -0400 Original-Received: by mail-vs1-xe34.google.com with SMTP id n17so5284012vsr.10 for ; Wed, 29 Sep 2021 17:59:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=d/lgZTZaQNgsO4Kq/9+J+M4nEZRhKaOcQ5q06LV7sQo=; b=HtjmOytWh6LmtQCrob3clKla7xc1olhzZMbmBvtMW2zCmxoCb7YBwFh7C1/tgCq9v6 Mc1iigoyKQT3YHAjuNrTElu54pLqQly62OypM5FIkX0VRXITnYAj8bvQ/diAMRxu0lEg aXEKOtURP31vSCHJtvIi6gnQsglVgso748s+9cqWobokN/bAUpA8BJWrKvD83eHaQrPA RYdTRvPLFXpIcSmXFLA07TFssorkAx/ZHnVJYQe6gepyLlZDhNrZKBwwHMvJlOyj+Dx+ iHRpsixJ9Db7iHNe+vhvYtbdeeTlkPsVXvuuPmUd1n4ftLqe1MfJMZ9HSv9UxAz151L5 suPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=d/lgZTZaQNgsO4Kq/9+J+M4nEZRhKaOcQ5q06LV7sQo=; b=jfvWUA3iMsxc1QTa+P9afRylG2iIYSa/y5xVavMmg4zdJgrIecJ5YC7Ctxniiis9rS rlYv2tgH/Ir4yKUn/FEqD/dqgDcBWjY0LJMyGZcCV9FEMxhN2Lz2vynarB63Ay8O9CCt XQznh0z9F25DG37mz5oFV88gu/ZOpk3AFUftBNY1KVmlkO1hASFqRiFMOSwESGMAjHwt bAcnXcIQ8juIJ+xJOU9e0+UI1Qf7YnG9eZzUIdZFEarpgFB4nHvXUYfduUx66NLA1AJa Uq56w60ABq05lj3TiBZVD+CywbOMGWqwh4kcmjcfIki/oRhDCOyXHsLnlBVwwciG68yc mAOw== X-Gm-Message-State: AOAM530wtBeYI47vtOFx58jmLKRTotG4QSAKmljfKr7W58GNrDL8Eglt LTWVOUIUAP60NII+VYkMZI/GsXXEMdTFPOn8aCo= X-Google-Smtp-Source: ABdhPJzGZPB8ZOxAFQvTciJot8o2eVlmJ2U3waUAUNnQ9DUF9ao1DjTNSXc3KjjpwBsAaTgo2hYj/m54QG4gx03iTGg= X-Received: by 2002:a67:edd7:: with SMTP id e23mr1371736vsp.14.1632963565331; Wed, 29 Sep 2021 17:59:25 -0700 (PDT) In-Reply-To: <87y27fjal0.fsf@logand.com> Received-SPF: pass client-ip=2607:f8b0:4864:20::e34; envelope-from=hongyi.zhao@gmail.com; helo=mail-vs1-xe34.google.com X-Spam_score_int: -2 X-Spam_score: -0.3 X-Spam_bar: / X-Spam_report: (-0.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URI_DOTEDU=1.787 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:133403 Archived-At: On Thu, Sep 30, 2021 at 6:11 AM Tomas Hlavaty wrote: > > On Wed 29 Sep 2021 at 08:28, Stefan Monnier wrote: > > Tomas Hlavaty [2021-09-29 08:10:36] wrote: > >> On Wed 29 Sep 2021 at 00:04, Stefan Monnier via Users list for the GNU Emacs > >> text editor wrote: > >>> And captured variables that are mutated are also quite rare > >> I don't think so. > > > > How rare can depend on the language (as mentioned: they are completely > > absent from Haskell/OCaml and friends), > > The topic is: Closures in Emacs and their usage scenarios. > > In Emacs Lisp, closures mutating captured variables might be rare now > because lexical scoping arrived recently. > > But it opens a new world of possibilities worth learning about. > It should be encouraged. > Many usage scenarios could be seen in Common Lisp and Scheme. > > There are examples in the Emacs code base already, see thunk-delay, for > example. > > >> The best thing about closures is that they allow wrapping state > >> mutations under simple interface of funcall. > > The toggle-counter-direction is an interesting example. > > Next step could be showing the wonderful concept of generators; > brilliantly shown, for example, in a solution of the same fringe > problem. Once one understands this, it is a great way of creating light > weight streams on the spot. It is also a robust solution to off-by-one > errors often caused by complexities of indexing in complex loops. > > @Hongyi Zhao: learn about the same fringe problem What's the exact meaning of "the same fringe problem"? > and possible solutions. It is worth the time and effort. I asked this question based on the following related concepts that I am currently considering: iterator, generator, recursor and closure (a decorator in Python is essentially a closure). According to the theory discussed by John McCarthy [1], it seems that the recursor or recursive Functions have a more important position in these concepts. [1] http://www-formal.stanford.edu/jmc/recursive/recursive.html > > but in my experience in ELisp they're the exception rather than the > > rule > > Because closures and dynamic binding does not go together well. > Luckily, that is changing now and closures became relevant in ELisp. > > > Note also that such mutated+captured variables are more costly (at > > least in the most prevalent way to implement closures). > > The cost is mostly theoretical and usually irrelevant for practical > purposes. I would say on the contrary, the code can be simpler, more > readable and robust and easier to optimize than alternatives. > > There is lots of work being done on ELisp compilation, so the future > seems bright. Maybe representing the captured variables as an alist is > more costly than if it was an array? (For example, I never had a > performance issue with closures mutating captured variables in sbcl.) > In any case, optimizing this is not really a high priority issue I > think.