diff lisp/cl-macs.el @ 5550:b908c7265a2b

Add the #'apply-partially API, as used by GNU. lisp/ChangeLog addition: 2011-08-12 Aidan Kehoe <kehoea@parhasard.net> * cl-macs.el: * cl-macs.el (apply-partially): New compiler macro. * subr.el: * subr.el (apply-partially): New. Sync this function's API and docstring from GNU. The implementation is mine and trivial; the compiler macro in cl-macs.el ensures that partially-applied functions in compiled code are also compiled. tests/ChangeLog addition: 2011-08-12 Aidan Kehoe <kehoea@parhasard.net> * automated/lisp-tests.el: Trivial tests of #'apply-partially, just added to subr.el.
author Aidan Kehoe <kehoea@parhasard.net>
date Fri, 12 Aug 2011 16:02:30 +0100
parents 2a6a8da4dd7c
children 62edcc6a11ec
line wrap: on
line diff
--- a/lisp/cl-macs.el	Wed Aug 10 16:50:37 2011 +0100
+++ b/lisp/cl-macs.el	Fri Aug 12 16:02:30 2011 +0100
@@ -3510,6 +3510,24 @@
 	    (list 'let (list (list temp val)) (subst temp val res)))))
     form))
 
+(define-compiler-macro apply-partially (&whole form &rest args)
+  "Generate a #'make-byte-code call for #'apply-partially, if appropriate."
+  (if (< (length args) 1)
+      form
+    (if (cl-const-exprs-p args)
+        `#'(lambda (&rest args) (apply ,@args args))
+      (let* ((placeholders (mapcar 'quote-maybe (mapcar 'gensym args)))
+             (compiled (byte-compile-sexp
+                        `#'(lambda (&rest args) (apply ,@placeholders args)))))
+        `(make-byte-code
+          ',(compiled-function-arglist compiled)
+          ,(compiled-function-instructions compiled)
+          (vector ,@(sublis (pairlis placeholders args)
+                            (mapcar 'quote-maybe
+                                    (compiled-function-constants compiled))
+                            :test 'equal))
+          ,(compiled-function-stack-depth compiled))))))
+
 (define-compiler-macro delete-dups (list)
   `(delete-duplicates (the list ,list) :test #'equal :from-end t))