从 Java 向 Clojure 传递参数

发布于 2024-09-06 02:10:50 字数 631 浏览 4 评论 0原文

我想将 Clojure 代码嵌入到 Java 中。 站点对于设置此功能的基础知识很有帮助,但唯一的参数它曾经传递的是 String 类型。我也尝试过使用整数,它们也有效。

我的问题是是否有某种格式化方法可以将结构化数据传递给 Clojure。特别是,我有一个想要传递给 Clojure 的点列表,并将其转换为一个如下所示的向量:

[[1 2] [3 4] [5 6]]

执行此操作的最简单方法是什么?我是否可以在 Java 端进行预处理,或者我应该在 Clojure 端进行后处理,或者 Clojure 中有什么东西可以处理这个问题?我怀疑它会将一串数字和每个元组的长度传递给 Clojure,并让它将字符串处理为向量。然而,Clojure 这方面的例子并不多,我很好奇我是否遗漏了一些明显的东西。

编辑:如果您对传递 Java 对象感兴趣,请查看 mikera 的答案。如果您只想提前将数据格式化为集合/地图/等的 Clojure 格式,请查看下面我的答案。

I would like to embed Clojure code into Java. This site was helpful in the basics of setting this up, but the only arg it ever passes is of type String. I have tried using ints as well, and those also work.

My question is whether there is some formatted way to pass in structured data to Clojure. In particular, I have a list of points I would like to pass to Clojure and turn into a vector that would look something like this:

[[1 2] [3 4] [5 6]]

What is the easiest way to go about doing this? Is there preprocessing I can do on Java's end, or should I do postprocessing on Clojure's end, or is there something in Clojure that will handle this? I suspect it's passing in a String of numbers and the length of each tuple to Clojure, and letting it process the String into a vector. However, this aspect of Clojure doesn't have many examples, and I'm curious if I'm missing something obvious.

EDIT: Please look at mikera's answer is you're interested in passing in Java Objects. Please look at my answer below if you just want to format your data ahead of time into a Clojure format for a set/map/etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2024-09-13 02:10:50

这在一定程度上取决于数据的开始格式,但您可能会发现最简单的方法是直接传递表示数据的 Java 对象并使用 Clojure 的 Java 互操作性功能读取它。

例如,您可以直接传递 Java Points 数组并执行以下操作:

(let [point (aget some-array index)]
  (do-stuff-with-point point))

It depends a bit on what format your data is in to start with, but you may find it easiest just to pass the Java object that represents the data directly and read it using Clojure's Java interoperability features.

e.g. you could pass a Java array of Points directly and do something like:

(let [point (aget some-array index)]
  (do-stuff-with-point point))
意中人 2024-09-13 02:10:50

对于那些情况,当您想要传递一个已格式化为类似于 Java 中的 Clojure 的简单数据结构时,您可以将该 arg 作为字符串传递。因此,对于我在问题中给出的示例,我将

"[[1 2] [3 4] [5 6]]"

作为我的参数传递。当您使用 invoke(arg) 调用 Clojure 时,您可以使函数的第一步成为对 arg 上的 readString 的调用:

(defn foo [d]
  (def data (read-string d)))

当传入示例 String 时,上面将生成一个向量。

For those cases when you want to pass in a simple data structure that you've already formatted to look like Clojure in Java, you can pass in that arg as a string. So for the example I gave in my question, I would pass in

"[[1 2] [3 4] [5 6]]"

as my arg. When you've called Clojure using invoke(arg), you can make the first step of your function to be a call to readString on your arg:

(defn foo [d]
  (def data (read-string d)))

The above will produce a vector when the example String is passed in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文