newbie question: how to include external libraries from Leiningen REPL

1,247 views
Skip to first unread message

Starry SHI

unread,
Nov 7, 2013, 8:18:30 AM11/7/13
to clo...@googlegroups.com
Hi, I am new to clojure and I find lein REPL very convenient to use. But I have one question: in REPL, how to include functions defined in other libraries and call them in my clojure code?

For example, my code need to call square root function defined in clojure.contrib.math (included in clojure-contrib.jar). Can anyone tell me how to include this jar file from lein repl, so that my code can call sqrt function?

Thank you! 

Cedric Greevey

unread,
Nov 7, 2013, 9:13:04 AM11/7/13
to clo...@googlegroups.com
Assuming the Java or Clojure library is on your classpath, it *should* be usable from the REPL.

(import '[java.library SomeClass])

(.foo (SomeClass. 42))

(require '[clojure.contrib.math :as m])

(m/sqrt 42.0)

or whatever.

(Didn't this exact question just get asked by someone five minutes ago? Even looking for the same math function.)



--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Mars0i

unread,
Nov 7, 2013, 10:47:46 AM11/7/13
to clo...@googlegroups.com
I'm pretty new, also.  The Leiningen documentation that's easy to find has all of the information you need ... but it's not easy to sort out at first.  Cedric Greevey gives the answer for standard libraries that usually come with Clojure.  For others, I suggest:

Find the library name and version number that you want at clojars.org.

Create a project directory with a command like:
lein new app newdir

Then edit newdir/project.clj:
Add
    [libname-from-clojars.org "version number"]
inside the outer pair of brackets [] after :dependencies,
and save.

Then change to the newdir directory and enter
lein deps

Then start the lein repl.

Then follow Cedric's instructions.

Sean Corfield

unread,
Nov 7, 2013, 6:53:28 PM11/7/13
to clo...@googlegroups.com
On Thu, Nov 7, 2013 at 5:18 AM, Starry SHI <star...@gmail.com> wrote:
> Hi, I am new to clojure and I find lein REPL very convenient to use. But I
> have one question: in REPL, how to include functions defined in other
> libraries and call them in my clojure code?

As Marshall indicated, you need to edit your project.clj file - which
in turn means you need to have such a file, which you create with:
lein new myproject (or whatever you want to call it). Leiningen will
create a folder called myproject containing a bunch of stuff including
project.clj. If you run `lein repl` inside the myproject folder, it
will have access to whatever libraries you've added to project.clj.

Example:

> lein new math
Generating a project called math based on the 'default' template.
To see other templates (app, lein plugin, etc), try `lein help new`.
> cd math
> cat project.clj
(defproject math "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]])

See :dependencies? That's where you'll add new libraries. You asked
about clojure.contrib.math but that is old and no longer maintained,
so for contrib libraries, consult this page:

http://dev.clojure.org/display/community/Where+Did+Clojure.Contrib+Go

(for other non-core/non-contrib libraries, you would search
http://clojars.org as Marshall indicated)

You'll see:

* clojure.contrib.math
* Migrated to clojure.math.numeric-tower - lead Mark Engelberg.
* Status: latest build status, latest release on Maven, report bugs.

And if you go to https://github.com/clojure/math.numeric-tower/ (which
is where clojure.math.numeric-tower links to) you'll see:

Leiningen dependency information:

[org.clojure/math.numeric-tower "0.0.2"]

So we'll edit project.clj to include that... and get:

> cat project.clj
(defproject math "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/math.numeric-tower "0.0.2"]])

Be careful about the brackets!

Now we'll start a repl:

> lein repl
...
user=> (require '[clojure.math.numeric-tower :as math])
nil
user=> (math/sqrt 1234)
35.12833614050059
user=>

Hope that helps?
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Qiu Xiafei

unread,
Nov 7, 2013, 10:27:18 PM11/7/13
to clo...@googlegroups.com
you may have a look at alembic: https://github.com/pallet/alembic
Reply all
Reply to author
Forward
0 new messages