From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: Closures in Emacs and their usage scenarios. Date: Wed, 29 Sep 2021 08:28:10 -0400 Message-ID: References: <87ilyjlxnn.fsf@logand.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36475"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: help-gnu-emacs@gnu.org To: Tomas Hlavaty Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 29 14:30:00 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 1mVYil-0009C4-Re for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 14:30:00 +0200 Original-Received: from localhost ([::1]:55708 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVYik-0006nj-U2 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 08:29:58 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58498) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVYh8-0006j2-0n for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 08:28:18 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:25539) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVYh5-0002sV-GZ for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 08:28:17 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 864E510021B; Wed, 29 Sep 2021 08:28:12 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 16942100025; Wed, 29 Sep 2021 08:28:11 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1632918491; bh=mEr3NnF2yh6TBEhqYJr412Y0dmyGat37HkU4hOPL8mE=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=AsbFkQlW/YXvqTL0TAFEic/DJOg2KbEM+ja3pKM0g6ScEGgYcUUVK7+9uuImtHm7Y X+eXGZ46qINolDyhHOXn/gAORT6MfiRHpfawQRmT1JvDk+T7CcPUtd4jmqV89GpA/L /BBvPhFJInveX7l+eyailtaCj7LFefV91BpUvbH7GaMG6RmJJ87OJf+s+3LCaoFKpo QiFY37z70ebgLn+JB1Axb7P9TeZbbN0ryOCSm7lpNQX8z1neVj0t0bZRgNJxydYYg5 xWzWtVzMQ1uDHW0AYlxAurr7b0rhAQEVPXm/Yz3Mxv6wgZPyYr97GtXhDIiq9DHt8r KJmAYe3PAWL7A== Original-Received: from milanesa (unknown [45.72.241.23]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id C86CF1203DF; Wed, 29 Sep 2021 08:28:10 -0400 (EDT) In-Reply-To: <87ilyjlxnn.fsf@logand.com> (Tomas Hlavaty's message of "Wed, 29 Sep 2021 08:10:36 +0200") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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:133389 Archived-At: 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), but in my experience in ELisp they're the exception rather than the rule and IIUC it's the same in Scheme. Note also that such mutated+captured variables are more costly (at least in the most prevalent way to implement closures). E.g. in ELisp, a code like (let ((x 0)) (lambda () (setq x (1+ x)))) will be turned into (let ((x' (list 0))) (lambda () (setcar x' (1+ (car x'))))) [ With more work we could avoid such a rewrite in the above case, but it's basically unavoidable in more complex cases like: (let ((x 0)) (list (lambda () (setq x (1+ x))) (lambda () x))) Note that the same kind of rewrite is used in many/most Scheme implementations. ] > The best thing about closures is that they allow > wrapping state mutations under simple interface of funcall. You may be fond of that feature but closures are very valuable even without it. Stefan