From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: JD Smith Newsgroups: gmane.emacs.devel Subject: Re: Code for cond* Date: Thu, 25 Jan 2024 10:17:47 -0500 Message-ID: <67B51664-352E-4EB1-AE17-6D571999FDDE@gmail.com> References: <1AD5807F-91F7-4B92-BCB0-D0FEA904A75D@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.300.61.1.2\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22431"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Madhu , emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Jan 25 16:18:46 2024 Return-path: Envelope-to: ged-emacs-devel@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 1rT1V8-0005ad-NM for ged-emacs-devel@m.gmane-mx.org; Thu, 25 Jan 2024 16:18:46 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rT1US-00036o-Gx; Thu, 25 Jan 2024 10:18:06 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rT1UQ-00036g-F9 for emacs-devel@gnu.org; Thu, 25 Jan 2024 10:18:02 -0500 Original-Received: from mail-qk1-x733.google.com ([2607:f8b0:4864:20::733]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rT1UO-0004aj-Ml for emacs-devel@gnu.org; Thu, 25 Jan 2024 10:18:02 -0500 Original-Received: by mail-qk1-x733.google.com with SMTP id af79cd13be357-7833b6bb41bso562827685a.3 for ; Thu, 25 Jan 2024 07:17:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706195879; x=1706800679; darn=gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=U5MQA4lyveoyHMKxMSHbJ47Pn8JnsefV0h2XQj3EICU=; b=crD0JpDp2iPLZr9lKbXq/F0ptrmDGkcfJw9Rccjz11vkpgCMAbEtHDp8YOIADqxxkL BQ25iIop/d+eqO1dD+6MGPFomyZk7V6mw3wiQKy6gYu2T0T6hgEato612wBW6surFnio eIzI/vaFrwJytmcK5el1yxiFcyZR5mT7OLmmcPLHzoV+DpHO3YVTk4vALbziEa0qjYoM suK6qZyxfHZMUjNloSWuBfcitdzAD12r6wca3yxJaz5I/JNrEuhdofVBWFAoo70eYI7t RgCSTVlj4VkPhjyxoLzrDYa576EH1+cs2eGaTjyPTGvCXef8OqSLWBYQ3I4cpJFP5+Fi HVcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706195879; x=1706800679; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U5MQA4lyveoyHMKxMSHbJ47Pn8JnsefV0h2XQj3EICU=; b=R2i5eJWjLSZdhFVsy6V0Z9gl6a71aedtL/jqpMvhBySpTDiMrWSVJVcb6SIKxhy6Mg HMkrj7QTqOXW1heiJrW3a/fy2slv6eNnoUcxJbqAkMlnUZK69eOh4cidKWUeZnZ3HnTf 1IVhYmwoJVsyk+J8Mn2mJg5XQ/QZZ54KXUG15X/4VAgq/mTp8kxh0UWlw9jjaUt0LFIj 3CbNSKL+//9rks2N12pg7JL6/rgnbo2CDX9KUYQR8XyIDTdBhSgomVezk1DcAyb9Qh6D yXX0oXVWHDgvuoDTi3kQ+X0+xveqiW1xhgQvvE4ORcg8RMK0hS+cTelduuZguAQpHHTI DHvg== X-Gm-Message-State: AOJu0YzowpZLmhY5HikBpTSfq9iTcr6cEF+SJ1rMhAKTPlxvCPmJm1oQ 3TiMgnIPqxAeJ70PY53lY55EDU4m5USCcZUuwcZVF6MWeTLtgZaijvSwWthl X-Google-Smtp-Source: AGHT+IHdtJ1wQthqQi94tDznbKa79B/83nK0iBj4Ag1pmbaATZ9fy/2AR/Hlg+++JBbEqTg+apSTow== X-Received: by 2002:a05:620a:a9b:b0:783:6463:3749 with SMTP id v27-20020a05620a0a9b00b0078364633749mr1173648qkg.95.1706195879055; Thu, 25 Jan 2024 07:17:59 -0800 (PST) Original-Received: from smtpclient.apple (cm-24-53-187-34.buckeyecom.net. [24.53.187.34]) by smtp.gmail.com with ESMTPSA id bi35-20020a05620a31a300b0078342dfebc6sm5122140qkb.12.2024.01.25.07.17.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Jan 2024 07:17:58 -0800 (PST) In-Reply-To: X-Mailer: Apple Mail (2.3774.300.61.1.2) Received-SPF: pass client-ip=2607:f8b0:4864:20::733; envelope-from=jdtsmith@gmail.com; helo=mail-qk1-x733.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: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:315363 Archived-At: > On Jan 25, 2024, at 8:57=E2=80=AFAM, Stefan Monnier = wrote: >=20 >>> [1] I regularly convince myself that it=E2=80=99s such low hanging = fruit, >>> there must in fact already BE a cond-let, and I go hunting for it. >>> The obvious interface seems like such a straightforward extension of >>> if/when-let, that there would be absolutely nothing new to learn: >>>=20 >>> (cond-let >>> (((var value) >>> (dvar (derived-from var)) >>> ((has-the-right-stuff-p dvar))) >>> (cons 'correct dvar)) >>>=20 >>> (((foo value2) >>> (bar (1- foo)) >>> ((< bar 0))) >>> (cons 'incorrect bar)) >>>=20 >>> (t nil)) >=20 > Nice. Would `var` and `dvar` scope over just the first branch or also = the > other ones? > Technically, there is no reason for them not to be visible in the = other > branches, so that's what I would go for. It seems most intuitive to me for each clause to have its own scope, but = mostly because active bindings made in sibling forms at the same depth = seem non-intuitive and easy to overlook. > Personally, I was thinking of a syntax like >=20 > (my-cond > (:let var value) > (:let dvar (derived-from var)) > ((has-the-right-stuff-p dvar) > (cons 'correct dvar)) >=20 > (:let foo value2) > (:let bar (1- foo)) > ((< bar 0) > (cons 'incorrect bar)) >=20 > (t nil)) >=20 > with the intention of replacing `cond` without introducing > any incompatibility. This is an interesting approach, and makes it much clearer when new = bindings appear. Mine is a different idea. In my cond-let approach, = just as for when-let, each binding in a clause=E2=80=99s binding group = is tested, one by one, and the clause only succeeds if all the bindings = are non-nil. So var and dvar must be non-nil, and = (has-the-right-stuff-p dvar) too. To ensure this, in your version, = I=E2=80=99d need something like: (:let var value) (:let dvar (and var (derived-from var)) ((and var dvar (has-the-right-stuff-p dvar)) =E2=80=A6) This would get repetitive and error prone for a large number of = bindings, not to mention awkward if some =E2=80=9Cbindings=E2=80=9D in = the middle are actually bound-variable-free tests like = ((has-the-right-stuff-p dvar)). The hypothetical cond-let makes it easy to build an arbitrary grouping = of sequential when's/when-let's which short circuit as soon as the first = clause body executes, no matter what that body returns. So: (cond-let (simple0 body0) ; a normal cond-style clause (bindings1 body1) (bindings2 body2) (bindings3 body3) (t fallthrough-body)) would be equivalent to: (catch 'cond-let (when simple0 (throw 'cond-let body0)) (when-let (bindings1) (throw 'cond-let body1)) (when-let (bindings2) (throw 'cond-let body2)) (when-let (bindings3) (throw 'cond-let body3)) (throw 'cond-let fallthrough-body)) But a lot less wordy/repetitive ;).=20=