Hot answers tagged

5

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


5

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


4

You are looking for the function apply. Use it as follows: (setq some-var '("-l" "-a" "-t" "-r")) (apply #'start-process "ls" "*temp*" "ls" some-var)


3

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


3

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


2

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


2

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.


2

Something like this, the new function to learn is apply: (defun get-quotes (x &rest y) (cons x y)) (let ((articles '("/home/matt/art/mice.pdf" ("/home/matt/art/cats.pdf" "Smith, \"Neural Pathways in Cat Brains\"" 3)))) (dolist (thisarticle articles) (if (stringp thisarticle) (get-quotes thisarticle) ...


2

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


1

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


Only top voted, non community-wiki answers of a minimum length are eligible