tl;dr
How can I derive a keyword from a number in ClojureScript:
(keyword 22)
;;=> :22 but in fact returns nil.
Background
In my ClojureScript/Hoplon application I make HTTP requests via cljs-http. Parts of the response I receive look like this:
{:companies
{:22 {:description ... } ; A company.
:64 {:description ... }
... }
{:offers
[{:description ... } ; An offer.
{:description ... }
... ]
Each offer within the vector behind :offers
has a :companyId
which represents a key in :companies
. As soon as I receive the response, I reset!
a cell (similar to an atom) query
.
Now, I'd like to iterate over each offer and call a function offer-tpl
that creates the corresponding HTML. In order to do so, offer-tpl
needs the offer itself as well as the related company:
(for [offer (:offers @query)]
(offer-tpl offer (get-in @query [:companies (keyword (:companyId offer))]))))))
Despite the fact that this surely can be done more elegant (suggestions very appreciated), the get-in
doesn't work. (:companyId offer)
returns a number (e.g. 22
) but (keyword (:companyId offer))
returns nil
. Calling (keyword (str (:companyId offer)))
does the trick, but aren't there any other ways to do this?