Fork me on GitHub
Matthew Twomey02:11:29

Pattern question: I have a lib of functions which interact with a client. Most of these functions have the pattern of: get-client; do-stuff using the client. In other non Clojure environments, I would be likely to create a getClient function that returns the existing client or if one doesn’t exist, creates and returns it (thereby reusing the client). I am having trouble “thinking in Clojure” for this, what are some ways I can achieve similar client reuse?


In general we tend to pass things like clients in as function arguments. So you may have (defn do-stuff [client ,,,] ,,,). This keeps your function pure and allows you to test it very easily.


Dependency injection libraries like, and can help you manage the starting/stopping and cross-referencing of stateful components like that.


But in some cases a bit of global state (with all its attendant problems) could be sufficient. For such cases I like kind of pattern.


If I understand your question correctly, the following code might help

user> (def get-client
        (let [myclient (atom nil)]
          (fn [] (if @myclient
                   (do (println "reusing") @myclient)
                   (do (println "creating") (reset! myclient 42) @myclient)))))
user> (get-client)
user> (get-client)


I.e. prefer a binding, and fall back to an atom.

Matthew Twomey02:11:31

Thanks both @UJY23QLS1 and @U0AT6MBUL for giving me a couple of options to think on here. I can see how I can apply these. I think I’ll ponder for a bit before I settle on an approach. One side-note, I realized I could “keep the function pure” by always passing the client along with the request, but I don’t want the users of the lib to have to deal with (or even know about) the client aspect of it.

Matthew Twomey02:11:33

I also see that these libraries you linked @UJY23QLS1, are aimed squarely at this situation. So I’m going to think about if I want / need to go that route of it it’s overkill for my fairly simple use case.


Yeah, it probably won't be worth it for simple cases.


Still, initializing some library state, passing it back to the caller, and expecting that state as a function argument, is quite a common pattern. E.g.

Matthew Twomey02:11:14

Yeah, that I do realize. I’ve done that myself often over the years. I just dislike doing it in cases where there is no “other” reason for the caller to deal with the client - besides avoiding caching the client on the “backend”. It feel like I’m just dumping the problem onto the caller.


instead of seeing it as dumping the problem onto the caller, you can view it as giving the caller more explicit control over state management, which I think anyone consuming a Clojure library will appreciate.

☝️ 4
Matthew Twomey04:11:25

Fair enough @UG00LE5TN, I’ll think on that.


Two common approaches are (foo client foo-args) and (foo foo-args {:client client :extra :opts}) - I think if the client state is something that is secondary to the problem and will unlikely be modified or need to considered by the API user, then perhaps the second approach is more appropriate (since the opts map is optional). Then the library could def or memoize a default client that is used if none is passed in. Two other approaches that I am not a fan of is multi-arity functions (with and without extra client arg); and dynamic vars. It's mostly a question of API aesthetics, but e.g. I would consider the first-arg approach for a database connection (a client API must know about and setup a DB conn) and the optional-map approach more for an overridable threadpool in an HTTP client libary. Also remember that it would be good if the convention is consistent for all functions in the library: they should all take the client as the first argument (e.g. a datasource in next.jdbc) or as the last argument in all variadic functions (e.g. the optional :registry key in all malli functions).


I am trying to follow the "Quickstart" set up of XTDB which can be found in this url: As I followed the steps, I tried to start the project repl and connect in VS Code using Calva. When I did, the terminal threw this error:


# A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00007f212f299073, pid=11126, tid=11178 # # JRE version: Java(TM) SE Runtime Environment (19.0.1+10) (build 19.0.1+10-21) # Java VM: Java HotSpot(TM) 64-Bit Server VM (19.0.1+10-21, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64) # Problematic frame: # C [] Java_org_rocksdb_RocksDB_getSnapshot+0x3 # # Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport -p%p -s%s -c%c -d%d -P%P -u%u -g%g -- %E" (or dumping to /home/user/dev-clojure/xtquickstart/core.11126) # # An error report file with more information is saved as: # /home/user/dev-clojure/xtquickstart/hs_err_pid11126.log # # If you would like to submit a bug report, please visit: # # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. # Aborted (core dumped) Jack-in process exited. Status: 134


you can also ask in the #CG3AM2F7V channel. It looks like the problem might be that you're on a linux-amd64 and using a configuration for linux64.


