Skip to content
Write Java inside Clojure
Clojure
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/tailrecursion
test/tailrecursion
.gitignore
.travis.yml
README.md
project.clj

README.md

javastar

Write Java inside Clojure:

(require '[tailrecursion.javastar :refer [java*]])

(defn sum-doubles [arr]
  (java* [] double [doubles]
    "double s = 0;
     double[] arr = ~{};
     for(int i = 0; i < arr.length; i++) {
       s += arr[i];
     }
     return s;"
     arr))

(sum-doubles (double-array 10 1.0)) ;=> 10.0

(defn hi-from-java [name]
  (java* [] String [String] "return \"hi \" + ~{} + \"!\";" name))

(hi-from-java "Bob") ;=> "hi Bob!"

(defn add2 [x y]
  (java* [] long [long long] "return ~{} + ~{};" x y))

(add2 1 2) ;=> 3

(java* [clojure.lang.Var clojure.lang.RT]
       Object [String String]
       "Var str = RT.var(\"clojure.core\",\"str\");
        return str.invoke(~{},\" \", ~{}, \"!\");"
       "holy"
       "cow") ;=> "holy cow!"

Requires Java 1.6 JDK or higher.

Dependency Build Status

[tailrecursion/javastar "1.1.6"]

License

Copyright © 2013 Alan Dipert

Distributed under the Eclipse Public License, the same as Clojure.

You can’t perform that action at this time.