14

I'm new to Clojure and Leiningen, and I've determined that some of what I'll want to use is located in clojure.contrib.generic.math-functions. I found API information for that at http://richhickey.github.com/clojure-contrib/branch-1.1.x/math-api.html, but I can't find anything that helps me figure out what I should put into my project.clj file for that dependency.

I have tried [clojure.contrib.generic.math-functions "1.1"], [clojure.contrib.generic.math-functions "1.1.x"], and [clojure.contrib.generic.math-functions "1.1.0"]. For each of those, I get something like...

...
Caused by: org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException: Missing:
----------
1) clojure.contrib.generic.math-functions:clojure.contrib.generic.math-functions:jar:1.1
Steve Jorgensen
  • 11,725
  • 1
  • 33
  • 43
  • Here's the correct link for the latest stable version's documentation for that library: http://clojure.github.com/clojure-contrib/generic.math-functions-api.html – Sean Corfield May 07 '11 at 01:57

4 Answers4

11

All clojure-contrib namespaces are shipped within a single jar file, for which the dependency has to be listed like:

[org.clojure/clojure-contrib "1.2.0"]

Please note that there are different versions available of that artifact. The 1.2.0 is the current stable release.

In order to use functions coming from the math-functions namespace in your clojure code, you need to either require or use such namespace, usually done within the ns form at the beginning of your source file:

(ns my.namespace
    (:use [clojure.contrib.generic.math-functions]))

Have a look here to see the differences between use and require.

Community
  • 1
  • 1
skuro
  • 13,414
  • 1
  • 48
  • 67
  • Thanks. My `lein deps` works correctly now, using your example. Where did you look find that version number though? Is it from the "Switch Tags" drop-down at https://github.com/richhickey/clojure-contrib ? – Steve Jorgensen May 06 '11 at 22:17
  • as per the other comments in this thread, the current stable release is 1.2.0, I updated my post to reflect that. Other available versions can be seen [here](http://build.clojure.org/releases/org/clojure/clojure-contrib/) – skuro May 07 '11 at 07:50
  • Don't use the richhickey repo. All active development has been at https://github.com/clojure/clojure for a long time, but the richhickey stuff still turns up first on google because it's linked to a lot. – amalloy May 09 '11 at 05:48
9

The next version of Leiningen will have a search task for precisely this purpose. It will search Clojars, Maven Central, and any other repositories your project has listed, provided they offer up downloadable indices. It's already implemented, so if you run Leiningen from git you can use it.

Also, the Leiningen tutorial covers this. Type "lein help tutorial".

5

You can generally find what you need at clojars.org - it's the default repository for leiningen. The current stable release of Clojure is 1.2.0, so you'd have this in your leiningen project.clj:

[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]

To use the generic math functions in your clojure, require or use it in your namespace declaration at the top of your source file:

(ns your-namespace
    (:use [clojure.contrib.generic.math-functions :as mathf]))

This allows you to refer to the functions in that namespace like this:

(mathf/abs -10) ;; => 10

:use-ing namespaces with :as is the preferred way to use functions from other namespaces in your code. require is ok, but you'd have to prefix your functions with the entire namespace (e.g. clojure.contrib.generic.math-functions/abs) so that's not practical. Using a namespace without :as allows you to use these functions without any prefix at all (e.g. abs), but you're more likely to get namespace clashes and it might be difficult to see where functions come from, especially if you :use many libraries.

You can browse all libraries available from the default leiningen repository by checking out http://clojars.org/repo/. The structure of clojure-contrib will change when 1.3.0 is out, so you'll have to include the specific contrib library if you're using version 1.3.0-alpha-xx:

[org.clojure.contrib/generic "1.3.0-alpha4"]
Gert
  • 3,839
  • 19
  • 22
  • Thanks for the clojars.org link. That helps a lot! – Steve Jorgensen May 06 '11 at 22:43
  • 1
    The current stable release of Clojure is 1.2.1 but Contrib is still at 1.2.0. The "richhickey" repo is outdated - everything is managed from the "clojure" organization now: https://github.com/clojure - unfortunately there are still a lot of old links out there in Google :( – Sean Corfield May 07 '11 at 01:52
  • Also note that Clojure 1.3.0 is in development and Contrib is being reorganized / updated so there will no longer be a single monolithic Contrib library. See this page for more info: http://dev.clojure.org/display/design/Contrib+Library+Names – Sean Corfield May 07 '11 at 01:56
  • Yes, there's a lot at clojars.org, but not everything--some of what one might want is at maven.org, and some of it is in neither place. Both clojars and maven include lots of older versions of libraries that this or that person has uploaded (and possibly hacked in ways that you don't necessariily want). As far as I can tell, the answer to OP's question is: Search clojars.org, search maven.org, google it, guess--and good luck! – Mars Apr 20 '15 at 18:52
2

Now that the clojure.contrib has been broken up, the math functions are in something called math.numeric-tower. The lein dependency is specified like this:

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

You can use or require as seems appropriate, for example

(use '[clojure.math.numeric-tower])

Rick Hall
  • 137
  • 3