Thanks a lot for the reply. I am going to reach out to the channel and convey to them my question.


"It looks like the problem might be that you're on a linux-amd64 and using a configuration for linux64." If this is really the cause of the error, do you have any idea on how am I going to fix this? Do I need to re-install something?


"It looks like the problem might be that you're on a linux-amd64 and using a configuration for linux64." What exactly do you mean by your reply? Does it have to do with my Java SDK installation?


it doesn't look like rocksdb has a native lib for linux-amd64


you can try using one of the other storage options,


Does this mean that I can use XTDB on linux if RocksDB is not going to work on it?


Ok, I will try jdbc. Thanks.


Thanks a lot. It now works!

🎉 1

May I know why this error came out? If possible, kindly lead me to the direction where I can fix this problem. Thank you very much!

Valentin Mouret08:11:21

Hello 🙂 I am finally able to work in Clojure and I finding it very pleasant. It’s especially satisfying when walking back on problems and solving them the Clojure way™. Somehow, this leads me to structurally repetitive code, and that sounds like a queue for macros. has proven really helpful already to understand how to technically write one, but I still struggle to see how to apply them in the real world. Do you have examples in the wild that could be helpful for a beginner?

Valentin Mouret09:11:22

For example, I am trying to wrap to make it a bit simpler to use. Its migrate and rollback functions take a config as a first argument. In my case, I currently have two environments: dev and prod, and I would like to default to dev. So, I wrapped them like so:

(defn- get-config
  ([]    (get-config ::postgres/dev))
  ([env] (update-in base-config [:db]
                    merge (env postgres/configs))))

(defn migrate
  ([]    (migrate ::postgres/dev))
  ([env] (-> env get-config migratus/migrate)))

(defn rollback
  ([]    (rollback ::postgres/dev))
  ([env] (-> env get-config migratus/rollback)))
And here is the structural repetition. rollback and migrate carry very little information, 95% is repetition. I feel like macros would help me here, but maybe I am mistaken.


Think of macros as functions around your code. Can you see where you could plug in parameters for each?


Although note that clojure generally discourages macros, as they add extra hidden complexity to code.


With that in mind, what would you like your macro api to look like? (wrap-migratus "dev")?

Valentin Mouret09:11:45

I was rather thinking about defining a macro to write 2-arity functions, such that I can write: (defoperation rollback migratus/rollback) Maybe that’s a bad idea, but that’s what instinctively comes to me when I see the above code. Maybe there are more idiomatic ways of dealing with the problem.


Sure, why not? That works well enough. Have you tried writing a macro yourself?

Valentin Mouret09:11:08

Well, I did but failed miserably. They are a bit tedious to write as well, the errors are not very clear for a beginner like me. With my current knowledge, it would take too much time to write it, and it would not be worth it in my opinion. That’s why I am inquiring about good examples in the wild, so that I can learn from good examples.


Oh right. Well I'm on the train home and it looked like fun, so

(ns scratch)

(defmacro defoperation
  [op-name op-reference]
  `(clojure.core/defn ~op-name
     ([]    (~op-name :ns.postgres/dev))
     ([env] (clojure.core/-> env get-config ~op-reference))))
  '(defoperation rollback migratus/rollback))
;; => result
 ([] (rollback :ns.postgres/dev))
  (clojure.core/-> scratch/env scratch/get-config migratus/rollback)))


Of note: it's probably a good idea to not rely on relative namespaces with macros


Actually that's probably a small error in there, one moment

Valentin Mouret09:11:42

I was going to ask the question. 😄


So things to note: because I didn't give env a namespace, it evals to the current namespace because of the backtick at the beginning


I specifically removed the :: from postgres/dev to something with an absolute namespace for the same sort of reason

Valentin Mouret09:11:02

Why macroexpand-1? I think I had a similar macro (delete it since :face_palm: ), but your macro also fails with macroexpand. It works with macroexpand-1.


So macroexpand will then expand every other macro below it

💡 1

macroexpand-1 only does the top level macro


So you can do (-> form macroexpand-1 macroexpand-1) to do two macro expansions

Valentin Mouret09:11:59

That makes a lot of sense.

Valentin Mouret09:11:28

Thanks a lot for you time and clear explanations. 🙇

❤️ 1

Note that I'm middling at best with macros. If you want to go heavy macros I'd recommend reading SICP


There's a lot of gotchas with macros and they're hard to reason about, so again it's generally recommended to avoid them in clojure without a very good reason


And probably not when it's only a couple of cases for your pattern. Probably more like when you have half a dozen items of very hairy syntax repeated the same way over and over


One good example (closed code but I can explain what it's doing) is importing a bunch of react components into reagent


So there's a bunch of slightly hairy stuff around looking up a name in a js list of objects, then defing each one with adapt-react-component


About 3-4 lines per entry. Not terrible but still painful when pulling in 30+ objects


So the declaration is instead something like (import-raws [Button Card Overlay ...])


And each item is now def'd in that namespace for import elsewhere

Valentin Mouret09:11:16

That makes sense. I agree that it’s overkill for my case, it felt like a good experiment though. Which is not over because the macro is not actually working 😄. macroexpand-1 is working, but macroexpand is not. It’s saying it does not conform to defn’s spec.

Valentin Mouret09:11:20

1. Caused by clojure.lang.ExceptionInfo
   Call to clojure.core/defn did not conform to spec.
                         ([] (migrate :me.postgres/dev))

Valentin Mouret09:11:38

(`me` is the root namespace of my project)


Oh I see. me.migrations/env is throwing the error, can't be namespaced


(defmacro defoperation
  [op-name op-reference]
  `(clojure.core/defn ~op-name
     ([]    (~op-name :ns.postgres/dev))
     ([~'env] (clojure.core/-> ~'env get-config ~op-reference))))
