So great, @nate!!! And I used my first juxt
last week — felt great, even though I ended up deleting it, because that whole section of code was wrong-headed!
@nate: Just curious, because I’m running into this right now, and you’ve mentioned it in your podcasts — is there an easy way to tell if a keyword is namespaced? :events/reset-card
vs. ::reset/card
.
Is there a better way than converting to string, and looking at first character? Seems… unexpectedly hacky. Thx!!!
@genekim To detect the namespace at all, you can use the namespace
function. It returns nil
if there is no namespace. Eg. (when (namespace ::reset/card) :found-namespace)
.
The ::
syntax is just shorthand for referencing a namespace. It's expanded by the reader. ::
by itself is the "current" namespace. Eg.
user=> ::stuff
:user/stuff
And for something you've required:
user=> (require '[clojure.set :as set])
nil
user=> ::set/difference
:clojure.set/difference
So in that case ::set
turns into :clojure.set
.
user=> (namespace ::set/difference)
"clojure.set"
As far as I know, you can't really tell if a namespace was created with the shorthand ::
or not.
user=> (= ::set/difference :clojure.set/difference)
true
user=> (identical? ::set/difference :clojure.set/difference)
true
Symbols are "interned", so identical values will be set to point to the single (immutable) value in memory, so the references will be identical if the value is equal.
I really wish namespace
would give you the keyword for the namespace instead of a string. But you can always convert it if you want the benefits:
(some-> some-keyword-value namespace keyword)
eg.
user=> (some-> ::set/difference namespace keyword)
:clojure.set
user=> (some-> :difference namespace keyword)
nil
@genekim Thank you! And let us know how it goes!