Subject: bug#76413: [PATCH] New macro 'compf' for composing functions

From: Stefan Kangas <[email-masked]>
Date: Wed, 19 Feb 2025 10:40:01 -0800

Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <[email-masked]> 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?