Search code examples
elisp

Mapping a function with multiple arguments to list when some are constant in elisp


What is the proper way of applying a function that takes multiple arguments, some of which are constant, to a list in elisp? I'm used to R, where you can pass mapping functions named arguments (in addition to the function and list) that get passed along as arguments to the function being applied. Is there something like this in elisp?

For example, I'm trying to add a number of variables using add-to-list. Do I need to create an anonymous function like this or is there another way?

(setq some-alist (list))
(mapc (lambda (x)
        (add-to-list 'some-alist x))
      '(("style-a")
        ("style-b")))

Solution

  • Using a lambda list is the usual style in Emacs Lisp. If you dislike lambda lists, you can use the apply-partially combinator:

    (mapc (apply-partially #'add-to-list 'some-alist)
          '(("style-a" "style-b")))