JPT
JPT

Reputation: 519

Converting a list back to a map of maps

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?

Upvotes: 4

Views: 1201

Answers (4)

kotarak
kotarak

Reputation: 17299

And just for fun:

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

Upvotes: 1

trptcolin
trptcolin

Reputation: 2340

Assuming you don't care about whatever the values of events-cache were, and that you want to end up with a map of the events-cache keys to the things you generate with make-event, you could do:

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

I'm not sure why you'd have a cache that includes values, but then not use those values, but that's another question :)

Upvotes: 1

Justin Kramer
Justin Kramer

Reputation: 4003

Maybe this is what you want?

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

Upvotes: 7

amalloy
amalloy

Reputation: 91897

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)

Upvotes: 1

Related Questions