GNU bug report logs - #76413
[PATCH] New macro 'compf' for composing functions

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Severity: wishlist; Reported by: Eshel Yaron <me@HIDDEN>; Keywords: patch; dated Wed, 19 Feb 2025 13:53:02 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 23 Feb 2025 00:13:55 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Feb 22 19:13:54 2025
Received: from localhost ([127.0.0.1]:57926 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tlzd4-0000ox-Js
	for submit <at> debbugs.gnu.org; Sat, 22 Feb 2025 19:13:54 -0500
Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]:54373)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <stefankangas@HIDDEN>)
 id 1tlzd2-0000oa-Mu
 for 76413 <at> debbugs.gnu.org; Sat, 22 Feb 2025 19:13:53 -0500
Received: by mail-ed1-x52a.google.com with SMTP id
 4fb4d7f45d1cf-5e05717755bso4986064a12.0
 for <76413 <at> debbugs.gnu.org>; Sat, 22 Feb 2025 16:13:52 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1740269626; x=1740874426; darn=debbugs.gnu.org;
 h=cc:to:subject:message-id:date:mime-version:references:in-reply-to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=GlauLH8k4ndf5N3+R76h6gEiZIVpOd1Rgzuu9O5x1j4=;
 b=WFitm6pemXhWzF3rufOg3egzcU+dB6D1Idz2TMqqEQCSwuFd5oEKlroeSyN50BF5N4
 CEmwiOyTsykn3/02NhNOy7cmXqsYs1nBlEY0BYnZeB3uBIQO1Gr+SHoilvTktt6a4RBg
 RW5LvLBpIteSCyPoSUTCI3cTllMTC7rYSma+h5jd0J95Ye49nxjpzmQOTzQvj9+Givwg
 FA5UMJSxhtqBtapMonUJWMJcRikE81cmS+70+N/jMb7qz5gJVwMUE1Q36MibyDDw7vTf
 0NcU/Z0o+w6dplxGSgNaV6PpXEFPrpTwNJvmh9WxY78/wvBpPPNvDIGYDvzFPw3wqyOZ
 VGgw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1740269626; x=1740874426;
 h=cc:to:subject:message-id:date:mime-version:references:in-reply-to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=GlauLH8k4ndf5N3+R76h6gEiZIVpOd1Rgzuu9O5x1j4=;
 b=NIMc4+Q478fkIsmNzL/S//HgVVoecpHpikIoEsUdAn0FmUe2GkJEcDaiyH/kk5foXW
 TavwMc45N1Ao4xiKzBAdB4xLA8T3lBMX7VEAhqmBJdgkvlJkE27y9iXKLBTmiofItNp+
 7hO5RRdmuir9gu7oV0PZGD+LJoBAGwsL+ewMZyQt9EWINERdh6khuG61dh5eDvkZjMyu
 hYVytkDyT8t4VTc/e33wZIJ09ENjZlbCI2pXpdLH6RS/6fwfzFntUQdCSo3zvBEBL7QX
 7gzUl0zUkRstCE7N1GbV50HQ16NPbwDnz3hK8yrse7L07fNk+RqbSaOKw00AMoH3iOfu
 iYIg==
X-Forwarded-Encrypted: i=1;
 AJvYcCXI0Y/bm/tyeS8SIaJAmWTfTqyZPY4M3u++jLKVQLVPNXlaAeIbTdijRdI70uGGb4yqNCnTaA==@debbugs.gnu.org
X-Gm-Message-State: AOJu0Yy2NZcN6NiilMEZvCE53HV1kXcQl6KE1lqBbFf2dnCIF2xWd9wy
 kBpaK4u/EqOtsd63Rahryt08lfJqjamSANIDkWRO4WMl5IUhLfWdO+rplVTR+I8e0Fypf/W5O49
 qeiCUnvFKhSwCd3ACD9GH65m6dTY=
X-Gm-Gg: ASbGnctUe5MJYRzhV+mNkLFnUtfCiay7cj0Ek/QIFWMsxl0oMcsBKXf2v6W4WqIBIha
 WysKdS4ls8bnS2sntzi962B4ErxpTr/GXrg759TmroRNlC9IIZgncrF/HImVRYdUOX9Ezt/GOkI
 rkl3fiVVGJ
X-Google-Smtp-Source: AGHT+IHdF3z7oooCHs6gp3InINIlzQnA9N+dZxV1ItBfSDb8ZkePEUB9x2H+GcDoblEkNPbpHo51jloTq3k6oY+jENY=
X-Received: by 2002:a05:6402:4589:b0:5df:b0e9:a5da with SMTP id
 4fb4d7f45d1cf-5e0b70f870emr7228678a12.16.1740269626368; Sat, 22 Feb 2025
 16:13:46 -0800 (PST)
Received: from 753933720722 named unknown by gmailapi.google.com with
 HTTPREST; Sun, 23 Feb 2025 00:13:45 +0000
From: Stefan Kangas <stefankangas@HIDDEN>
In-Reply-To: <m1frk9hi9t.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN>
 <CADwFkmnOdHN0fjeAP-cnwOoa2vXqF_NeR=x5i1Dx6VL3hue1TA@HIDDEN>
 <m1frk9hi9t.fsf@HIDDEN>
MIME-Version: 1.0
Date: Sun, 23 Feb 2025 00:13:45 +0000
X-Gm-Features: AWEUYZlLSwNsibp7VSW330jTaS2ToeUSwZ58oIG9WhfshdKqFNyuv_jShvDEkx4
Message-ID: <CADwFkmk93pjBHn2i6wyhi_mtTJCTmFRXKaBK985Qu6u-UKygiA@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
To: Eshel Yaron <me@HIDDEN>
Content-Type: text/plain; charset="UTF-8"
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76413
Cc: Stefan Monnier <monnier@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Eshel Yaron <me@HIDDEN> writes:

>> Why not name it `compose` as in CL and ELisp?
>
> It's a slightly different construct then Common Lisp's compose, so I
> feel like calling it by that name could lead to wrong expectations.
> Another option is fcomp instead of compf, BTW, if that seems better.

If it is different, then I agree that a different name is in probably in
order.  But see below.

>> How about a function with a compiler macro?
>
> That's a valid alternative, but it'd require quoting function names,
> which is slightly less concise/convenient.
>
> Do you see a clear advantage for using a function in this case?

It's slightly less typing, but also risks being more unclear, as it has
different evaluation rules.  The name `compf` is similarly fine, but to
me comes off as a little bit cryptic.

