I'm confused about return value of apply-partially. Documentation states that it returns a function, and source of the function shows that it actually retruns a lambda. But I can't invoke the return value directly, only via funcall. Creating lambda directly at the call place allows me to invoke it directly.

Here are three examples of what am I talking about:

((apply-partially 'string-prefix-p ".") ".emacs")
;; => (invalid-function (apply-partially 'string-prefix-p "."))
((lambda (x) (string-prefix-p "." x)) ".emacs")
;; => t
(funcall (apply-partially 'string-prefix-p ".") ".emacs")
;; => t

Why is it working in such way?


((lambda ...) ...) is a special case, and IIRC the only such special case.

Lots of elisp functions return functions, and apply-partially is no different to any of the others in this regard.

source of the function shows that it actually returns a lambda

Most functions are (ultimately) lambdas. See C-hig (elisp)What Is a Function for details.

C-hig (elisp)Function Indirection might also be of interest.

  • I've rejected the following as an edit to the answer because it's not what the question was about; but in case anyone else arrives at this Q&A with this same requirement, another user wished to point out that you can define a function like so (defalias 'dot-prefix-p (apply-partially 'string-prefix-p ".")) which can be called like this (dot-prefix-p ".emacs").
    – phils
    Sep 17 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.