Fork me on GitHub
#clojuredesign-podcast
<
2020-07-26
>
genekim19:07:48

So great, @nate!!! And I used my first juxtlast week — felt great, even though I ended up deleting it, because that whole section of code was wrong-headed!

genekim19:07:34

@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!!!

neumann19:07:18

@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).

neumann19:07:45

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

neumann19:07:01

And for something you've required:

user=> (require '[clojure.set :as set])
nil
user=> ::set/difference
:clojure.set/difference

neumann19:07:38

So in that case ::set turns into :clojure.set.

neumann19:07:53

user=> (namespace ::set/difference)
"clojure.set"

neumann19:07:01

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

neumann19:07:29

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.

neumann19:07:38

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)

neumann19:07:46

eg.

user=> (some-> ::set/difference namespace keyword)
:clojure.set
user=> (some-> :difference namespace keyword)
nil

genekim20:07:36

So good, @neumann !!! Thank you!!! I’ll keep you posted on my use of it — keep up all the great work with @nate !!

neumann20:07:33

@genekim Thank you! And let us know how it goes!