Convert list back to map card

I have a function that returns a structure that has two fields :: key: event. Field: event is a map (decomposed Java object obtained from the cache). In REPL, I see the return value as a map.

Then I apply, (def events (map #(make-event %) (keys events-cache))) , using the make-event function for each key from the cache, and I want the back map to contain every event map entered by the key.

What I get is this, but inside the list. Therefore, the call of any card functions, search, etc. Throws an error, clojure.lang.LazySeq cannot be attributed to clojure.lang.IFn.

I am sure that I think everything is wrong about this, but is there a way to get a card from the list of cards?

+4
source share
4 answers

Perhaps this is what you want?

 (into {} (for [k (keys events-cache)] [k (make-event k)])) 
+7
source

Your terms are vague, and the error message you post suggests that the problem is different from the question you ask. You will most likely receive additional help if you post some code, and especially real glass.

But overall, this error message says: β€œYou have a lazy seq object that you are trying to call as a function”, for example:

 (let [m1 (map some-function whatever) m2 (...more stuff...)] (m1 m2)) 

If you want to return a two-element list of m1 and m2 , instead of calling m1 as a function with m2 as an argument, you want to use the list function:

 (list m1 m2) 
+1
source

Assuming that you do not need the values ​​of events-cache , and that you want to get an make-event events-cache key map for the things you create with make-event , you can do:

 (def events (let [event-keys (keys events-cache)] (zipmap event-keys (map make-event event-keys)))) 

I'm not sure why you will have a cache that contains values, but then do not use these values, but this is another question :)

+1
source

And just for fun:

 (into {} (map (juxt identity make-event) event-keys)) 
+1
source

Source: https://habr.com/ru/post/1345976/


All Articles