reddit / emacs

Help with Elisp macros

Hastrack · 2022-03-18 18:43
I would like to make a function that allows multiple values and each value is executed by the same function but I don't know how exactly I should do that. Like this, visually: (made-function "thing" "thing 2" "thing 3" "thing 4") Behaviour expected: (made-function (nice-function "thing") (nice-function "thing 2") (nice-function "thing 3") (nice-function "thing 4"))
crawsphi · 2022-03-18 19:07
You probably don't need this to be a macro. A function can call another function on each of its arguments: (defun made-function (&rest args) (dolist (arg args) (nice-function arg))) But for completeness: (defmacro made-function (&rest args) `(progn ,@(mapcar (lambda (arg) `(nice-function ,arg)) args)))
Hastrack · 2022-03-18 19:38
Thanks! Those worked as I hoped.
Hastrack · 2022-03-18 20:21
How should I proceed when I have two values? (made-function ("arg1" "arg2") ("thing 1" "thing2")) And I want the function to run like this: (nice-function arg1 arg2) (nice-function thing1 thing2)
crawsphi · 2022-03-18 22:41
For the function, use `apply`. For the macro, another splice: (defun made-function (&rest args) (dolist (arglist args) (apply #'nice-function arglist))) (defmacro made-macro (&rest args) `(progn ,@(mapcar (lambda (arglist) `(nice-function ,@arglist)) args))) Note that this is going to expose one of the differences between functions and macros. A macro's arguments are passed unevaluated. The function's arguments need to be explicitly quoted: (made-function '("spam" "eggs") '("ham" "pancakes")) (made-macro ("spam" "eggs") ("ham" "pancakes"))
shsms · 2022-03-19 15:03
When making a function, there's also the option to use `pcase-dolist`: (defun made-function (&rest args) (pcase-dolist (`(,a1 ,a2) args) (nice-function a1 a2))) (made-function '("arg1" "arg2") '("thing 1" "thing2"))