Java-Clojure interop in REPL: 'require versus' imports Java class

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

I run Clojure REPL through lein repl in the clatrix directory, whose project.clj indicates a dependency on org.jblas . (This is the importance of clathrice for my question.)

I can use import jblas classes, but I would like require 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, not Java? Am I making a punctuation mistake?

Rationale: if I could org.jblas.Solve be require d and superimposed, say, S (as an example), I could just do (S/solveLeastSquares foo bar) , which I find better than dot-spatial notation. In addition, slash notation is used throughout the entire Clatrix source code, and it would be nice to use this also when experimenting in REPL to facilitate copy-paste.

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

+4
source share
1 answer

Require and import non-overlapping uses:

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

I don’t think that at the moment there is a way for the alias of the java package, the best way is to import it, as you do, using Solve to avoid entering the package name. You cannot use import, give it a different package name.

+4
source

Source: https://habr.com/ru/post/1485873/


All Articles