Emacs Lisp displays a list of function names and calls them all with the same argument

I am having trouble understanding the approach I need to take to collapse the list of functions and call them all with a specific argument.

This is what I was supposed to work for. I tried various options using eval , etc. Any pointers?

 (mapcar (lambda (fn) (fn 'utf-8)) (list #'set-terminal-coding-system #'set-keyboard-coding-system #'prefer-coding-system)) 

When I run this, I just get "Character Function Definition: void: fn".

EDIT | So this works, but it seems odd to use apply when the above example passes functions with the syntax #'function-name .

 (mapcar (lambda (fn) (apply fn '(utf-8))) '(set-terminal-coding-system set-keyboard-coding-system prefer-coding-system)) 
+4
source share
2 answers

In Emacs lisp, characters have separate slots of values ​​and functions 1 .

Function arguments are passed as the value of the argument character, but when you evaluate (fn 'utf-8) , you use the fn character function slot that will not contain what you want (or nothing at all in this case, so the error is "Defining character function invalid: fn ").

To call the function contained in a variable, you must therefore have funcall or apply (or similar).

See also:

1 i.e. this is the so-called "lisp -2", unlike "lisp -1", where both have the same namespace.

+7
source

For side effects of dash the list library has a -each function and its anaphoric copy of --each :

 (-each '(fn1 fn2 fn3) (lambda (x) (funcall x YOUR-ARGUMENT))) (--each '(fn1 fn2 fn3) (funcall it YOUR-ARGUMENT)) 

If you want to collect the results of these functions in a list, use -map instead of arguments:

 (-map (lambda (x) (funcall x YOUR-ARGUMENT)) '(fn1 fn2 fn3)) (--map (funcall it YOUR-ARGUMENT) '(fn1 fn2 fn3)) 
+1
source

Source: https://habr.com/ru/post/1481605/


All Articles