I am new to Clojure. Currently trying to learn the language by reading books and trying to build a scraping tool using Clojure.
In the book ‘On Lisp’ Paul Graham advises a lisp programmer to keep some useful utility functions handy while working various projects. I was wondering how I can achieve this.
Step 1: Create a utility library.
Step 2: Require it in your app.
Nothing sets a utility library apart from any other type of library. They’re all libraries.
Here is a list of utility libraries authored by experienced Clojurians:
Personally, I tend to separate them according to their dependencies. For example, utility functions that don’t require any dependency, that are pure Clojure, go into a general-purpose utility library with no dependencies. Functions that are more specific to a line of applications go into a separate library. For example, I have a small utility library devoted to ring applications, with a dependency on ring.
Here is a quick overview of how I organize things, but remember that there is a lot of leeway and utlimately it’s up to you.
If you’re using Clojure CLI / deps.edn then this is very straightforward. You can add it as an alias to your projects.deps.edn, or even as a global alias in ~/.clojure/deps.edn. You can use :local/root to simply point at the directory with your project, or reference it by git coordinates. No need to push to Clojars.
Checkouts are more intended for when you’re developing interdependent libraries at the same time. I never found them particularly handy to work with. Just doing a lein install of your utility project should work though, then you just include it as any other dependency (probably under a profile).
https://jitpack.io/ use libraries directly off github. I also blogged about that a while back. with deps.edn you don’t need this as it has first class support for git dependencies, but with leiningen this is quite handy
The simplest thing is to put your favorite functions in user.clj, which needs to be on the classpath so Clojure can find it. If you use Leiningen, put it at dev/user.clj relative to the directory with your project.clj.
So you can actually separate your user file into mutliple files and load-file them together.
That said, this is more of a dev/messing around approach. If you plan to bundle or release programs which depend on your utility lib, I think having a separate repo with your personal utility lib in it, that you lein install might be best.
The only thing is that, how would I make new changes in utility library available to other projects?
I will have to first publish it to github. Pull it using ‘lein deps’.
Is there any other better way?