Fork me on GitHub
Vincent Cantin00:02:10

Is it possible to write a macro that returns the qualified name of a symbol passed as parameter? Example:

(my-macro foo)
; => this-ns/foo

(my-macro other-ns/foo)
; => other-ns/foo


macros don't implicitly namespace symbols, but the ` reader-macro does


but it doesn't apply to your args

Vincent Cantin00:02:14

hm .. so I guess that I have to (in the macro) manually append the current ns to the symbol’s name when it is unqualified.


also "current ns" in a macro can be counterintuitive


Yeah, is it the namespace where the macro is defined, or where it is invoked?


And out of curiosity, what problem are you trying to solve?

Vincent Cantin01:02:29

It’s a little difficult to explain ..

Vincent Cantin01:02:35

I would like to refer to external functions from inside the template as if the template was Clojure syntax.

Vincent Cantin01:02:12

For example:

(defn sum [a b]
  (+ a b))

(defc my-comp [data]
  {:id :app/my-comp}
  (let [x (:number1 data)
        y (:number2 data)]
    [:div x " + " y " = " (sum x y)]))

Vincent Cantin01:02:03

the let expression is just symbolic data, but I wish to have current-ns/sum and its value from the symbol sum

Alex Miller (Clojure team)02:02:15

you can resolve in terms of *ns* - is that sufficient?

Vincent Cantin02:02:05

I think that would work. Thank you. I will try it tonight. (in 12 hours)


Hi everyone. I'm just getting started with clojure...

🎉 40
👋 28
👍 20

Hey. I am debugging some code with a toplevel def and use something of the following form to try to monitor the changes.

(def ^:dynamic *a* (atom {}))

(add-watch *a* :watcher
           (fn [key atom old-state new-state]
             (prn "-- Atom Changed --")
             (prn "key" key)
             (prn "atom" atom)
             (prn "old-state" old-state)
             (prn "new-state" new-state)))

(reset! *a* {:foo "bar"})
The above works fine in a clean project, but somehow has no effect (nothing printed) in the code I am working on. Is there something I need to be aware of when using add-watch or any ideas what could be the issue?


The repo where it doesn't work is btw


Somethow my add-watch statement has no effect if I do

(-> *a* .getWatches keys)
I get nil.


why put an atom in a dynamic var?


that's weird


@U051SS2EU yes I don't know. haven't written the code myself. Maybe its stayed this way as a legacy.


No there is some good reason, but I don't know exactly. Without the ^:dynamic the code fails.


I'm suspicious that the failure could be related to the way the dynamic var is used - it could point to another atom in the code you care about


Hi! Beginner question here, I'm trying to find my way through the options of serializing Clojure objects. I have a small ML model as an instance of a deftype that implements a bunch of protocols. Now that it's trained, I'd like to save it out to disk so that I can use it later on. In Python I'd either use TensorFlow's options to save to disk, or just use Pickle if it's an class I defined myself. I don't seem to find an equivalent to Pickle that works out of the box for custom objects in Clojure though. Am I missing something? A bit more background info: What I'm trying to save out is small neural net, implemented as a NeuralNetworkInference deftype as defined in Dragan Rocks' blogpost: So it's basically a bunch of Neanderthal matrices and some function definitions wrapped in an interface. Thanks! 🙂


@frederikdieleman You do not need to serialize functions etc, just data - your weight and bias matrices. Until I provide an out-of-the-box solution for that (in Deep Diamond) please see what Kamil Toman provided as a DIY solution


Neanderthal-stick doesn't seem to be on clojars, so had to clone it. In the end it did turn out to be the easiest solution. Worked great, only thing I still needed to add was a way to serialize the type of activation function each layer has. On a sidenote: I didn't find the repo of the code you created in the "Deep Learning from Scratch to GPU" series and hence don't know what kind of licence you've put on it. Is it ok if I use it in a repo of mine? (with mention of source of course)


@frederikdieleman java comes with serialization, but for clojure data you can use transit and extend it for native types as needed


transit will pick up the custom data readers / writers for any defrecord / deftype

Ivan Koz16:02:44

careful with java serialization tho, it's a mess and leaky, better to avoid it


this is an example of using java's built in serialization, fwiw - I agree that it has serious problems

(import ( FileOutputStream

(require '[ :as io])

(defn to-file
  [file-name & data]
  (with-open [file (FileOutputStream. file-name)
              stream (ObjectOutputStream. file)]
    (doseq [d data]
      (.writeObject stream d))))

(defn from-file
  (with-open [file (FileInputStream. file-name)
              stream (ObjectInputStream. file)]
    (into []
          (take-while (complement #{::done}))
             (.readObject stream)
             (catch IOException _ ::done))))))


I think the problems with java serialization are less severe with clojure (especially if you commit to reading and writing from the same clojure version) but transit is still more compact and it's human readable


Thanks for the help! In any case it's just for short-term saving of my own ML models, so not too worried about potential risks with it. Unfortunately Neanderthal matrices are not serializable, so will have a look at one of the other options.


the obvious workaround would be to convert neanderthal matrices into a tag plus an array of arrays (that should serialize very efficiently...) - but I'm sure it's even simpler in a proper clojure serialization lib


I'm using nippy in my hobby project


it works out of the box with all Clojure standard types AFAIK, and serialises to bytes

Michaël Salihi21:02:29

As a Clojure beginner, I just discover condp and wow ! 😮 I updated my "Perfect number" exercism and now it's more concise than cond


Wait till you realise a special case condp = is equal in power to switch/case in most languages. condp can do so much more.

👍 4