Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using clojure.math.numeric-tower, or any library

Tags:

I try to learn a bit of Clojure because the language looks nice. But it seems that there is no info anywhere on how to install/use libraries like for example the clojure.math.numeric-tower.

For now I run the REPL by typing this in my Linux shell:

java -cp ~/Clojure/clojure-1.3.0/clojure-1.3.0.jar  clojure.main

I downloaded the numeric-tower jar, and put it everywhere. I have modified the -cp option in every possible way. I have put the numeric_tower.clj file everywhere. It still doesn't work.

What's the way to use the libraries?

like image 724
John Smith Optional Avatar asked Jan 03 '12 00:01

John Smith Optional


2 Answers

I had this same issue yesterday (also as a newcomer to Clojure). From my understanding, between Clojure 1.2 and 1.3 they split out many of the contrib libraries into separate projects so that they could be managed more easily. And shrink the core of what Clojure is.

The easiest way to accomplish what you're trying to do is through leiningen (I got this answer from technomancy in the #clojure IRC channel yesterday evening). The recommendation is to create a "playground" project using leiningen that you can play around in and learn Clojure.

So, create a playground project with:

lein new playground

Modify the project.clj file to include:

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

as a dependency. I was told that http://search.maven.org/ is the easiest way to find out which is the most up-to-date version of a particular library.

Then, run

lein deps

to pull in the jars. Now you're ready to

lein repl

to get going.

Once the repl is started, pull in numeric-tower:

(require '[clojure.math.numeric-tower :as math])

Then you can do your expt call:

(math/expt 4 6) ;; yields 4096

Hope that helps!

like image 105
bitops Avatar answered Sep 26 '22 02:09

bitops


Thanks to all. It works now. In fact the problem that really confused me was that java didn't understand the ~ (linux home) symbol after a colon in the classpath. I was doing

java -cp ~/Clojure/clojure-1.3.0/clojure-1.3.0.jar:~/Clojure/lib  clojure.main

and java doesn't understand that.

However, if you do

java -cp ~/Clojure/clojure-1.3.0/clojure-1.3.0.jar:/home/username/Clojure/lib  clojure.main

it works.

Strange behavior of the classpath variable.

I would have used lein, but since I'm considering doing some programming for android, I thought it would be better if I could understand how all this work with java only. Unless lein can package android project as well?

Anyway, thanks. All three replies were very informative.

like image 36
John Smith Optional Avatar answered Sep 27 '22 02:09

John Smith Optional