How to use a dependency from clojure repl without starting a lein project?

I wish to use permutation and combinations on a list, it turns out clojure.core has nothing to offer in the way.
thus i need to use clojure/math.combinatorics, which can be added as a leiningen dependency so that one could do this :

(comb/combination [1 2 3 4 5] 2)

how does one get these dependencies without starting a leiningen project and on a simple clojure repl ?

2 Likes

Install CLI tools. Then run

$ clj -Sdeps "{:deps {org.clojure/math.combinatorics {:mvn/version \"0.1.4\"}}}"
7 Likes

If you’re a lein fan, there’s always the lein-try plug-in. It doesn’t require a project, but you would have to install the plug-in in your ~/.lein/profiles.clj.

If you’re a boot fan, you can do it directly on the command-line: boot -d org.clojure/math.combinatorics:0.1.4 repl (or, if you don’t care about the specific version, you can just leave it off and boot will automatically grab the latest: boot -d org.clojure/math.combinatorics repl).

1 Like

Another option to consider is Boot, which lets you start a REPL anywhere with any dependencies specified via the command-line:

boot -d org.clojure/math.combinatorics:0.1.4 repl

If you just want the latest version, whatever that is, you can omit the version spec:

boot -d org.clojure/math.combinatorics repl
1 Like

You don’t really need any plugins to do this, this is the syntax for adding a dependency from the shell

lein update-in :dependencies conj '[org.clojure/math.combinatorics "0.1.4"]' -- repl

Lately I’ve started adding a add-dependency helper based on Pomegranate to user.clj in my projects, so it’s easy to load an extra dependency to try out without restarting your REPL. You could make this available to any lein repl by adding it to your ~/.lein/profiles.clj.

{:user {:dependencies
        [[com.cemerick/pomegranate "0.4.0"]]

        :incjections
        [(defn add-dependency [dep-vec]
           (require 'cemerick.pomegranate)
           ((resolve 'cemerick.pomegranate/add-dependencies)
            :coordinates [dep-vec]
            :repositories (merge @(resolve 'cemerick.pomegranate.aether/maven-central)
                                 {"clojars" "https://clojars.org/repo"})))]}}

It loads pomegranate upon first usage, so it doesn’t add to startup time.

% lein repl
nREPL server started on port 36519 on host 127.0.0.1 - nrepl://127.0.0.1:36519

user=> (add-dependency '[org.clojure/math.combinatorics "0.1.4"])
{[org.clojure/math.combinatorics "0.1.4"] #{[org.clojure/clojure "1.7.0"]}, [org.clojure/clojure "1.7.0"] nil}
user=> (require '[clojure.math.combinatorics :as comb])
nil
user=> (comb/combinations [1 2 3 4 5] 2)
((1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (2 5) (3 4) (3 5) (4 5))

9 Likes

TIL that the forum will not allow me to post a comment containing only “+1”.

1 Like