;; =>
 ([] (rollback :ns.postgres/dev))
 ([env] (clojure.core/-> env scratch/get-config migratus/rollback)))

💡 1

So ~ is evaluate, so ~'env is evaluate (quote env), which makes it just the symbol env


(Well it's not quite evaluate, but it'll do for our understanding)

Valentin Mouret09:11:08

Now I understand the juggling with chainsaw analogy. 😄


Yeah.. as a learning exercise absolutely do some more, but avoid in codebases where possible

Valentin Mouret09:11:38

Thanks for the solution and cautionary tale, it now works like a charm and will I avoid using it. 😄

😂 1

Again, if you want to dive more into the wonderful world of macros (or just get an idea of what you can really do with them), have a read through SICP


Also so you can learn how to make hygienic macros

Valentin Mouret10:11:20

It feels like a threshold along the journey. Write macros because you think you need them, get bitten hard, stop using macros, carefully write macros. Like this meme with the gaussian curve.


I dunno, I'm pretty sure I'm not even at the peak of the curve, and I feel like after the peak there's the racketeers


And their brains are just the next level aliens I'm nowhere near yet


(I still like Clojure more than racket for sheer pragmatism and getting stuff done, but good god do they have some weird and cool stuff)

👍 1
☝️ 1

Jo is there a way to write a link to a file (relative to the project root, the default dir when running a dev repl) in a doc string? If I do [[]] cider tries to interpret the string as a symbol. I realized a better way is to link to a perma link of e.g. the github file - so I guess I answered my question


is there a function to extract the namespace (as a keyword or symbol) of a fully-qualified keyword?

Alex Miller (Clojure team)14:11:03

that will give you a string, which you can pass to symbol or keyword


that's what I get for trying to trust google instead of just looking back over thanks!


You may also find one of the searchable cheastsheets here useful:


Or in a REPL, the apropos and find-doc functions.

Matthew Twomey15:11:02

I hadn’t seen this cheatsheet, awesome


The official version on has everything except the dynamic search text box, but you can use browser text search on the page to look for strings, too: That page also has a link to the one that I gave above.


I have a question which I though I for sure would have figured out by now. Assume I have a protocol FooAbility with methods a and b defined in namespace one. Assume further that I want to reify and use an instance implementing the protocol in namespace two. Is the correct way to require '[one :as p]) in namespace two and then have to prefix (p/a instance args) in all references to the protocol itself and the methods in two?


due to requires cycles I refactored out some protocols and instead of using them from current namespace I now have them in a separate one and realized I don't quite grok the above. It seems you can not "reuquire the protocol" but that the methods are entities in themselves and if I wanted to avoid namespace prefixing (yeah I know you probably should) I would have to refer both the protocol and the protocol methods?

Alex Miller (Clojure team)16:11:26

protocol methods are functions in the namespace where the protocol is defined, and you should treat them that way for calling purposes


ok makes sense from my poking at the repl

