I came across a thing I needed today where I wanted to progressively associate entries in a map, each based on the results of the previous one(s). Here’s what I did:
(defn -Y [v k f] (assoc v k (f v)))
(defn build-map [a-map]
(-> a-map
(-Y :x #(reduce + (:values %) ) )
(-Y :y #(/ (:x %) 100) )
(-Y :z #(* (:y %) 10000000) )
)
)
(build-map {:values (range 8)})
I’d welcome your thoughts on a) is it a good thing ? b) is there an existing way to do it that I haven’t seen ? (I don’t know the API well)
So you got a map that goes through some transformations and you end up with a final map. Your implementation looks fine to me except that
-Yisn’t doing much of thing and it may not be required as a separate function.You can do all this using just
reducefunction, something like: