unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Adam Porter <adam@alphapapa.net>, emacs-devel@gnu.org
Subject: Re: Compiler macro for apply-partially
Date: Wed, 25 Aug 2021 10:45:15 -0700	[thread overview]
Message-ID: <87k0k9beqc.fsf@ericabrahamsen.net> (raw)
In-Reply-To: <jwvv93ueb7p.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Tue, 24 Aug 2021 18:32:55 -0400")

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	other threads:[~2021-08-25 17:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-24  6:37 Compiler macro for apply-partially Adam Porter
2021-08-24  6:51 ` Adam Porter
2021-08-24  7:22   ` Andreas Schwab
2021-08-24  7:38     ` Adam Porter
2021-08-24 22:32 ` Stefan Monnier
2021-08-25 17:45   ` Eric Abrahamsen [this message]
2021-08-25 18:27     ` Stefan Monnier
2021-08-25 18:44       ` Eric Abrahamsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k0k9beqc.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=adam@alphapapa.net \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).