Call several functions with the same value

Basilio Hinojosa del Castillo

I have various functions and I want to call each function with the same value. For instance, I have these functions:

(defun OP1 (arg) ( + 1 arg) )
(defun OP2 (arg) ( + 2 arg) )
(defun OP3 (arg) ( + 3 arg) )

And a list containing the name of each function:

(defconstant *OPERATORS* '(OP1 OP2 OP3))

So far, I'm trying:

(defun TEST (argument) (dolist (n *OPERATORS*) (n argument) ) )

I've tried using eval, mapcar, and apply, but these haven't worked.

This is just a simplified example; the program that I'm writing has eight functions that are needed to expand nodes in a search tree, but for the moment, this example should suffice.

Joshua Taylor

Other answers have provided some idiomatic solutions with mapcar. One pointed out that you might want a list of functions (which *operators* isn't) instead of a list of symbols (which *operators* is), but it's OK in Common Lisp to funcall a symbol. It's probably more common to use some kind of mapping construction (e.g., mapcar) for this, but since you've provided code using dolist, I think it's worth looking at how you can do this iteratively, too. Let's cover the (probably more idiomatic) solution with mapping first, though.

Mapping

You have a fixed argument, argument, and you want to be able to take a function function and call it with that `argument. We can abstract this as a function:

(lambda (function)
  (funcall function argument))

Now, we want to call this function with each of the operations that you've defined. This is simple to do with mapcar:

(defun test (argument)
  (mapcar (lambda (function)
            (funcall function argument))
          *operators*))

Instead of operators, you could also write '(op1 op2 op3) or (list 'op1 'op2 'op3), which are lists of symbols, or (list #'op1 #'op2 #'op3) which is a list of functions. All of these work because funcall takes a function designator as its first argument, and a function designator is

an object that denotes a function and that is one of: a symbol (denoting the function named by that symbol in the global environment), or a function (denoting itself).

Iteratively

You can do this using dolist. The [documentation for actually shows that dolist has a few more tricks up its sleeve. The full syntax is from the documentation

dolist (var list-form [result-form]) declaration* {tag | statement}*

We don't need to worry about declarations here, and we won't be using any tags, but notice that optional result-form. You can specify a form to produce the value that dolist returns; you don't have to accept its default nil. The common idiom for collecting values into a list in an iterative loop is to push each value into a new list, and then return the reverse of that list. Since the new list doesn't share structure with anything else, we usually reverse it destructively using nreverse. Your loop would become

(defun test (argument)
  (let ((results '()))
    (dolist (op *operators* (nreverse results))
      (push (funcall op argument) results))))

Stylistically, I don't like that let that just introduces a single value, and would probably use an &aux variable in the function (but this is a matter of taste, not correctness):

(defun test (argument &aux (results '()))
  (dolist (op *operators* (nreverse results))
    (push (funcall op argument) results)))

You could also conveniently use loop for this:

(defun test2 (argument)
  (loop for op in *operators*
     collect (funcall op argument)))

You can also do somewhat succinctly, but perhaps less readably, using do:

(defun test3a (argument)
  (do ((results '() (list* (funcall (first operators) argument) results))
       (operators *operators* (rest operators)))
      ((endp operators) (nreverse results))))

This says that on the first iteration, results and operators are initialized with '() and *operators*, respectively. The loop terminates when operators is the empty list, and whenever it terminates, the return value is (nreverse results). On successive iterations, results is a assigned new value, (list* (funcall (first operators) argument) results), which is just like pushing the next value onto results, and operators is updated to (rest operators).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

call several functions within same class in hooks

From Dev

Test several functions with the same list of value with quickCheck

From Dev

How to call the same functions to several buttons c#

From Dev

Use the same template for several functions

From Dev

Invoke several functions to the same variable in JavaScript

From Dev

Populate same value to several rows in Pandas Dataframe

From Dev

Numerical Value That Appears Several Times in Same Column

From Dev

Condense several conditional statement comparing the same value

From Dev

Functions keeps returning the same value

From Java

Is there a short-hand to call several functions in parallel in Java?

From Dev

Is there a short-hand to call several functions in parallel in Java?

From Dev

How to call two different functions for a same condition?

From Dev

Call multiple IO functions on the same input

From Dev

Several Threads racing to set the same data to the same value

From Dev

Is it safe to call the same method several times asynchronously in C# 5

From Java

Same name functions in same class, elegant way to determine which to call?

From Dev

Same name functions in same class, elegant way to determine which to call?

From Dev

Same name functions in same class - is there an elegant way to determine which to call?

From Dev

Forwarding the same value to two or more functions

From Dev

HashMap contains several different keys having the same value?

From Dev

Shorter way of checking several arguments to same value in a if-statement?

From Dev

selecting average value from group of same with max on another for several columns

From Dev

Best way to check if several inputs have the same value

From Dev

Plot several Y-values at the same x-value in .NET

From Dev

call different method with same string in different value

From Dev

Restrict MATLAB to call functions from same folder as running file

From Dev

Can I call the same function inside of two other functions?

From Dev

How to call 2 Functions at the same time using Twig AJAX Form?

From Dev

How is the C++ compiler's performance affected by defining several functions with the same name but different types?

Related Related

  1. 1

    call several functions within same class in hooks

  2. 2

    Test several functions with the same list of value with quickCheck

  3. 3

    How to call the same functions to several buttons c#

  4. 4

    Use the same template for several functions

  5. 5

    Invoke several functions to the same variable in JavaScript

  6. 6

    Populate same value to several rows in Pandas Dataframe

  7. 7

    Numerical Value That Appears Several Times in Same Column

  8. 8

    Condense several conditional statement comparing the same value

  9. 9

    Functions keeps returning the same value

  10. 10

    Is there a short-hand to call several functions in parallel in Java?

  11. 11

    Is there a short-hand to call several functions in parallel in Java?

  12. 12

    How to call two different functions for a same condition?

  13. 13

    Call multiple IO functions on the same input

  14. 14

    Several Threads racing to set the same data to the same value

  15. 15

    Is it safe to call the same method several times asynchronously in C# 5

  16. 16

    Same name functions in same class, elegant way to determine which to call?

  17. 17

    Same name functions in same class, elegant way to determine which to call?

  18. 18

    Same name functions in same class - is there an elegant way to determine which to call?

  19. 19

    Forwarding the same value to two or more functions

  20. 20

    HashMap contains several different keys having the same value?

  21. 21

    Shorter way of checking several arguments to same value in a if-statement?

  22. 22

    selecting average value from group of same with max on another for several columns

  23. 23

    Best way to check if several inputs have the same value

  24. 24

    Plot several Y-values at the same x-value in .NET

  25. 25

    call different method with same string in different value

  26. 26

    Restrict MATLAB to call functions from same folder as running file

  27. 27

    Can I call the same function inside of two other functions?

  28. 28

    How to call 2 Functions at the same time using Twig AJAX Form?

  29. 29

    How is the C++ compiler's performance affected by defining several functions with the same name but different types?

HotTag

Archive