Combine one-arg functions into a multi-arg one in Clojure
-
03-06-2021 - |
質問
I wonder if there is an idiom in Clojure for combining several one argument functions into a new function accepting a vector. The new function should apply the first function to the first argument and so on.
I think this is useful in a number of situations. For example, you might want to transform x, y and z coordinates of a point using simpler unidimensional functions.
I would implement by myself as follows but I'm afraid it should already exists something similar in the standard API.
(defn vector-comp [& fns]
(fn [avect]
(vec (map #(%1 %2) fns avect))))
他のヒント
Other way to write this:
(defn vector-comp [& fns]
#(map apply fns % (repeat nil)))
I am 99% sure there is no ready-made solution in the standard libraries (juxt
comes the closest that I know of)
所属していません StackOverflow