From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.bugs Subject: bug#56875: 29.0.50; [PATCH] Add thread-as macro Date: Mon, 01 Aug 2022 19:06:53 +0200 Message-ID: <87h72vq4eq.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35475"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) To: 56875@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Aug 01 19:09:32 2022 Return-path: Envelope-to: geb-bug-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 1oIYv5-00092u-UL for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 01 Aug 2022 19:09:32 +0200 Original-Received: from localhost ([::1]:40576 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIYv4-0003Xe-WC for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 01 Aug 2022 13:09:31 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55860) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIYtf-0002Hc-AS for bug-gnu-emacs@gnu.org; Mon, 01 Aug 2022 13:08:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:51781) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIYte-0004Y6-Ec for bug-gnu-emacs@gnu.org; Mon, 01 Aug 2022 13:08:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oIYte-0006I9-9U for bug-gnu-emacs@gnu.org; Mon, 01 Aug 2022 13:08:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Augusto Stoffel Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 01 Aug 2022 17:08:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 56875 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.165937362624103 (code B ref -1); Mon, 01 Aug 2022 17:08:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 1 Aug 2022 17:07:06 +0000 Original-Received: from localhost ([127.0.0.1]:41530 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIYsj-0006Gg-Hz for submit@debbugs.gnu.org; Mon, 01 Aug 2022 13:07:05 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:53020) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIYsd-0006GC-Pb for submit@debbugs.gnu.org; Mon, 01 Aug 2022 13:07:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55492) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIYsd-00015h-J9 for bug-gnu-emacs@gnu.org; Mon, 01 Aug 2022 13:06:59 -0400 Original-Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]:44592) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIYsb-0004IU-Og for bug-gnu-emacs@gnu.org; Mon, 01 Aug 2022 13:06:59 -0400 Original-Received: by mail-ed1-x532.google.com with SMTP id i13so14569317edj.11 for ; Mon, 01 Aug 2022 10:06:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:user-agent:mime-version; bh=0pQuMitzs4gX9XOP+pTmDDnodhFuoZOxmgCE98SIO7k=; b=EaVtoqzg4ekhnnajuXLMejh0NQYxvRJE6KKOu9nwy1wsFjD4IgyMTuCLZQD9FWPel9 v0+xzlFksvG8uc/wKVwh2M5OiIn9m6egjVV3/GU+1LnsDGdjBb2c1qU3efblWldL7C2V OC6G13FJjKlJdLdoJSIZVVYQ4eKmfI4tXoghrsX9Cc/RJ+IEp0eU6/FDISnZ/YR2fZ1U cHuiT0l+z3hlPgekbXM8bfOl4iDXxLjUNB3SwFwtpk/4FDYi2DdKrNBMqMg0YFW6ROH4 gEXSLzq2UWMiRKqy48c18iwZe7wCiQzWR/56T82d2NZLCk8vaxwlgAZDNwjFL2S9A7zO x05Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:user-agent :mime-version; bh=0pQuMitzs4gX9XOP+pTmDDnodhFuoZOxmgCE98SIO7k=; b=p+hTxUszuHoR+Q32NjSpT2f84so58w/P7F/viMushzRvqEKFlO3zVg+q6wQ0EZ8WO2 WuTROpZwduB0/nquICd/xIjJ1VzP6IrwHOEOOm6GKMdVX2t1kBqrh1g/x1t3MoGrfhdf CS91nIFXF0f8ZfqWffmPGLeEYYUGBC2j51riCMH97dEOzro+DgndfgCgNvwjCzrzwGIb wMWF/ZCvEz3/DjuPJyxhYY42Z341xR+uPl/RQdZAp7iIlsbZhb+TbF+BcRg4gRJPgiBX kRDyZCQzezMv7AZP0COsgGdtm6qu0AOYQSlbfVpWy15vxGdPZLDqet0/jZBNJ94CLa94 ZNwg== X-Gm-Message-State: ACgBeo1pfzPUQjhmxyo2/ZnFup2TaV+UyWr20IqNO/S/5j5rnqvu8s9H LCYUcYxOXQqHBXSJx1zvCqIDoHNT+jU= X-Google-Smtp-Source: AA6agR4unwrRgG81Yff+lZ6ETCsFY69jHIvhvpMmMUryHrkiE0ybfQCw9vDphnW1Jcg8WCMLigOb6Q== X-Received: by 2002:a05:6402:5190:b0:43d:da02:566a with SMTP id q16-20020a056402519000b0043dda02566amr1864508edd.187.1659373614916; Mon, 01 Aug 2022 10:06:54 -0700 (PDT) Original-Received: from ars3 ([2a02:8109:8ac0:56d0::11cf]) by smtp.gmail.com with ESMTPSA id x25-20020aa7d6d9000000b0043bc4b28464sm7078554edr.34.2022.08.01.10.06.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Aug 2022 10:06:54 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::532; envelope-from=arstoffel@gmail.com; helo=mail-ed1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:238451 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable It has been noted many times that threading macros are not as useful in Emacs Lisp because the APIs are not always consistent with regard to the ordering of arguments. A =E2=80=9Cthread-as=E2=80=9D macro on the lines of= Clojure's 'as->' [1] would address this difficulty. The signature of the Clojure macro is (as-> expr name & forms), which looks a bit odd to me. Here I've changed this so the lexical variable (name) comes before the initial value (expr). Also, unlike thread-first and thread-last, in the attached version of the macro there's no magic whereby an element of FORMS which is a symbol, say fun, is turned into a function call, i.e. (fun name). In other words, (thread-as x 1 1+) returns the symbol 1+. Clojure behaves the same. I'm not necessarily against changing the macro so that the above example returns 2, but I don't see a good reason to do so either; for one thing, the example gives a byte-compilation warning (unused lexical variable x). [1]: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/a= s-%3E --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: attachment; filename=0001-Add-thread-as-macro.patch Content-Transfer-Encoding: quoted-printable >From c044cf5891638cd0095db091e86303d683cf380d Mon Sep 17 00:00:00 2001 From: Augusto Stoffel Date: Mon, 1 Aug 2022 15:59:53 +0200 Subject: [PATCH] Add thread-as macro * lisp/subr-x.el (thread-as): New macro. --- lisp/emacs-lisp/subr-x.el | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index d5d7bfeb6f..916ed39a03 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -81,6 +81,23 @@ thread-last threading." (declare (indent 0) (debug thread-first)) `(internal--thread-argument nil ,@forms)) + +(defmacro thread-as (var &rest forms) + "Successively bind VAR to the result of evaluating each of the FORMS. +Return the last computed value. + +Example: + (thread-as x + 4 + (- 10 x) + (/ x 2)) + =E2=87=92 3" + (declare (indent 1)) + (if forms + `(let ((,var ,(car forms))) + (thread-as ,var ,@(cdr forms))) + var)) + (defsubst hash-table-empty-p (hash-table) "Check whether HASH-TABLE is empty (has 0 elements)." (zerop (hash-table-count hash-table))) --=20 2.37.1 --=-=-=--