I think allowing function names to be in variables is also more general
and thus flexible, for instance supporting things like:

    (let ((fun #'some-function))
      (mapcar (compose #'not fun) some-list))

I also forgot to say thanks for digging up the old thread and this idea.
It had completely fallen off my radar.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.
Severity set to 'wishlist' from 'normal' Request was from Stefan Kangas <stefankangas@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 19 Feb 2025 19:00:21 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 14:00:21 2025
Received: from localhost ([127.0.0.1]:51702 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkpIy-0000MN-Uu
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 14:00:21 -0500
Received: from mail.eshelyaron.com ([107.175.124.16]:41170 helo=eshelyaron.com)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <me@HIDDEN>) id 1tkpIv-0000Ks-NT
 for 76413 <at> debbugs.gnu.org; Wed, 19 Feb 2025 14:00:18 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1739991617;
 bh=b3u4ImznH6i9nrNe5G12eRaEJ+lzuSahUWaAyRTBF4s=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=K1PZHozyQcYiIUATjSNynXJvQWGu8I0sE0achukg0Yz/w23bFMyNdMCY585xhQ7hw
 YYrjyzQKBjMJ2wk4oa17keBrgMgbcwz0px1o9L9cWgqKq78R2ghd0fEQtAs2L7OsWl
 DxOuIYZI9rwAbC84xMIy+DMr4/6WNPYcrG0DOReKx+111Kn7KoqzcMNLyz1mEfEZYj
 oAuWQYZr1mjqtgSZ+2XGEzEb+01xyG03waK3EaUmOp8tABPwgMu/EhwugR+TBZ9Uzn
 dqA2b9H1du1eAxHEStQfnEUWc4jTtZc8OjGMzmlQ3DNGbFdNAgjAiQb/XDcCu4WUZm
 W9O6TyYAQzuaA==
From: Eshel Yaron <me@HIDDEN>
To: Stefan Kangas <stefankangas@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
In-Reply-To: <CADwFkmnOdHN0fjeAP-cnwOoa2vXqF_NeR=x5i1Dx6VL3hue1TA@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN>
 <CADwFkmnOdHN0fjeAP-cnwOoa2vXqF_NeR=x5i1Dx6VL3hue1TA@HIDDEN>
Date: Wed, 19 Feb 2025 20:00:14 +0100
Message-ID: <m1frk9hi9t.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76413
Cc: Stefan Monnier <monnier@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Hi Stefan,

Stefan Kangas <stefankangas@HIDDEN> writes:

> Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
> editors" <bug-gnu-emacs@HIDDEN> writes:
>
>> Tags: patch
>>
>> This patch adds a new macro 'compf' that streamlines the common pattern
>> of function composition.
>>
>> Namely, instead of (lambda (x) (foo (bar (baz x)))), with this macro we
>> can write (compf foo bar baz), which expands to exactly the same form.
>
> Why not name it `compose` as in CL and ELisp?

It's a slightly different construct then Common Lisp's compose, so I
feel like calling it by that name could lead to wrong expectations.
Another option is fcomp instead of compf, BTW, if that seems better.

> How about a function with a compiler macro?

That's a valid alternative, but it'd require quoting function names,
which is slightly less concise/convenient.

Do you see a clear advantage for using a function in this case?


Thanks,

Eshel




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 19 Feb 2025 18:46:47 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 13:46:47 2025
Received: from localhost ([127.0.0.1]:51539 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkp5r-0007AX-Bq
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 13:46:47 -0500
Received: from mail.eshelyaron.com ([107.175.124.16]:55862 helo=eshelyaron.com)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <me@HIDDEN>) id 1tkp5p-0007A5-3k
 for 76413 <at> debbugs.gnu.org; Wed, 19 Feb 2025 13:46:46 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1739990802;
 bh=sZZP0YjwYlVqeCpLWXuIy+sLiqsDo+CFzr4jBL7anpc=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=p1E9cnbD4GnELfD0x5bVaQmAPGJMMDYYIcOhf/i5X9WZ0SskFhcEHF1xJdKVCtyZA
 JluFt0UAgJlO55vVmyZk0PpnXpzvnuQiM9TtS3MU+wLheldvOqyPhA5rYFuE30KApN
 aOmQaU1U48Nmx52h0QBlNptOXYjxMrWe6YshTcFW3cvZTKovYPXTukFEoB39EXHT32
 h/wbjym8Mfl8Jx2f4v/a6xIb9IzMS7kt12soiq6K40TGMMOlqIFbKQ4ygGroIBN7Xw
 Uc2U2JQVkRTILsb4FgVU59e4Rvt7Rlbe8cIPCU1eiH7TeX3Nn3L1g/8aGW7Gv4fujG
 iWnl9t0zy0/vw==
From: Eshel Yaron <me@HIDDEN>
To: "Basil L. Contovounesios" <basil@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
In-Reply-To: <87v7t5j1sw.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN> <87mseic8an.fsf@HIDDEN>
 <m134gakm27.fsf@HIDDEN> <87v7t5j1sw.fsf@HIDDEN>
Date: Wed, 19 Feb 2025 19:46:39 +0100
Message-ID: <m1frk9ixgw.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76413
Cc: Michael Albinus <michael.albinus@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Hi,

"Basil L. Contovounesios" <basil@HIDDEN> writes:

> Eshel Yaron [2025-02-19 16:10 +0100] wrote:
>
>> From e2834b162f3f1fa1c8dc8e057e3aa00950269d8e Mon Sep 17 00:00:00 2001
>> From: Eshel Yaron <me@HIDDEN>
>> Date: Wed, 19 Feb 2025 12:58:40 +0100
>> Subject: [PATCH v2] New macro 'compf' for composing functions
>
> Nice.
>
>> +FUNS may contain symbols which refer to functions, such as `car' and
>> +`cdr' in the example above, and it can also contain `lambda' functions
>> +and other forms which evaluate to function values.
>
> A (declare (debug ...)) property would be nice, particularly for
> stepping through the latter types of argument.

Oh, good idea...  The following spec seems to work well, but I'm not
that experienced with Edebug specs, so improvement suggestions would be
very welcome:

  (declare (debug (&rest &or symbolp vectorp lambda-expr form)))

>>  To refer to a local
>> +variable VAR that is bound to a function, wrap VAR in a vector, as in:
>> +
>> +  (let ((foo (lambda (...) ...)))
>> +    (compf ignore [foo] always))
>
> Is there precedent for this [syntax] somewhere?

Not that I know of.

> I understand that function symbols may be needed more frequently than
> variable symbols in arguments to compf,

Indeed, that's the use case I'm optimizing for.

> but did you consider distinguishing between the two kinds of symbol by
> #'-quoting function symbols, and leaving variables unquoted?  AFAIK
> this has more precedents in Elisp.

I have considered and tried it, and I found that alternative slightly
more cumbersome.  It's nice to be able to pass function names unquoted.
Of course I'm open to reconsider if people have a strong preference.

The way I see it, it's important to support local variables for the sake
of flexibility, but it's more of a niche use case, so it's OK if
specifying a local variable requires a bit more effort, compared to a
named function.

>> +    (let* ((x (gensym "x")) (arg x))
>> +      (dolist (fun (reverse funs))
>> +        (setq arg
>> +              (cond
>> +               ((symbolp fun) `(,fun ,arg))
>> +               ((vectorp fun) `(funcall ,(aref fun 0) ,arg))
>> +               (t `(funcall ,fun ,arg)))))
>> +      `(lambda (,x) ,arg)))))
>
> I wonder if we could/should be smart=E2=84=A2 about the func-arity of the
> rightmost function and adapt the wrapper lambda's arglist accordingly.

Perhaps, could be interesting if we could do it reliably.

> Finally, would you like to write an announcement and some tests as well?

Sure, if there no objections to this addition, I'll some tests and docs.


Eshel





Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 19 Feb 2025 18:40:12 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 13:40:12 2025
Received: from localhost ([127.0.0.1]:51450 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkozT-0006LW-Ol
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 13:40:12 -0500
Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]:48383)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <stefankangas@HIDDEN>)
 id 1tkozQ-0006Jy-4h
 for 76413 <at> debbugs.gnu.org; Wed, 19 Feb 2025 13:40:09 -0500
Received: by mail-ed1-x52b.google.com with SMTP id
 4fb4d7f45d1cf-5dedae49c63so106248a12.0
 for <76413 <at> debbugs.gnu.org>; Wed, 19 Feb 2025 10:40:08 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1739990402; x=1740595202; darn=debbugs.gnu.org;
 h=cc:to:subject:message-id:date:mime-version:references:in-reply-to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=EFu5tq9r+fobj2Lqvn9uglgEpcH48XXXJY8hnHOByw0=;
 b=NfsLg1ncaJAAjgBlNKAY87/6owWp5P/7uPXtuwGtreoUcYsIZWyiDoAk60/Dx3g0sd
 YpG3znTVb+eSns3X2uQpZPB+XjMNbDhQpG79r6q/DOl4pZaSPNejW2/miAttYYJPY1xn
 yJP+FwRRsXgUA6GzHNMcV3VOdJlSGy6PHmzmacgCp/IrZvLsFnHfkeUL2ZmcyOJaTaIQ
 mym23KIzroBuPHcyUvZQkFoQs7xx9ttG3cvIsxcMwjexovNvxK3vLXyvqs3c1lu9GopB
 q0nZcOuHe2EFyIBGX3NQ4IE5GuuRk9MB6tnTDE3FdqYaM8uzDfCOSzKE1mFMEu89u+MX
 +rCw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1739990402; x=1740595202;
 h=cc:to:subject:message-id:date:mime-version:references:in-reply-to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=EFu5tq9r+fobj2Lqvn9uglgEpcH48XXXJY8hnHOByw0=;
 b=IK/R8yt+hrr1V4fD+U7ljQXaRRdX0/9kSWz5YF6JMf2+hafyYqzT73+KA+wYfx3a30
 WmXQtVXy/xz3HEjQXkjXd/C8QJ9zq659FK8t/7fsVPCeBw2w+4fZGwh1e8OP9JivrhzQ
 TK30L9mjEYO1Cf68fXOnvdNPPm+lKNZ+baJbLdwpkq8fX4j9zkrLsWWbWHv8da+eKlTu
 afWO1i2NYfJYfQ2cbU2cHO6GkTVt4/amby5wZUJTx3tlXmLHsL/dutozpvWTovoCes5v
 WGhgXAD6rTM6vp9wgn0cYUEOcJvOGS4nhyM6u6sPwP46k+MBWSkmDIq8rN00jhh+zmuf
 Y3Lg==
X-Forwarded-Encrypted: i=1;
 AJvYcCW3pLMA8914UagUOBBhQ1sFgQn14NIQuy/OFo85lHo/rIuS5NCfWQCzPB9+22JcVsVCjVf7SA==@debbugs.gnu.org
X-Gm-Message-State: AOJu0YyZFUsYBALe33mLwovxRsKyNLCVXjUEdZC+fB+ooh2YUvQGU21D
 cID+6d4NIE7uqyALUUNlrXBxhTI5UtQ5xIl3uSu3bWdmkZd+wsT/278JZ3Xnj7n0ExDtU2gSFE2
 b0RRUbdvBykYGNcq5LxX8uNZu6CTfI0U/
X-Gm-Gg: ASbGncuCiNol010NGmr8UNId0YK7+grED9Xy4FRjGI/zRHT5MN20ewKzzGhfrAQETuZ
 CH5jWaAwhmyPfTsgjEiIDX1JH0sQbF7ft9YNigE0w2NUjGvLU5oe9yaMzrw47MvhHjRZh1Yknug
 ==
X-Google-Smtp-Source: AGHT+IGBmLFwrv4+9JmhnAGIvc3Fuuzez/PeJGf6FrtJw1GbcYB0o3Lf7xHmTYy8rjndkDJ27+IXPC7c/34BLjUZAK4=
X-Received: by 2002:a05:6402:2385:b0:5e0:51c0:701e with SMTP id
 4fb4d7f45d1cf-5e051c0718dmr9973825a12.32.1739990401948; Wed, 19 Feb 2025
 10:40:01 -0800 (PST)
Received: from 753933720722 named unknown by gmailapi.google.com with
 HTTPREST; Wed, 19 Feb 2025 10:40:01 -0800
From: Stefan Kangas <stefankangas@HIDDEN>
In-Reply-To: <m17c5mkpo4.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN>
MIME-Version: 1.0
Date: Wed, 19 Feb 2025 10:40:01 -0800
X-Gm-Features: AWEUYZn56e3LmmlcekyBdpCjBuccz2xIMa8V-BgO6C6mxWB4MSCmBwkNlYfiLss
Message-ID: <CADwFkmnOdHN0fjeAP-cnwOoa2vXqF_NeR=x5i1Dx6VL3hue1TA@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
To: Eshel Yaron <me@HIDDEN>, 76413 <at> debbugs.gnu.org
Content-Type: text/plain; charset="UTF-8"
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76413
Cc: Stefan Monnier <monnier@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <bug-gnu-emacs@HIDDEN> writes:

> Tags: patch
>
> This patch adds a new macro 'compf' that streamlines the common pattern
> of function composition.
>
> Namely, instead of (lambda (x) (foo (bar (baz x)))), with this macro we
> can write (compf foo bar baz), which expands to exactly the same form.
>
> This is similar in essence to the "compose" function proposed in
> https://lists.gnu.org/archive/html/emacs-devel/2021-02/msg01138.html
> with the main difference that compf is a macro, not a function, so it
> incurs no runtime performance penalty.
>
> There are many occurrences of function composition in the Emacs code
> base alone that can be simplified with this macro.  For example:
>
> - In lisp/calc/calc-mode.el:
>   (lambda (v) (symbol-value (car v)))       => (compf symbol-value car)
> - In lisp/dired-aux.el:
>   (lambda (dir) (not (file-remote-p dir)))  => (compf not file-remote-p)
> - In lisp/env.el:
>   (lambda (var) (getenv (upcase var)))      => (compf getenv upcase)
> - In lisp/mail/smtpmail.el:
>   (lambda (s) (intern (downcase s)))        => (compf intern downcase)
> - In lisp/net/tramp-container.el:
>   (lambda (line) (car (split-string line))) => (compf car split-line)
> - ...

Why not name it `compose` as in CL and ELisp?

How about a function with a compiler macro?




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 19 Feb 2025 18:36:38 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 13:36:38 2025
Received: from localhost ([127.0.0.1]:51417 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkow1-000612-Mt
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 13:36:38 -0500
Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]:44262)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <stefankangas@HIDDEN>)
 id 1tkovy-00060T-SS
 for 76413 <at> debbugs.gnu.org; Wed, 19 Feb 2025 13:36:35 -0500
Received: by mail-ed1-x536.google.com with SMTP id
 4fb4d7f45d1cf-5e095d47a25so93784a12.0
 for <76413 <at> debbugs.gnu.org>; Wed, 19 Feb 2025 10:36:34 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1739990188; x=1740594988; darn=debbugs.gnu.org;
 h=cc:to:subject:message-id:date:mime-version:references:in-reply-to
 :from:from:to:cc:subject:date:message-id:reply-to;
 bh=wnC3bQkXqlBdCBXZrnmkVeKA+dezL0cMq8CldQCIq+k=;
 b=ZWBggp60UFmKKSdo0maf83w9aRb+22D1CezovG3JFpyPU1Fn8b7hNfyHCwtLmX5/6Q
 aOgTaFm13oDMbZk4pdakwSeDOAAPYCi1UgGxaMTeEKTbzQHp62fxv+5X0mnLLWdFFzsK
 qkmQe+y2l11mg50VxA4YJAyTblvP9RO6iZ/rWbJln0Ru0ZKABGV8b9NMOWzdz03DB50p
 O3bASJBYvSbwUC2WI7/9cc3Kpvq5uOk6R3jvxSFkhG6/qrFQxLlDzfWHDx830z3PCUub
 qcEdQAiHawTDsHdrkAiyZyDlXXAvLwmfWhaKvbWoJG+uMbwlztVuQr7q8GMzjP2JCjAL
 8cIg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1739990188; x=1740594988;
 h=cc:to:subject:message-id:date:mime-version:references:in-reply-to
 :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=wnC3bQkXqlBdCBXZrnmkVeKA+dezL0cMq8CldQCIq+k=;
 b=UmFHnrhsR/jyBIGhW1C3/U6jfWC57O5uV9QtYf8qWPqa51e1erdhPDWrPATO0piFvK
 E+tWKYst8VFJVh84maat8masAlfBA4C1jvpVCe9C/hpaC0CynPC3aix5E3je5ZaMsqEM
 KG30aFXvKjz5iF8IBW9On28i9/u72sc0HsgAqgoLGgH14ijPYKuuRZ/oqSlu1361k7k3
 FeWXHoiJFzTfDbwvAuxg6zg7fI0/vTOFHrYcbLY2Yu/xYSk74nhol0bqXWD8VZ7vMVUT
 MPBr6ALundKtxZAQpZvfWLw0IFCUb3X8eNQ6k9rQndaUjrMXiis8sNFKl3j+TqYGfTqh
 +VNQ==
X-Forwarded-Encrypted: i=1;
 AJvYcCXO7G2PmAcgaBqEDLGLR8j5mS/bh9iu/7gqbLkIYieGGIYDAZHh3Fsd7sIVD7hEPh7KgN/dyg==@debbugs.gnu.org
X-Gm-Message-State: AOJu0YxXUKWjmv9urXEfmllmKwa3ZtMdKlCVjpv3ooqqSPUxXx2nwKrR
 Ixr48LMeDOPI2XzNhDRfCs6ILv/wUytySAlWn1rf49QyCYrUTdd5z/mlIuku9Hiv0X9A9wNg6uO
 tCcsHebmKaSXt8oyRDkuyl9D+/Nw=
X-Gm-Gg: ASbGnctdwk1XQDJKP02tCCOgldo+3XLzA/q8yKe+IDQMfugjZMtxRBZTK1fTBH4eJX0
 mWfzR27vMS6jSOTNYDyXSGmZwyJ2eCelnLIHlW2B0HV/hwUxG4Ul37FrNs4U+2atw/5cKiHJ2/Q
 ==
X-Google-Smtp-Source: AGHT+IGTimKn1MOUYxNQAP8spiDky5Z9ZR3lzMihdGflFDnT0WsMvjfhbIHDkuLf6MYH4ewfXOvHjQiAuj0+i6+fFCU=
X-Received: by 2002:a17:907:9494:b0:ab7:ec84:10af with SMTP id
 a640c23a62f3a-abb70b1d813mr1946130066b.16.1739990188318; Wed, 19 Feb 2025
 10:36:28 -0800 (PST)
Received: from 753933720722 named unknown by gmailapi.google.com with
 HTTPREST; Wed, 19 Feb 2025 10:36:27 -0800
From: Stefan Kangas <stefankangas@HIDDEN>
In-Reply-To: <m17c5mkpo4.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN>
MIME-Version: 1.0
Date: Wed, 19 Feb 2025 10:36:27 -0800
X-Gm-Features: AWEUYZlLbMhdxyEDmvvgFIlq-h0I5wMmLtga_Ced33vieiPArcZDFfZDVad9ssA
Message-ID: <CADwFkmmWkp_4EQgo5HnSw4hQNB8Ow8OPAF+iPQxUJL5WT1jxQw@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
To: Eshel Yaron <me@HIDDEN>, 76413 <at> debbugs.gnu.org
Content-Type: text/plain; charset="UTF-8"
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76413
Cc: Stefan Monnier <monnier@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <bug-gnu-emacs@HIDDEN> writes:

> This patch adds a new macro 'compf' that streamlines the common pattern
> of function composition.
>
> Namely, instead of (lambda (x) (foo (bar (baz x)))), with this macro we
> can write (compf foo bar baz), which expands to exactly the same form.
>
> This is similar in essence to the "compose" function proposed in
> https://lists.gnu.org/archive/html/emacs-devel/2021-02/msg01138.html
> with the main difference that compf is a macro, not a function, so it
> incurs no runtime performance penalty.
>
> There are many occurrences of function composition in the Emacs code
> base alone that can be simplified with this macro.  For example:
>
> - In lisp/calc/calc-mode.el:
>   (lambda (v) (symbol-value (car v)))       => (compf symbol-value car)
> - In lisp/dired-aux.el:
>   (lambda (dir) (not (file-remote-p dir)))  => (compf not file-remote-p)
> - In lisp/env.el:
>   (lambda (var) (getenv (upcase var)))      => (compf getenv upcase)
> - In lisp/mail/smtpmail.el:
>   (lambda (s) (intern (downcase s)))        => (compf intern downcase)
> - In lisp/net/tramp-container.el:
>   (lambda (line) (car (split-string line))) => (compf car split-line)
> - ...

I'm copying in Stefan Monnier.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 19 Feb 2025 17:13:21 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 12:13:21 2025
Received: from localhost ([127.0.0.1]:50472 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkndQ-0004sM-Gl
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 12:13:21 -0500
Received: from mta-13-4.privateemail.com ([198.54.127.109]:35869)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <basil@HIDDEN>)
 id 1tkndN-0004rU-Rq
 for 76413 <at> debbugs.gnu.org; Wed, 19 Feb 2025 12:13:18 -0500
Received: from mta-13.privateemail.com (localhost [127.0.0.1])
 by mta-13.privateemail.com (Postfix) with ESMTP id 4Yyjdd20xhz3hhVZ;
 Wed, 19 Feb 2025 12:13:09 -0500 (EST)
DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=contovou.net;
 s=default; t=1739985189;
 bh=u6tNctaN8O/p2a7MiayMJglJSZ/nI8dmPHOlA8kO2Po=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=ieaCrEyGoWvigLVP/7lknsLDP7Igxjcrew/LmjSxzZSTSN+xtaq55Ig2QrVxUUE/Y
 OIborSc9cpFnx5aBpciHXeOibv6If11b9lthcVtRry4EKXkuYQUx66XE/9x7zNrRvz
 zj/XiE0ksyWsl0PGKYb0CjQmlvqHBLZKD340iCJ3zeSqXiYeTGfhZDvVLwycemerpA
 wcWrnFVGfcZJ0BXqMViim7btK56Gw86gZuLRKVSg/lkDsieGwo0+BrUV056H+KYYXW
 reyXFqycil3AgPcXVPfeCa13oQrxm/OQIxdw81RdLb/ew9oxWo2DNxBFmjtTSJe15P
 Vq9j+sGujI/Mg==
Received: from localhost (unknown [79.127.182.166])
 by mta-13.privateemail.com (Postfix) with ESMTPA;
 Wed, 19 Feb 2025 12:13:06 -0500 (EST)
From: "Basil L. Contovounesios" <basil@HIDDEN>
To: Eshel Yaron <me@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
In-Reply-To: <m134gakm27.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN> <87mseic8an.fsf@HIDDEN>
 <m134gakm27.fsf@HIDDEN>
Date: Wed, 19 Feb 2025 18:13:03 +0100
Message-ID: <87v7t5j1sw.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Virus-Scanned: ClamAV using ClamSMTP
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76413
Cc: Michael Albinus <michael.albinus@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Eshel Yaron [2025-02-19 16:10 +0100] wrote:

> From e2834b162f3f1fa1c8dc8e057e3aa00950269d8e Mon Sep 17 00:00:00 2001
> From: Eshel Yaron <me@HIDDEN>
> Date: Wed, 19 Feb 2025 12:58:40 +0100
> Subject: [PATCH v2] New macro 'compf' for composing functions

Nice.

> +FUNS may contain symbols which refer to functions, such as `car' and
> +`cdr' in the example above, and it can also contain `lambda' functions
> +and other forms which evaluate to function values.

A (declare (debug ...)) property would be nice, particularly for
stepping through the latter types of argument.

>  To refer to a local
> +variable VAR that is bound to a function, wrap VAR in a vector, as in:
> +
> +  (let ((foo (lambda (...) ...)))
> +    (compf ignore [foo] always))

Is there precedent for this [syntax] somewhere?

I understand that function symbols may be needed more frequently than
variable symbols in arguments to compf, but did you consider
distinguishing between the two kinds of symbol by #'-quoting function
symbols, and leaving variables unquoted?
AFAIK this has more precedents in Elisp.

> +    (let* ((x (gensym "x")) (arg x))
> +      (dolist (fun (reverse funs))
> +        (setq arg
> +              (cond
> +               ((symbolp fun) `(,fun ,arg))
> +               ((vectorp fun) `(funcall ,(aref fun 0) ,arg))
> +               (t `(funcall ,fun ,arg)))))
> +      `(lambda (,x) ,arg)))))

I wonder if we could/should be smart=E2=84=A2 about the func-arity of the
rightmost function and adapt the wrapper lambda's arglist accordingly.

Finally, would you like to write an announcement and some tests as well?

Thanks,
--=20
Basil




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 19 Feb 2025 15:10:16 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 10:10:16 2025
Received: from localhost ([127.0.0.1]:48912 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkliJ-0007Ak-Dv
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 10:10:16 -0500
Received: from mail.eshelyaron.com ([107.175.124.16]:40752 helo=eshelyaron.com)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <me@HIDDEN>) id 1tkliF-00077x-CA
 for 76413 <at> debbugs.gnu.org; Wed, 19 Feb 2025 10:10:12 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1739977810;
 bh=S9fZhPj0TcSdxhEbgaCE2LlTrA1nCJtKsLnz9/w6KUQ=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=kWKStosD8ytKk+SSarHxyOfj8V51zPXqHUACK7itXn35IRQcGMfRc4foUoyjeHaOn
 r8SABBtHENwzNd50Wlq3n1CTje0groyAMO28ibvEEXHKGaMvOyd4ezHA7R8tipAE6n
 zttd2QlZM0wJuLuE4eBwFotGr8BRyxRiSM2E+n0nksmNso8KKrghcl215rR870QMsb
 WGwRiPD78km55JOWAOQ9TCm0hJ+JYuv93wGovtwx5kirAO7yJOFZ6/g7Q/uCKm6WDg
 T7b0DawcKfpS/kZIGHxbjRSBOz73w0xcpW+7lV85mo973mXK8pgdyia0ie5vQpBCLl
 WqCOYpkEZplFg==
From: Eshel Yaron <me@HIDDEN>
To: Michael Albinus <michael.albinus@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
In-Reply-To: <87mseic8an.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN> <87mseic8an.fsf@HIDDEN>
Date: Wed, 19 Feb 2025 16:10:08 +0100
Message-ID: <m134gakm27.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 76413
Cc: "Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text
 editors" <bug-gnu-emacs@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain

Hi Michael,

Michael Albinus <michael.albinus@HIDDEN> writes:

> Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
> editors" <bug-gnu-emacs@HIDDEN> writes:
>
> Hi Eshel,
>
>> This patch adds a new macro 'compf' that streamlines the common pattern
>> of function composition.
>
> I have no opinion about this change. However,
>
>> - In lisp/net/tramp-container.el:
>>   (lambda (line) (car (split-string line))) => (compf car split-line)
>
> Pls don't touch Tramp. It is backwards compatible down to Emacs 28.

Noted.  To be clear, I'm not proposing to change any existing code at
this point, only to add the new macro.

Anyway, here's an updated patch, which improves the macro's hygiene and
fixes a typo in the docstring:


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline;
 filename=v2-0001-New-macro-compf-for-composing-functions.patch

From e2834b162f3f1fa1c8dc8e057e3aa00950269d8e Mon Sep 17 00:00:00 2001
From: Eshel Yaron <me@HIDDEN>
Date: Wed, 19 Feb 2025 12:58:40 +0100
Subject: [PATCH v2] New macro 'compf' for composing functions

* lisp/subr.el (compf): New macro.  (Bug#76413)
---
 lisp/subr.el | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lisp/subr.el b/lisp/subr.el
index e9b49ae5376..d1615c9083f 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -7678,4 +7678,37 @@ internal--c-header-file-path
                   base
                 (append base (list (expand-file-name arch "/usr/include"))))))))))
 
