Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a list back to a map of maps

Tags:

clojure

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

I then apply, (def events (map #(make-event %) (keys events-cache))), applying the make-event function for each key from the cache, and want a map back containing each event map keyed by key.

What I get back is that, but inside a list. So calling any map functions, to search etc., throws an error, clojure.lang.LazySeq cannot be cast to clojure.lang.IFn.

I'm sure I'm thinking about this all wrong, but is there a way to pull the map of maps from the list?

like image 999
JPT Avatar asked Dec 28 '22 23:12

JPT


2 Answers

Maybe this is what you want?

(into {} (for [k (keys events-cache)]
           [k (make-event k)]))
like image 198
Justin Kramer Avatar answered Jan 15 '23 20:01

Justin Kramer


Your terms are vague, and the error message you post suggests the issue is of a very different sort than the question you're asking. You're likely to get more help if you post some code, and especially a real stacktrace.

But in general, this error message says "You have a lazy seq object that you are trying to call as a function", like:

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

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

(list m1 m2)
like image 33
amalloy Avatar answered Jan 15 '23 21:01

amalloy