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: Eric Abrahamsen
Subject: Re: Compiler macro for apply-partially
Date: Wed, 25 Aug 2021 10:45:15 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>   (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.

For us slower kids, this explicit approach might look like:

(cl-flet ((curried (arg3)
                   (function-to-apply-partially arg1 arg2 arg3)))
  (curried "arg3"))

Either that or just plain `let' a lambda, and then `funcall' it?



reply via email to

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