Search code examples
clojurehigher-order-functions

How to apply a sequence of functions on a sequence of variables in Clojure?


I have a function which takes a sequence of function and a sequence of arguments. This should return a vector with the results of each function applied to the sequence of the arguments.

((solution + max min) 2 3 5 1 6 4) ;;--> [21 6 1]

Im trying to solve it with reduce but i dont know how to apply all functions it works only for the first function:

(defn solution
  [& args]
 (fn [& args2]
 (reduce (first args) [] args2)))

Solution

  • Use juxt:

    ((juxt + max min) 2 3 5 1 6 4)
    => [21 6 1]
    

    Or define function solution:

    (defn solution
      [& args]
      (fn [& args2]
        (apply (apply juxt args) args2)))
    
    ((solution + max min) 2 3 5 1 6 4)
    => [21 6 1]