Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a clojure keyword into a string?

Tags:

clojure

In my application I need to convert clojure keyword eg. :var_name into a string "var_name". Any ideas how that could be done?

like image 363
Santosh Avatar asked Sep 15 '10 15:09

Santosh


2 Answers

user=> (doc name) ------------------------- clojure.core/name ([x])   Returns the name String of a string, symbol or keyword. nil user=> (name :var_name) "var_name" 
like image 90
kotarak Avatar answered Sep 30 '22 21:09

kotarak


Actually, it's just as easy to get the namespace portion of a keyword:

(name :foo/bar)  => "bar" (namespace :foo/bar) => "foo" 

Note that namespaces with multiple segments are separated with a '.', not a '/'

(namespace :foo/bar/baz) => throws exception: Invalid token: :foo/bar/baz (namespace :foo.bar/baz) => "foo.bar" 

And this also works with namespace qualified keywords:

;; assuming in the namespace foo.bar (namespace ::baz) => "foo.bar"   (name ::baz) => "baz" 
like image 24
txmikester Avatar answered Sep 30 '22 20:09

txmikester