emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Compiler macro for apply-partially


From: Stefan Monnier
Subject: Re: Compiler macro for apply-partially
Date: Tue, 24 Aug 2021 18:32:55 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>   (defun apply-partially* (fun &rest args)
>     "Return a function that is a partial application of FUN to ARGS.
>   ARGS is a list of the first N arguments to pass to FUN.  The
>   result is a new function which does the same as FUN, except that
>   the first N arguments are fixed at the values with which this
>   function was called."
>     (declare (compiler-macro (lambda (exp)
>                                `(lambda (&rest args2)
>                                   (apply ,fun ,@args args2)))))
>     (lambda (&rest args2)
>       (apply fun (append args args2))))

Looks OK to me.

FWIW, I never added such a compiler macro for the following reason: it's
almost always preferable to use an explicit `lambda` where you can
specify how many args are expected and hence avoid the `&rest` and the
`apply`, leading to significantly more efficient code.

IOW if you care about performance use `lambda` rather than
`apply-partially`.  `apply-partially` was a hack to make it easy to
build closures back when we didn't have `lexical-binding`.
Adding a compiler macro to it makes it a bit less slow, but the real
gain can only come after the authors provide a more complete calling
convention as is the case when using `lambda`.

>   (defun apply-partially** (fun &rest args)

This is an example of the more efficient code I'm talking about and it
requires the programmer to tell us that the returned function will be
called only with exactly 1 argument, so it can't be just a compiler-macro.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]