danny
danny

Reputation: 3266

How to understand the Clojure name function?

I am confused by Clojure's name function. Its document is:

(name x)
Returns the name String of a string, symbol or keyword.

example:
user=>(name :x)
"x"
user=>(name "x")
"x"
user=>(name 'x)
"x"

so what is the point of this function? where/how to use it?

Upvotes: 3

Views: 89

Answers (1)

d11wtq
d11wtq

Reputation: 35308

Just think of it as a type-cast. You use name when you need to ensure you're working with a String data type.

For example turning the map {:foo 42, :bar "example"} into the JSON {"foo":42, "bar":"example"} requires the use of name on the keys, as opposed to str, which would produce {":foo":42, ":bar":"example"}.

Upvotes: 3

Related Questions