5

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?

9

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)
  • 4
    n.b. Use mapc instead to avoid accumulating the return value sequence, if you don't want that. – phils Jan 7 '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?). – Dan Jan 7 '15 at 21:18
  • 1
    Little nitpick: identity is built-in :) – lunaryorn Jan 8 '15 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.