gpt4 book ai didi

java - 如何从 Java 调用 Clojure 宏?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:54:29 33 4
gpt4 key购买 nike

有没有办法从 Java 调用 Clojure 宏?

这是我正在尝试做的事情:

RT.var("clojure.core", "require").invoke(Symbol.create("clojure.contrib.prxml"));
Var prxml = RT.var("clojure.contrib.prxml", "prxml");
Var withOutStr = RT.var("clojure.core", "with-out-str");
String stringXML = (String) withOutStr.invoke((prxml.invoke("[:Name \"Bob\"]")));

prxml 默认写入 *out*,这就是为什么我需要用返回字符串的宏 with-out-str 包装它。

我收到这个错误:

 [java] java.lang.IllegalArgumentException: Wrong number of args (1) passed to: core$with-out-str
[java] at clojure.lang.AFn.throwArity(AFn.java:437)
[java] at clojure.lang.RestFn.invoke(RestFn.java:412)
[java] at clojure.lang.Var.invoke(Var.java:365)
[java] at JavaClojure.xml.main(Unknown Source)

最佳答案

你必须自己动手做 withOutStr。

class YourClass {
static final Var withBindings = RT.var("clojure.core", "with-bindings*");
static final Var list = RT.var("clojure.core", "list*");
static final Var out = RT.var("clojure.core", "*out*");
static final Var prxml = RT.var("clojure.contrib.prxml", "prxml");

static String withOutStr(IFn f, Object args...) {
StringWriter wtr = new StringWriter();
withBindings.applyTo(list.invoke(RT.map(out, wtr), f, args));
return wtr.toString();
}

...

String stringXML = withOutStr(prxml, "[:Name \"Bob\"]");
}

关于java - 如何从 Java 调用 Clojure 宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6672934/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com