Use apply when you don't know what the individual arguments are, or how many there are. You can use funcall (or apply) when you do know that.
For example, suppose your list of args to apply the function to is the value of variable foo. Then you would use (apply 'some-fun foo).
If you know that you're going to apply the function to args 3, toto, and 4, in ...
apply-partially was most useful back when Emacs did not have lexical binding, since it let you build "closures". But for your example, you don't even need a closure:
(defalias 'fix-a-c-in-foo (lambda (b) (foo "a_value" b "c_value")))
for a more general case, OTOH you do need closures, so you'll want to add -*- lexical-binding:t -*- to the first line of ...
(advice-add #'undo-tree :filter-return #'undo-tree-advice-history-save-file-name)
This advises the function undo-tree, whereas the defadvice form advises undo-tree-make-history-save-file-name. The equivalent advice-add would be
(advice-add 'undo-tree-make-history-save-file-name :filter-return #'undo-tree-advice-history-save-file-name)
You have everything you need in the emacs-lisp core language with fset, lambda expressions and backticks.
The following source code is an extension of your second example.
The comma expression allows you to easily substitute something in a back-tick quoted lambda expression
and you can assign that expression to a symbol's function-cell with fset.
(defun foo ...
You need to apply the function to the list of arguments:
(f-write-bytes (apply 'unibyte-string random-data) "file.dat")
(apply '+ '(1 2 3)) is equivalent to (+ 1 2 3). See the section about calling functions in the elisp manual for more details.
Something like this, the new function to learn is apply:
(defun get-quotes (x &rest y)
(cons x y))
"Smith, \"Neural Pathways in Cat Brains\""
(dolist (thisarticle articles)
(if (stringp thisarticle)
This is because and is not a function (it is a special form).
Note that C-h f and tells you "and is a special form in `C source code'."
apply must be used with a function.
The manual says:
‘apply’ returns the result of calling FUNCTION. As with ‘funcall’,
FUNCTION must either be a Lisp function or a primitive function;
special forms and macros do not ...
Not exactly. Function buffer-substring requires two arguments. Function - will accept a single argument, but in that case it just returns the negative of that numeric argument. What you want to do is, in effect, apply such functions to a list of arguments. You can use higher-order function apply to do that.
What you can do, if you want, is have a macro or ...
((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.
Your C-h f is not the standard C-h f (M-x describe-function), describe-function doesn't provide "References ..." information at all. To find the source of apply, use C-h f apply, it will say
apply is a built-in function in `src/eval.c'.
src/eval.c will be clickable, then click it to go to the source. If you didn't install Emacs from the source code by ...
I figured it out. As noted in my comment above, my code:
(defun add2 (apply-partially 'add-numbers 2))
can't work because defun is a special form and (apply-partially 'add-numbers 2) is seen as the argument list to the special form, i.e. apply-partially never gets evaluated.
This leaves us with the challenge of saving the call (apply-partially 'add-...