Alex Miller (Clojure team)16:11:24

that means requiring the namespace defining the protocol and using function names that sufficiently refer to that (either by fully qualified name, aliased name, or by referring them to use as short name)


in fact that makes things simpler


excellent, thank you!

Alex Miller (Clojure team)16:11:20

an important thing here is that functions are an abstraction and from the caller perspective, whether you are calling a function, a macro, a multimethod, a protocol method, a var that holds a map, etc - the caller says exactly the same thing (f args)

Alex Miller (Clojure team)16:11:48

which means the implementor is free to change the implementation without affecting the caller

💡 1

with that understanding I guess this should have been obvious


thanks again - this makes total sense

Jared Langson19:11:13

How do I dynamically construct regex? I have a function like

(defn my-funct [string character]
  (let [pattern (re-pattern (str "#" character))]
do stuff))
I want to take a single character and turn it into a regex. How do I do that?


i'm not quite following. Do you have an example of how you want your function to behave? I was about to suggest looking into re-pattern, but I see that you've already found it.


(re-find (re-pattern "#.") "abc#def")
;; => "#d"
(re-find (re-pattern ".#.") "abc#def")
;; => "c#d"


The # prefix to regexes just tells the reader to call re-pattern on the following string

☝️ 1

Your issue is likely the fact that you are prepending # onto the string of your regex to mimic the syntax of regex literals before passing to re-pattern

👍 1

;; equivalent:
(re-find (re-pattern "#.") "abc#def")
(re-find #"#." "abc#def")

👍 1
Francesco Pischedda19:11:12

A story about defmulti, compiling and general project structure -> long message in thread

Francesco Pischedda19:11:53

Hi all! Recently I have been a bit confused by how defmulti/defmethod works and I could not find anything to help me to understand what was going on or how to better approach my problem. What I wanted to do was to provide a pluggable interface to store and retrieve some data in a pet project, something that could be changed via simple configuration, which could look like this:

(def config {:persistence {:backend :json-file
                           :base-path "/some/path"}})


(def config {:persistence {:backend :jdbc
                           :connection-string "sqlite:///:memory:"}})
The generic persistence interface, the implementation and the usage would be something like this:
;; persistence.clj
(ns persistence)

(defmulti store-data! (fn [config id data] (:backend config)))

(defmulti get-data (fn [config id] (:backend config)))

;; persistence.json_file.clj
(ns persistence.json-file
  (:require [persistence :refer [store-data! get-data]]))

(defmethod store-data! :json-file
  [config id data]
  (store-data-to-filesystem (:base-path config) id data))

(defmethod get-data :json-file
  [config id]
  (get-data-from-sowmhere (:base-path config) id))

;; client-code.clj
(ns client
  (:require [persistence :refer [store-data! get-data]]))

;; ...and some code dealing with data...
So far so good. When working with an active REPL I was evaluating all namespaces, everything was working as expected, the sun was shining and birds singing. Later I created an uberjar, deployed and the application started complaining that there was not an implementation of the defmulti for was not good anymore. It looks like that, because persistence.json-file was not required anywhere, the compiler did not know about it and that specialization was lost. The same happens if I start a fresh new REPL and evaluate the client namespace. I've "solved" it by requiring persistence.json-file in the client namespace but it feels wrong. Questions (finally): - Is there a way to say "please compile this namespace even if it is not referenced anywhere"? - Is there something wrong in the whole approach? i.e. is there a better way to build this configurable storage?


FWIW, I do it the same way.

Francesco Pischedda20:11:51

Yes, it works, but somehow I feel it is breaking the abstraction


Yeah, requiring an ns for no other reason than to get the methods defined feels odd, but it makes sense at least: how else is your Clojure to know that they exist? There are other, more dynamic ways to accomplish this (e.g. search for and loading code in the classpath), but they are a lot more involved.

Alex Miller (Clojure team)20:11:25

the code using the abstraction does not have to be the same code that's configuring the extensions by loading them

Alex Miller (Clojure team)20:11:41

but yes, someone has to load them

Francesco Pischedda21:11:21

Thanks! Can someone point me to some docs/articles to help me understand how to achieve the dynamic loading you are mentioning?


I was just reading about this yesterday, but cannot remember where. Basically, if you do not require the namespace, then the JVM will not even bother building the class files. So you're doing it right by just requiring the namespace, even if it "feels weird".