4
votes

This question involves the clatrix Clojure library [1] as well as the jblas Java library [2] (the former partially wraps the latter).

I start the Clojure REPL via lein repl in the clatrix directory, whose project.clj specifies a dependency on org.jblas. (This is the extent of clatrix's relevance to my question.)

I'm able to import jblas' classes but I'd like to require them instead.

user> (import '[org.jblas DoubleMatrix Solve])
org.jblas.Solve

user> (. Solve solveLeastSquares (. DoubleMatrix rand 2 2) (. DoubleMatrix rand 2 1))
#<DoubleMatrix [1.965810; -1.044592]>

user> (require '[org.jblas Solve])
FileNotFoundException Could not locate org/jblas/Solve__init.class or org/jblas/Solve.clj on classpath:   clojure.lang.RT.load (RT.java:432)

Can I only require Clojure libraries and not Java ones? Am I making a punctuation error?

The rationale: if I could get org.jblas.Solve to be required and aliased to, say, S (just as an example), I could simply do (S/solveLeastSquares foo bar) which I find better than the dot-space notation. Also, the slash notation is used all over clatrix's source code and it'd be nice to use that also while experimenting in the REPL to ease copy-paste.

[1] See https://github.com/tel/clatrix [2] Especially its Solve class: https://github.com/mikiobraun/jblas/blob/master/src/main/java/org/jblas/Solve.java#L44

1
An equivalent and more concise call to solveLeastSquares: (Solve/solveLeastSquares foo bar). - Ahmed Fasih

1 Answers

4
votes

Require and import have non overlaping uses:

  • Require is only for Clojure namespaces.
  • import is only for java classes

I don't think there is a way to alias a java package at the moment, the best you can do is import it as you are doing with Solve to avoid typing the package name. You can't using import, give it a different package name.