Reputation: 1158
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))))
Upvotes: 2
Views: 137
Reputation: 91857
https://github.com/flatland/useful/blob/develop/src/useful/fn.clj#L90
Upvotes: 1
Reputation: 45071
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)
Upvotes: 1