access library functions in leiningen REPL

Related searches

For the development of a library I started from a lein project, invoked like so:

lein new mylib

if I call lein install now, I can access my library in other projects. But trying to immidiately test the functions I wrote failed:

lein repl
...
(dir mylib.core)
Exception No namespace: mylib.core found  clojure.core/the-ns (core.clj:4008)

Do I have to add something to the project.clj file maybe?

In order to use a library you must cause the code to be loaded - that it be on the classpath is not sufficient.

You can do this easily in an ns declaration in a file of course, but in the repl it can be easier to use (require '[my-lib.whatever :as w]) after which one can call (w/foo) (w/bar) etc. as expected. You can also use (in-ns 'my-lib.whatever) in order to switch to the namespace, but this will not give you a good result unless you have previously used require or use or load-file etc. to get the definitions first.

Access library function in clj file after adding dependency to project , Access library function in clj file after adding dependency to project.clj and I have to restart emacs so that I can use the library in any clojure file. Also, the pomegranate library has the ability to resolve and add dependencies from the repl. proto-repl. A set of helper functions for projects used in Proto REPL. N/A. cljdoc; 245,973 Downloads; 240,826 This Version; Leiningen/Boot [proto-repl "0.3.1"]

Let's say you created a new library named clj-foo.

% lein new clj-foo

Start your repl.

% cd clj-foo
% lein repl

In the repl, load the main entry point to your library and switch to its namespace.

(load-file "src/clj_foo/core.clj")
(ns clj-foo.core)

Now you're in the clj-foo.core namespace, make sure to add back in the repl ns to get things like doc available.

(use 'clojure.repl)

That's it. You're all set to start calling functions in your library. Note that other library files will be available from the clj-foo.core namespace if they were loaded by namespace declaration at the top of clj_foo/core.clj. If not, then you'll need to invoke load-file with their path as well.

If you make changes in core.clj. You can invoke load-file again to pick up the new code. As you progress, you can use cider to facilitate loading of individual functions and files. But that's for another question. :)

Programming at the REPL: Enhancing your REPL workflow, Most Clojure programmers don't use the terminal-based REPL for everyday you define functions for automating common development tasks (for example: starting a system-viz is a Clojure library for visualizing the components of a running� When I would launch the REPL using lein repl with that project, it would always put me in the project's core namespace automatically. Even though I was in that namespace, I could still access built-in resources e.g. (doc build-in-function-name), etc. Now, I've created another non-app project and I seem to have lost this autoloading capability.

You need to add a dependency to use your library from another project. To do this add a vector (a tuple-2) to the vector that is the value of the :dependencies key in the project.clj file. Here's an example:

:dependencies [[org.clojure/clojure "1.7.0"]
               [org.clojure/clojurescript "1.7.170"]
               [org.clojure/core.async "0.2.371"]
               [default-db-format "0.1.0-SNAPSHOT"]
               [com.andrewmcveigh/cljs-time "0.3.14"]]

My own local library is called default-db-format. Its really no different to adding a dependency for com.andrewmcveigh/cljs-time.

As you say you can already do this, but are having trouble getting a REPL connection to the project of the library itself. When you go (in-ns 'some-path), you need the single quote in front of some-path. Note that some-path is a different thing to the name of your library.

Rather than use lein repl you can use the figwheel repl - if your project is setup with figwheel. My library has only one entry point and that is lein figwheel devcards. After that I had no problem going to a namespace and trying out a function:

cljs.user=> (in-ns 'default-db-format.core)
nil
default-db-format.core=> (check 1 2)

As noisesmith mentioned having a REPL in your IDE is the best setup. No fiddly typing just bring up pre-configured REPLs (per namespace) with the click of a button (or keystroke). Figwheel/Cursive setup instructions here.

The REPL and main entry points, The clojure.main namespace provides functions that allow Clojure programs and interactive sessions to be launched via Java's� Leiningen - an extensible build tool that provides dependency management, REPL support, testing, packaging, deployment, and many other capabilities. Boot - build tooling for Clojure: instead of a special-purpose DSL, Boot supplies abstractions and libraries you can use to automate nearly any build scenario with the full power of the Clojure

I was also facing the same issue with the following configuration:

Leiningen 2.9.0 on Java 1.8.0_201 Java HotSpot(TM) 64-Bit Server VM

My file looks like this, and from the repl I desired to invoke the foo function

 (ns cljtest.test
  (:gen-class))

(defn foo [input]
  (assoc {} "a" 123)) 

Both these approaches worked fine for me on the repl.

1)Switch to the appropriate name space:

cljtest.core=> (in-ns 'cljtest.test)
#object[clojure.lang.Namespace 0x90175dd "cljtest.test"]
cljtest.test=> (foo nil)
{"a" 123}
cljtest.test=>

2)Require the appropriate name space:

cljtest.core=> (require '[cljtest.test :as test])
nil
cljtest.core=> (test/foo nil)
{"a" 123}
cljtest.core=>

Using third-party libraries, Start a free trial to access the full title and Packt library. We will go from downloading the file and starting Clojure REPL manually, to using The newly required function takes a vector of coordinates using the same pattern we have seen so� If you run lein repl from inside a Leiningen project directory, the library dependencies and source code of the project will be available at the REPL. Boot If you have installed Boot , you can use it to launch a REPL:

A Clojure Development Environment That Gets Out of Your Way, Fire up an interactive Clojure REPL session by typing lein repl in your project, we have to declare a dependency on the proto-repl library. the REPL has access to all of the values and functions defined in this source file:. Context. Leiningen uses the Maven library under the hood, to manage dependencies. As a consequence, the dependency scope mechanism is used to affect the classpath.. Continue reading →

Using the REPL, compute the sum of 7654 and 1234. Rewrite the following algebraic expression as a Clojure expression: ( 7 + 3 * 4 + 5 ) / 10. Using REPL documentation functions, find the documentation for the rem and mod functions. Compare the results of the provided expressions based on the documentation.

The set of commands (tasks in Leiningen parlance) natively supported by Leiningen can be very easily extended by various plugins. One of them is the lein-localrepo plugin which allows to install any jar lib as an artifact in the local maven repository of your machine (typically in the /.m2/repository directory of your username).

Comments
  • In which namespace is the REPL starting? I normally switch via (in-ns NAME).
  • dir should also work from other namespaces. However, trying to (in-ns ..) evokes the same error mentioned above...
  • in-ns will give you a seemingly unusable repl if run before loading that namespace's code. (clojure.core/refer-clojure) will rescue the repl from that state btw.
  • Just to be sure: Your have a corresponding (ns BLAH BLAH) in your files? No typo there?
  • Interesting thing is that with Figwheel (in-ns 'my-lib.whatever) is all you need to do. See here.
  • This is because figwheel is preloading the ns for you. You can't guarantee that every config does this (and typos in the ns form can break this even when your config is right).
  • unfortunately I keep getting the same error after adding my own lib to the dependencies as described in your example.
  • Yes - I don't really like/use lein repl. Answering this question before testing as usual! I'm investigating doing it with figwheel which also gives you a repl.
  • lein install works / doesn't give me errors. which it would, if there was something wrong with the dependencies. Figwheel is a cool thing, but rather for web development / or nodejs apps - at least I thought so.
  • My little library started off as a real library with no UI. But while 'doing the rounds' with boot and cljs.test etc I thought why not go back to what I know works. Even if your library doesn't have a UI your tests can (devcards rather than tests!).
  • Yeah, but that would be the best of course: having a repl that hot-reloads the library code whenever sth. changes.