简体   繁体   English

将向量,序列和映射从Clojure返回到Java

[英]Returning Vectors, Sequences and Maps from Clojure to Java

I've read the excellent answer to Calling clojure from java which shows the new style of calling Clojure from Java. 我读过了绝佳的答案从Java调用的Clojure这表明从Java调用Clojure的新风格。

But the example given just returns a float. 但是给出的示例仅返回浮点数。 How can I return Clojure Vectors and other Sequence types from Clojure to Java? 如何将Clojure向量和其他序列类型从Clojure返回到Java? And Maps? 和地图?

Specifically in this gen-class : 特别是在这个gen-class中:

(ns dummy-app.core
  (:gen-class
   :name com.example.dummy
   :methods [#^{:static true} [f [int] int]
             #^{:static true} [getVect [] XXXX]
             #^{:static true} [getMap [] YYYY]
             #^{:static true} [getSeq [] ZZZZ]
             ] ))

what should I put for XXXX, YYYY and ZZZZ? 我应该为XXXX,YYYY和ZZZZ放什么? And what types should they be in my Java program? 它们在我的Java程序中应该是什么类型?

Update : in response to Tomo's promising answer, if I try to just use IPersistentVector in the gen-clas I get 更新:为了响应Tomo的有前途的答案,如果我尝试仅在gen-clas中使用IPersistentVector,我会得到

Caused by: java.lang.ClassNotFoundException: java.lang.IPersistentVector

when I try to create the Uberjar. 当我尝试创建Uberjar时。 I assume there's something else I need to do to import these types? 我认为我还需要做其他导入这些类型的事情吗?

Update 2 : OK, thanks to more comments from Tomo this seems to work : 更新2:好的,感谢Tomo的更多评论,这似乎可行:

(ns dummy-app.core
  (import clojure.lang.IPersistentVector)
  (:gen-class
   :name com.example.dummy
   :methods [#^{:static true} [f [int] int]
             #^{:static true} [getVect [] clojure.lang.IPersistentVector]
             ] ))

I'd use appropriate interfaces: IPersistentMap , IPersistentVector , ISeq and so on. 我将使用适当的接口: IPersistentMapIPersistentVectorISeq等。 Check out the source code ( https://github.com/clojure/clojure/tree/master/src/jvm/clojure/lang ). 查看源代码( https://github.com/clojure/clojure/tree/master/src/jvm/clojure/lang )。

You can also just specify the appropriate Java interfaces. 您也可以只指定适当的Java接口。

Clojure vectors, list and seqs all implement java.util.List Clojure向量,列表和序列均实现java.util.List

Clojure maps implement java.util.Map Clojure映射实现java.util.Map

Functions implement both java.util.concurrent.Callable and java.lang.Runnable 函数同时实现java.util.concurrent.Callablejava.lang.Runnable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM