Clojure card key with spaces in the key name

I am new to Clojure. I have a result map from a database and it contains key values, such as:

{:Unique Product ID "09876" :ProductName "XYZ"} 

I want to get values ​​from the map, but I'm having trouble getting a unique product identifier.

 ProductID (str ( map-name ":Unique Product ID")) 

The product name works great using:

 ProductName (str ( map-name :ProductName")) 

I am not sure how to handle the space in the field of the Product ID field. How do I get the value for this key from the card?

thanks

+7
source share
3 answers

Try it (keyword "Unique Product Identifier")

+14
source

Space is not a valid character in a keyword, you are trying to do something that will almost certainly cause pain in the future.

Note that the keyword function does not test it, so @ jeff-johnston is incorrect. I'm afraid.

Long discussion here:

https://groups.google.com/d/topic/clojure/WvXYkvLoQhI/discussion

clojuredocs has been updated with new docstrings after this discussion, see here:

http://clojuredocs.org/clojure_core/clojure.core/keyword

+7
source

You can use (keyword) , as Jeff points out, but I think you would be better off at all if you converted the map that you return from the database query to one that doesn't have spaces. I find this feature useful for this purpose:

 (defn despace [m] (zipmap (map #(keyword (clojure.string/replace (name %) " " "_")) (keys m)) (vals m))) 

Then use underscores instead of spaces:

 (:Unique_Product_ID (despace {(keyword "Unique Product ID") "09876" :ProductName "XYZ"})) #=> "09876" 
0
source

All Articles