clojuredesign-podcast

Discussions around the Functional Design in Clojure podcast - https://clojuredesign.club/
genekim 2020-07-26T19:05:48.123700Z

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!

genekim 2020-07-26T19:07:34.125500Z

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

neumann 2020-07-26T19:15:18.127500Z

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

neumann 2020-07-26T19:16:45.128700Z

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

neumann 2020-07-26T19:18:01.129200Z

And for something you've required:

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

neumann 2020-07-26T19:18:38.129600Z

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

neumann 2020-07-26T19:18:53.130Z

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

neumann 2020-07-26T19:20:01.130800Z

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

neumann 2020-07-26T19:22:29.132400Z

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.

neumann 2020-07-26T19:26:38.134500Z

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)

neumann 2020-07-26T19:26:46.134700Z

eg.

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

genekim 2020-07-26T20:25:36.135600Z

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

neumann 2020-07-26T20:26:33.136Z

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