+(defmacro compf (&rest funs)
+  "Expand to the function composition of FUNS, outermost function first.
+
+For example, (compf car cdr) expands to (lambda (x) (car (cdr x))),
+which does the same as `cadr'.
+
+FUNS may contain symbols which refer to functions, such as `car' and
+`cdr' in the example above, and it can also contain `lambda' functions
+and other forms which evaluate to function values.  To refer to a local
+variable VAR that is bound to a function, wrap VAR in a vector, as in:
+
+  (let ((foo (lambda (...) ...)))
+    (compf ignore [foo] always))
+
+If FUNS is empty, expand to `identity'."
+  (cond
+   ((null funs) '#'identity)
+   ((length= funs 1)
+    (let ((fun (car funs)))
+      (cond
+       ((symbolp fun) `#',fun)          ; Function name.
+       ((vectorp fun) (aref fun 0))     ; Local variable reference.
+       (t fun))))                       ; `lambda' and other forms.
+   (t
+    (let* ((x (gensym "x")) (arg x))
+      (dolist (fun (reverse funs))
+        (setq arg
+              (cond
+               ((symbolp fun) `(,fun ,arg))
+               ((vectorp fun) `(funcall ,(aref fun 0) ,arg))
+               (t `(funcall ,fun ,arg)))))
+      `(lambda (,x) ,arg)))))
+
 ;;; subr.el ends here
-- 
2.46.2


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 19 Feb 2025 15:10:26 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 10:10:26 2025
Received: from localhost ([127.0.0.1]:48920 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkliT-0007F3-Dz
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 10:10:26 -0500
Received: from lists.gnu.org ([2001:470:142::17]:37690)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <me@HIDDEN>) id 1tkliQ-0007Bb-AR
 for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 10:10:23 -0500
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 <me@HIDDEN>) id 1tkliH-00032x-Qz
 for bug-gnu-emacs@HIDDEN; Wed, 19 Feb 2025 10:10:14 -0500
Received: from mail.eshelyaron.com ([107.175.124.16] helo=eshelyaron.com)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <me@HIDDEN>) id 1tkliF-0003es-Ss
 for bug-gnu-emacs@HIDDEN; Wed, 19 Feb 2025 10:10:13 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1739977810;
 bh=S9fZhPj0TcSdxhEbgaCE2LlTrA1nCJtKsLnz9/w6KUQ=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=kWKStosD8ytKk+SSarHxyOfj8V51zPXqHUACK7itXn35IRQcGMfRc4foUoyjeHaOn
 r8SABBtHENwzNd50Wlq3n1CTje0groyAMO28ibvEEXHKGaMvOyd4ezHA7R8tipAE6n
 zttd2QlZM0wJuLuE4eBwFotGr8BRyxRiSM2E+n0nksmNso8KKrghcl215rR870QMsb
 WGwRiPD78km55JOWAOQ9TCm0hJ+JYuv93wGovtwx5kirAO7yJOFZ6/g7Q/uCKm6WDg
 T7b0DawcKfpS/kZIGHxbjRSBOz73w0xcpW+7lV85mo973mXK8pgdyia0ie5vQpBCLl
 WqCOYpkEZplFg==
From: Eshel Yaron <me@HIDDEN>
To: Michael Albinus <michael.albinus@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
In-Reply-To: <87mseic8an.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN> <87mseic8an.fsf@HIDDEN>
Date: Wed, 19 Feb 2025 16:10:08 +0100
Message-ID: <m134gakm27.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=107.175.124.16; envelope-from=me@HIDDEN;
 helo=eshelyaron.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,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,
 SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 0.9 (/)
X-Debbugs-Envelope-To: submit
Cc: "Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text
 editors" <bug-gnu-emacs@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.1 (/)

--=-=-=
Content-Type: text/plain

Hi Michael,

Michael Albinus <michael.albinus@HIDDEN> writes:

> Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
> editors" <bug-gnu-emacs@HIDDEN> writes:
>
> Hi Eshel,
>
>> This patch adds a new macro 'compf' that streamlines the common pattern
>> of function composition.
>
> I have no opinion about this change. However,
>
>> - In lisp/net/tramp-container.el:
>>   (lambda (line) (car (split-string line))) => (compf car split-line)
>
> Pls don't touch Tramp. It is backwards compatible down to Emacs 28.

Noted.  To be clear, I'm not proposing to change any existing code at
this point, only to add the new macro.

Anyway, here's an updated patch, which improves the macro's hygiene and
fixes a typo in the docstring:


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline;
 filename=v2-0001-New-macro-compf-for-composing-functions.patch

From e2834b162f3f1fa1c8dc8e057e3aa00950269d8e Mon Sep 17 00:00:00 2001
From: Eshel Yaron <me@HIDDEN>
Date: Wed, 19 Feb 2025 12:58:40 +0100
Subject: [PATCH v2] New macro 'compf' for composing functions

* lisp/subr.el (compf): New macro.  (Bug#76413)
---
 lisp/subr.el | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lisp/subr.el b/lisp/subr.el
index e9b49ae5376..d1615c9083f 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -7678,4 +7678,37 @@ internal--c-header-file-path
                   base
                 (append base (list (expand-file-name arch "/usr/include"))))))))))
 
+(defmacro compf (&rest funs)
+  "Expand to the function composition of FUNS, outermost function first.
+
+For example, (compf car cdr) expands to (lambda (x) (car (cdr x))),
+which does the same as `cadr'.
+
+FUNS may contain symbols which refer to functions, such as `car' and
+`cdr' in the example above, and it can also contain `lambda' functions
+and other forms which evaluate to function values.  To refer to a local
+variable VAR that is bound to a function, wrap VAR in a vector, as in:
+
+  (let ((foo (lambda (...) ...)))
+    (compf ignore [foo] always))
+
+If FUNS is empty, expand to `identity'."
+  (cond
+   ((null funs) '#'identity)
+   ((length= funs 1)
+    (let ((fun (car funs)))
+      (cond
+       ((symbolp fun) `#',fun)          ; Function name.
+       ((vectorp fun) (aref fun 0))     ; Local variable reference.
+       (t fun))))                       ; `lambda' and other forms.
+   (t
+    (let* ((x (gensym "x")) (arg x))
+      (dolist (fun (reverse funs))
+        (setq arg
+              (cond
+               ((symbolp fun) `(,fun ,arg))
+               ((vectorp fun) `(funcall ,(aref fun 0) ,arg))
+               (t `(funcall ,fun ,arg)))))
+      `(lambda (,x) ,arg)))))
+
 ;;; subr.el ends here
-- 
2.46.2


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 19 Feb 2025 14:35:09 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 09:35:09 2025
Received: from localhost ([127.0.0.1]:45748 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tklAK-0002Rf-DC
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 09:35:09 -0500
Received: from lists.gnu.org ([2001:470:142::17]:55212)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <michael.albinus@HIDDEN>)
 id 1tklAH-0002OO-RR
 for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 09:35:07 -0500
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 <michael.albinus@HIDDEN>)
 id 1tklA6-0008Kw-A5
 for bug-gnu-emacs@HIDDEN; Wed, 19 Feb 2025 09:34:54 -0500
Received: from mout.gmx.net ([212.227.15.15])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <michael.albinus@HIDDEN>)
 id 1tklA4-0003r9-Eo
 for bug-gnu-emacs@HIDDEN; Wed, 19 Feb 2025 09:34:54 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de;
 s=s31663417; t=1739975681; x=1740580481; i=michael.albinus@HIDDEN;
 bh=y7fh/qigsTl3YXq+v7uB/OQbwtUCHwmJ/LGQ/gE5fAk=;
 h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References:Date:
 Message-ID:MIME-Version:Content-Type:cc:content-transfer-encoding:
 content-type:date:from:message-id:mime-version:reply-to:subject:
 to;
 b=NfXahEzah3auyqNvWGDdgJGRe7UUue3oz1rpoVSyQ9eu/5nmUNJyom/yZJM6fIxq
 /HX6ELB2nJaJ/SWY2Zy/XC1vK9yTBlkb6P8eGu+jenzAHDpwso9rva3/eiBwewsCC
 niH7QD8codYtYZHobNdFBI4wyeGVYdn+TGSjoyHdVpTGEJMgnRX9SksbwNRHsdXlM
 JwNrYjG8QljTB+YpBG+nIl+vnmw44zCS1Iw/5PCXLzmnjmnNqrgJKj/BXRxUHJwEE
 Mr+zbJZKJUPirvKzMJv1XHDipkGzHYJKi18/gI4UpD0KuyEcZQNPoERPN0/AyMcML
 fF7GPFao0YVXVp3ECg==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Received: from gandalf.gmx.de ([185.89.38.155]) by mail.gmx.net (mrgmx005
 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MOREi-1tzXxf0ED0-00In9X; Wed, 19
 Feb 2025 15:34:41 +0100
From: Michael Albinus <michael.albinus@HIDDEN>
To: Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
 editors" <bug-gnu-emacs@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
In-Reply-To: <m17c5mkpo4.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN>
Date: Wed, 19 Feb 2025 15:34:40 +0100
Message-ID: <87mseic8an.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain
X-Provags-ID: V03:K1:SB/jg2vI/M+FLHTA/uiEQtggT0ilVYswi/7ElrFl/W4RcN9Ygm8
 PjpOZVdDSOtxEvfzuAim1/CGls81zmE99WRvK6UBfJH86R+i8ZLUQFeGTulPzpqsoqqPzSD
 ILn52TCwEtrFXhakVZVE8ec0aPpHmqOwWOEojHnqtjiPHKfq++JyHm/UlDjQaVcPdmhMqgu
 FzmERBtRkbFeV3COc6A0g==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:nmDLnp20MWI=;9yBEtNMZ05RT7dWLwZOHqrna3RP
 DIYtRghl5ADXV09K59LHT381r8oQaYZqZL9WiQoELd9VlghzV2MtmUWNu61bxtVDYrc7+Honq
 hO/TjbxKyr/RNokEV/7eqTeyVN1EtJ4nOjVOZ892ilzNyiBkAy/V5+3uqGp6c6pmOxq5MWzyj
 N5wRlFxfbqlP1fwxebbgMpNOiVBAvuockQCt5woSfhOVetABqUD167Ak1nNhQX+No4xB9mdNu
 4fJLIgBtrVqipJjTDopiOb7sVH7BgChl5Z0F9hIpFipNntJ8bRLSzSK6sZxSBXJOUfSk+c8V+
 JVMbW84KeBACUids9tPMqmSnEmQ32v1NRxVYMZU8ws3LDZq09UtX3TecGZeYZY78XBe7B1v5r
 E/rRwVoF334iTlunOknUOuanF+jxUOxxhuX+Y9p9I3swwxKmCpo9hBR5vYHvdt1P4o3w92yjo
 sJgelLDsNLvxjaUQXdP9NK4cWM80bi4TdMRThen3JHgbY8TW0Mj7XC0vOKiFH7yGE6DOZmxLq
 8cu7vK3QHHsqneMehWPtM6ygj8+of4ALWtK1YtNRC3nYcUF9zDvetSsgF0tpyUrmCLMISriDn
 yEMl8nesdsBfqmsR/W11uiIhI+8RRc8RATeODG8dzr4XTssjbSVhAAuxya4TC83E/i1YGV6N2
 ngZ3xfgnuBfILFHu9Ty60db0p/nHNUtNP7KvAoZsEPshlgzFhGgihMybejJjqc70Z1mMPRMxG
 cZQNuFCl5P3F61YuRMcUdxUuwIhSFvQfzZ5biIsnBm5EvgPoFgDu1YQRgUBgJGV7Q/+JGNy9h
 SVj+CgQkveDurbEPzdP5yRClSk8HDw30jvFbVFsmNuBBQbNtP1+Br6Vdpdnqf2qxXnU3zW03H
 rUSCAjXMOho+3gvVe/bmanOKDEGiBJIUPJNihn+t2AAAFPzsyYH3brIYp22JkwuQVjJHrGYi2
 Ju9ad3+ONc/kNalnAt/ON6j9g5Rjsh6hPrjBPwZFnQ+1zLtTZA2kTWSuI5atX9XYO2qQNxFhB
 aeFFzo3A/Wmr+dMq0abCAfK2NQ6hZPSyT8lvcsoWhLGOn6qgMZ8KwKBUP5XSZEROP7dJrrZXN
 3KoUjjTeaKp1yaq8RpsVrMTWTN4b5n3x/QnpzuYbBmREOYKBSV82Gp/AnrAS5zH006gpbEIhk
 RtF4jBkHnPj2hBqQurbJ7d37Lu7JBKIYHcEQDWSFjPcT+p0atXCE8nUb7QAz1knZICBgZ2soe
 +mwllea1NxTFbv0tcWephYisKpJACc3qDs5OhkxpVpcfVf7a4FNuDZBfGYqy2FElMEprY3xPv
 JZGjiHB1XajVzFdvmvO+3+r0HMN9Voy9fLoDbeVX/kEPdtvMFFNo93+ttdw2mz4fVDnX3LCis
 TxgqSSWzMpT60Gb/yEcMXXd/1EGWsBSgGc3ri3ywBybI2aJEuvKzA4KVr1
Received-SPF: pass client-ip=212.227.15.15;
 envelope-from=michael.albinus@HIDDEN; helo=mout.gmx.net
X-Spam_score_int: -27
X-Spam_score: -2.8
X-Spam_bar: --
X-Spam_report: (-2.8 / 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_LOW=-0.7, RCVD_IN_MSPIKE_H2=0.001,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: submit
Cc: Eshel Yaron <me@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <bug-gnu-emacs@HIDDEN> writes:

Hi Eshel,

> This patch adds a new macro 'compf' that streamlines the common pattern
> of function composition.

I have no opinion about this change. However,

> - In lisp/net/tramp-container.el:
>   (lambda (line) (car (split-string line))) => (compf car split-line)

Pls don't touch Tramp. It is backwards compatible down to Emacs 28.

> Best,
>
> Eshel

Best regards, Michael.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at 76413 <at> debbugs.gnu.org:


Received: (at 76413) by debbugs.gnu.org; 19 Feb 2025 14:34:53 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 09:34:53 2025
Received: from localhost ([127.0.0.1]:45742 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tklA4-0002Nz-OB
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 09:34:53 -0500
Received: from mout.gmx.net ([212.227.15.15]:56603)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <michael.albinus@HIDDEN>)
 id 1tklA1-0002NA-Jt
 for 76413 <at> debbugs.gnu.org; Wed, 19 Feb 2025 09:34:50 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de;
 s=s31663417; t=1739975681; x=1740580481; i=michael.albinus@HIDDEN;
 bh=y7fh/qigsTl3YXq+v7uB/OQbwtUCHwmJ/LGQ/gE5fAk=;
 h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References:Date:
 Message-ID:MIME-Version:Content-Type:cc:content-transfer-encoding:
 content-type:date:from:message-id:mime-version:reply-to:subject:
 to;
 b=NfXahEzah3auyqNvWGDdgJGRe7UUue3oz1rpoVSyQ9eu/5nmUNJyom/yZJM6fIxq
 /HX6ELB2nJaJ/SWY2Zy/XC1vK9yTBlkb6P8eGu+jenzAHDpwso9rva3/eiBwewsCC
 niH7QD8codYtYZHobNdFBI4wyeGVYdn+TGSjoyHdVpTGEJMgnRX9SksbwNRHsdXlM
 JwNrYjG8QljTB+YpBG+nIl+vnmw44zCS1Iw/5PCXLzmnjmnNqrgJKj/BXRxUHJwEE
 Mr+zbJZKJUPirvKzMJv1XHDipkGzHYJKi18/gI4UpD0KuyEcZQNPoERPN0/AyMcML
 fF7GPFao0YVXVp3ECg==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Received: from gandalf.gmx.de ([185.89.38.155]) by mail.gmx.net (mrgmx005
 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MOREi-1tzXxf0ED0-00In9X; Wed, 19
 Feb 2025 15:34:41 +0100
From: Michael Albinus <michael.albinus@HIDDEN>
To: Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
 editors" <bug-gnu-emacs@HIDDEN>
Subject: Re: bug#76413: [PATCH] New macro 'compf' for composing functions
In-Reply-To: <m17c5mkpo4.fsf@HIDDEN>
References: <m17c5mkpo4.fsf@HIDDEN>
Date: Wed, 19 Feb 2025 15:34:40 +0100
Message-ID: <87mseic8an.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain
X-Provags-ID: V03:K1:SB/jg2vI/M+FLHTA/uiEQtggT0ilVYswi/7ElrFl/W4RcN9Ygm8
 PjpOZVdDSOtxEvfzuAim1/CGls81zmE99WRvK6UBfJH86R+i8ZLUQFeGTulPzpqsoqqPzSD
 ILn52TCwEtrFXhakVZVE8ec0aPpHmqOwWOEojHnqtjiPHKfq++JyHm/UlDjQaVcPdmhMqgu
 FzmERBtRkbFeV3COc6A0g==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:nmDLnp20MWI=;9yBEtNMZ05RT7dWLwZOHqrna3RP
 DIYtRghl5ADXV09K59LHT381r8oQaYZqZL9WiQoELd9VlghzV2MtmUWNu61bxtVDYrc7+Honq
 hO/TjbxKyr/RNokEV/7eqTeyVN1EtJ4nOjVOZ892ilzNyiBkAy/V5+3uqGp6c6pmOxq5MWzyj
 N5wRlFxfbqlP1fwxebbgMpNOiVBAvuockQCt5woSfhOVetABqUD167Ak1nNhQX+No4xB9mdNu
 4fJLIgBtrVqipJjTDopiOb7sVH7BgChl5Z0F9hIpFipNntJ8bRLSzSK6sZxSBXJOUfSk+c8V+
 JVMbW84KeBACUids9tPMqmSnEmQ32v1NRxVYMZU8ws3LDZq09UtX3TecGZeYZY78XBe7B1v5r
 E/rRwVoF334iTlunOknUOuanF+jxUOxxhuX+Y9p9I3swwxKmCpo9hBR5vYHvdt1P4o3w92yjo
 sJgelLDsNLvxjaUQXdP9NK4cWM80bi4TdMRThen3JHgbY8TW0Mj7XC0vOKiFH7yGE6DOZmxLq
 8cu7vK3QHHsqneMehWPtM6ygj8+of4ALWtK1YtNRC3nYcUF9zDvetSsgF0tpyUrmCLMISriDn
 yEMl8nesdsBfqmsR/W11uiIhI+8RRc8RATeODG8dzr4XTssjbSVhAAuxya4TC83E/i1YGV6N2
 ngZ3xfgnuBfILFHu9Ty60db0p/nHNUtNP7KvAoZsEPshlgzFhGgihMybejJjqc70Z1mMPRMxG
 cZQNuFCl5P3F61YuRMcUdxUuwIhSFvQfzZ5biIsnBm5EvgPoFgDu1YQRgUBgJGV7Q/+JGNy9h
 SVj+CgQkveDurbEPzdP5yRClSk8HDw30jvFbVFsmNuBBQbNtP1+Br6Vdpdnqf2qxXnU3zW03H
 rUSCAjXMOho+3gvVe/bmanOKDEGiBJIUPJNihn+t2AAAFPzsyYH3brIYp22JkwuQVjJHrGYi2
 Ju9ad3+ONc/kNalnAt/ON6j9g5Rjsh6hPrjBPwZFnQ+1zLtTZA2kTWSuI5atX9XYO2qQNxFhB
 aeFFzo3A/Wmr+dMq0abCAfK2NQ6hZPSyT8lvcsoWhLGOn6qgMZ8KwKBUP5XSZEROP7dJrrZXN
 3KoUjjTeaKp1yaq8RpsVrMTWTN4b5n3x/QnpzuYbBmREOYKBSV82Gp/AnrAS5zH006gpbEIhk
 RtF4jBkHnPj2hBqQurbJ7d37Lu7JBKIYHcEQDWSFjPcT+p0atXCE8nUb7QAz1knZICBgZ2soe
 +mwllea1NxTFbv0tcWephYisKpJACc3qDs5OhkxpVpcfVf7a4FNuDZBfGYqy2FElMEprY3xPv
 JZGjiHB1XajVzFdvmvO+3+r0HMN9Voy9fLoDbeVX/kEPdtvMFFNo93+ttdw2mz4fVDnX3LCis
 TxgqSSWzMpT60Gb/yEcMXXd/1EGWsBSgGc3ri3ywBybI2aJEuvKzA4KVr1
X-Spam-Score: -0.7 (/)
X-Debbugs-Envelope-To: 76413
Cc: Eshel Yaron <me@HIDDEN>, 76413 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.7 (-)

Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <bug-gnu-emacs@HIDDEN> writes:

Hi Eshel,

> This patch adds a new macro 'compf' that streamlines the common pattern
> of function composition.

I have no opinion about this change. However,

> - In lisp/net/tramp-container.el:
>   (lambda (line) (car (split-string line))) => (compf car split-line)

Pls don't touch Tramp. It is backwards compatible down to Emacs 28.

> Best,
>
> Eshel

Best regards, Michael.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 19 Feb 2025 13:52:26 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 19 08:52:26 2025
Received: from localhost ([127.0.0.1]:45252 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1tkkV0-0005rA-34
	for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 08:52:26 -0500
Received: from lists.gnu.org ([2001:470:142::17]:42322)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <me@HIDDEN>) id 1tkkUy-0005qJ-CC
 for submit <at> debbugs.gnu.org; Wed, 19 Feb 2025 08:52:25 -0500
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 <me@HIDDEN>) id 1tkkUr-00086b-OA
 for bug-gnu-emacs@HIDDEN; Wed, 19 Feb 2025 08:52:17 -0500
Received: from mail.eshelyaron.com ([107.175.124.16] helo=eshelyaron.com)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <me@HIDDEN>) id 1tkkUq-0005Xc-3G
 for bug-gnu-emacs@HIDDEN; Wed, 19 Feb 2025 08:52:17 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1739973133;
 bh=Bddv+nKprXumeRaTOoaW2Ej4CyN5qEObkQdBEHQpBsY=;
 h=From:To:Subject:Date:From;
 b=p1JB2P3g/stUSCHi4COIh2Z6U/F8+tioiBhFdOYBzPn+t4yJldO3uR4PAAL8me/lP
 lRizDp4kc7UofIVqQmfmrzpAXN8TJIqceo2sXMM69nt2dywON20DOq7NgEbfZ2qCEZ
 Th8gnPlWVMtgp0sSfkjI4VbcwShOf9f4300s3/GcAXhvShp44YG8gVYQZ6AG0uwxQ2
 77pgW9muYyR7mVlVKH+jdDaI6DgcS6eo2+4xl5EsaGmf2Wm870yZSq4YPtlvzSZb9t
 g7YWBXnGL0hc2+JLv0fk+yy6cRmHRZl3epnjjzvGDTDwKENDA0HFzTPzvQXdSuCJ4G
 wV44TKt2P/1rQ==
From: Eshel Yaron <me@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: [PATCH] New macro 'compf' for composing functions
Date: Wed, 19 Feb 2025 14:52:11 +0100
Message-ID: <m17c5mkpo4.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=107.175.124.16; envelope-from=me@HIDDEN;
 helo=eshelyaron.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,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,
 SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 0.9 (/)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.1 (/)

--=-=-=
Content-Type: text/plain

Tags: patch

This patch adds a new macro 'compf' that streamlines the common pattern
of function composition.

Namely, instead of (lambda (x) (foo (bar (baz x)))), with this macro we
can write (compf foo bar baz), which expands to exactly the same form.

This is similar in essence to the "compose" function proposed in
https://lists.gnu.org/archive/html/emacs-devel/2021-02/msg01138.html
with the main difference that compf is a macro, not a function, so it
incurs no runtime performance penalty.

There are many occurrences of function composition in the Emacs code
base alone that can be simplified with this macro.  For example:

- In lisp/calc/calc-mode.el:
  (lambda (v) (symbol-value (car v)))       => (compf symbol-value car)
- In lisp/dired-aux.el:
  (lambda (dir) (not (file-remote-p dir)))  => (compf not file-remote-p)
- In lisp/env.el:
  (lambda (var) (getenv (upcase var)))      => (compf getenv upcase)
- In lisp/mail/smtpmail.el:
  (lambda (s) (intern (downcase s)))        => (compf intern downcase)
- In lisp/net/tramp-container.el:
  (lambda (line) (car (split-string line))) => (compf car split-line)
- ...


Best,

Eshel



--=-=-=
Content-Type: text/patch
Content-Disposition: attachment;
 filename=0001-New-macro-compf-for-composing-functions.patch

From fffadc1a6cb04b925d136859db66e4b706563651 Mon Sep 17 00:00:00 2001
From: Eshel Yaron <me@HIDDEN>
Date: Wed, 19 Feb 2025 12:58:40 +0100
Subject: [PATCH] New macro 'compf' for composing functions

* lisp/subr.el (compf): New macro.
---
 lisp/subr.el | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lisp/subr.el b/lisp/subr.el
index e9b49ae5376..a76dad8ea82 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -7678,4 +7678,37 @@ internal--c-header-file-path
                   base
                 (append base (list (expand-file-name arch "/usr/include"))))))))))
 
+(defmacro compf (&rest funs)
+  "Expand to the function composition of FUNS, outermost function first.
+
+For example, (compf car cdr) expands to (lambda (x) (car (cdr x))),
+which does the same as `cadr'.
+
+FUNS may contain symbols which refer to functions, such as `car' and
+`cdr' in the example above, and it can also contain `lambda' functions
+and other forms which evaluate to function values.  To refer to a local
+variable VAR that is bound to a function, wrap VAR in a vector, as in:
+
+  (let ((foo (lambda (...) ...)
+    (compf ignore [foo] always))
+
+If FUNS is empty, expand to `identity'."
+  (cond
+   ((null funs) '#'identity)
+   ((length= funs 1)
+    (let ((fun (car funs)))
+      (cond
+       ((symbolp fun) `#',fun)          ; Function name.
+       ((vectorp fun) (aref fun 0))     ; Local variable reference.
+       (t fun))))                       ; `lambda' and other forms.
+   (t
+    (let ((arg 'x))
+      (dolist (fun (reverse funs))
+        (setq arg
+              (cond
+               ((symbolp fun) `(,fun ,arg))
+               ((vectorp fun) `(funcall ,(aref fun 0) ,arg))
+               (t `(funcall ,fun ,arg)))))
+      `(lambda (x) ,arg)))))
+
 ;;; subr.el ends here
-- 
2.46.2


--=-=-=--




Acknowledgement sent to Eshel Yaron <me@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#76413; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Sun, 23 Feb 2025 00:15:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.