8

I have a function foo-function that takes the variable x and performs a specific action. The variable x comes before the definition of foo-function:

(defvar x "value1")

(defun foo-function ()
  (blabalabla x))

I would like to define a new function foo-function2 which takes several variables value1, value2, value3, .... of x (the number of variables is indeterminate) and runs the function foo-function for these variables, i.e., the following action:

(foo-function2 "value1" "value2" "value3", ...)

It should result in the execution of foo-function for the values value1, value2, value3, .... of x.

How do I define this function?

Dan
  • 32,584
  • 6
  • 98
  • 168
Name
  • 7,689
  • 4
  • 38
  • 84

1 Answers1

13

Looks like you want mapcar. From the docstring:

(mapcar FUNCTION SEQUENCE)

Apply FUNCTION to each element of SEQUENCE, and make a list of the results. The result is a list just as long as SEQUENCE. SEQUENCE may be a list, a vector, a bool-vector, or a string.

(defun identity-fnx (x)
  x)

(mapcar #'identity-fnx '(1 2 3)) ; => (1 2 3)

(defvar x '(a b c d))
(mapcar #'identity-fnx x)        ; => (a b c d)
Dan
  • 32,584
  • 6
  • 98
  • 168
  • 4
    n.b. Use `mapc` instead to avoid accumulating the return value sequence, if you don't want that. – phils Jan 07 '15 at 21:13
  • @phils: right on; I wasn't sure if he wanted to accumulate or not. And another n.b. on your n.b. is to use a more general option like `cl-map` (compare [How can I map over a vector and get a vector?](http://emacs.stackexchange.com/questions/2961/how-can-i-map-over-a-vector-and-get-a-vector)). – Dan Jan 07 '15 at 21:18
  • 1
    Little nitpick: `identity` is built-in :) –  Jan 08 '15 at 10:16