【问题标题】:Clojure and Java interoperation: Java.math.BigIntegerClojure 和 Java 互操作:Java.math.BigInteger
【发布时间】:2013-11-18 21:07:18
【问题描述】:

我有一个输出 BigInteger 的 clojure 代码。

(ns com.domain.tiny
  (:gen-class
    :name com.domain.tiny
    :methods [#^{:static true} [binomial [int int] java.math.BigInteger]]))

(defn binomial
  "Calculate the binomial coefficient."
  [n k]
  (let [a (inc n)]
    (loop [b 1
           c 1]
      (if (> b k)
        c
        (recur (inc b) (* (/ (- a b) b) c))))))

(defn -binomial
  "A Java-callable wrapper around the 'binomial' function."
  [n k]
  (binomial n k))

(defn -main []
  (println (str "(binomial 5 3): " (binomial 5 3)))
  (println (str "(binomial 10042 111): " (binomial 10042 111)))
)

单独执行,我可以毫无问题地得到结果:

(binomial 5 3): 10
(binomial 10042 111): 
4906838957506814494663377752836616342 ...
48178314846156008309671682804824359157818666487159757179543983405334334410427200

我可以用lein uberjar 生成jar 文件。尝试从 Java 中使用它,我想出了这段代码。

import com.domain.tiny;
import java.math.BigInteger;
public class Hello
{
    public static void main(String[] args) {
        BigInteger res = tiny.binomial(5, 3);
        System.out.println("(binomial 5 3): " + res);
        res = tiny.binomial(10042, 111);
        System.out.println("(binomial 10042, 111): " + res);
    }
}

不幸的是,我遇到了异常。

Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast 
    to java.math.BigInteger
at com.domain.tiny.binomial(Unknown Source)
at Hello.main(Hello.java:11)

如何为 Java.Math.BigInteger 互操作 clojure 和 Java?我可以使用:methods [#^{:static true} [binomial [int int] double]]))double res = tiny.binomial(10042, 111);,但不能使用BigInteger。

这是我获取 jar 并执行 java 的步骤。

lein new com.domain.tiny
copy the tiny.clj in com.domain
lein deps
lein uberjar
javac -cp .:com.domain.tiny-1.0.0-SNAPSHOT-standalone.jar Hello.java
java -cp .:com.domain.tiny-1.0.0-SNAPSHOT-standalone.jar Hello

clojure 代码复制自:Calling clojure from java

【问题讨论】:

  • Clojure 太酷了。
  • 您是否尝试过实际返回 java.math.BigInteger? (java.math.BigInteger."123123123")

标签: java clojure interop


【解决方案1】:

返回值应该是 clojure.lang.BigInt。

Clojure 代码

:methods [#^{:static true} [binomial [int int] clojure.lang.BigInt]]))
(defn binomial
  "Calculate the binomial coefficient."
  [n k]
  (let [a (inc n)]
    (loop [b 1
           c 1]
      (if (> b k)
        (bigint c) ;; <-- Without this code 
                   ;; java.lang.ClassCastException: 
                   ;; java.lang.Long cannot be cast to clojure.lang.BigInt occurs
        (recur (inc b) (* (/ (- a b) b) c))))))

Java 代码

import clojure.lang.BigInt;
...
BigInt res = tiny.binomial(10042, 111);
System.out.println("(binomial 10042, 111): " + res.toString());

结果

java -cp .:com.domain.tiny-1.0.0-SNAPSHOT-standalone.jar Hello
>>> 
(binomial 5 3): 10
(binomial 10042 111): 49068389575068144946 … 27200

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2012-08-09
    • 2014-03-31
    相关资源
    最近更新 更多