可以混合使用 JVM 语言吗? 即:Groovy & 克洛尤尔

发布于 2024-07-13 20:05:42 字数 153 浏览 11 评论 0原文

我知道你可以轻松地混合groovy&java,clojure&java,无论什么JvmLang&java。

这是否也意味着我也可以让 clojure 和 groovy 代码进行交互? 如果我使用 Grails 或 jRoR,我也可以在该环境中使用 clojure 吗?

I understand that you can easily mix groovy&java, clojure&java, whateverJvmLang&java.

Does this also mean I can have clojure and groovy code interact as well? If I use Grails or jRoR, can I also make use of clojure in that environment?

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

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

发布评论

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

评论(6

泼猴你往哪里跑 2024-07-20 20:05:42

只要所讨论的语言实际上生成 Java 类(即:Java 平台已知的类,不一定用 Java 语言编写),那么是的:它们可以轻松地相互交互。

至少对于 Groovy,我知道完全有可能编写可以从“普通”Java 代码中使用的类。

As long as the languages in question actually produce Java classes (that is: classes that are known to the Java platform, not necessarily written in the Java language), then yes: they can easily interact with each other.

At least for Groovy I know that it's entirely possible to write classes that can be used from "normal" Java code.

所有深爱都是秘密 2024-07-20 20:05:42

Clojure 可以通过 gen-class 编译为 .class 文件,并像任何其他代码一样从 Java 中使用; 请参阅此处

Clojure can be compiled to .class files via gen-class and used from Java like any other code; see here.

人间☆小暴躁 2024-07-20 20:05:42

There is also an example on how you can call Clojure code from JRuby.

遥远的绿洲 2024-07-20 20:05:42

正如已经提到的,Clojure 可以轻松地与 Java 交互。 尽管您看到的许多示例都展示了如何从 REPL 执行此操作,但一旦将 Clojure 或任何其他 JVM 语言编译为类或 jar 文件,就可以像任何其他类一样从 Java 调用它。

就我而言,困难(不是很大)是弄清楚如何正确使用gen-class。 有一小部分用 Clojure 编写的统计函数,位于 http://kenai.com/projects/binomialstats 这说明了如何做到这一点。 该 Clojure 库由 Java 项目——signtest 使用。 (抱歉,由于我是新人,StackOverflow 不允许我发布多个链接。)

如果某些 IDE 能够在同一项目中处理不同的语言,那就太好了。 例如,上述两个项目作为两个单独的 NetBeans 项目进行维护。 确保这两个部分始终同步可能需要一些额外的工作。

As has been mentioned, Clojure can interact easily with Java. Although many of the examples you see show how to do it from the REPL, once you compile Clojure, or any other JVM language, to a class or jar file, it can be called from Java just like any other class.

In my case, the difficulty (not much of one) was figuring out how to use gen-class correctly. There is a small set of statistical functions written in Clojure at http://kenai.com/projects/binomialstats that illustrates how to do it. That Clojure library is used by a Java project -- signtest. (Sorry, since I'm new, StackOverflow won't let me post multiple links.)

What would really be nice now is if some of the IDEs could deal with different languages in the same project. The two projects mentioned above are maintained as two individual NetBeans projects for example. Assuring that the two parts are always synchronized can be a bit of extra work.

╰沐子 2024-07-20 20:05:42

正如已经提到的,您可以通过预编译从 Groovy 访问 Clojure 类。

您还可以通过 Java 6 的脚本 API 访问最流行的脚本语言,但 Scala 和 Clojure 似乎并未得到官方支持。 以下是 Groovy 的示例:

http://groovy.codehaus。 org/JSR-223+access+to+other+JVM+语言

您还可以从 Groovy 访问 Clojure 的类,例如(对于 Groovy 1.7 快照):

@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.*

def ss = StringSeq.create('The quick brown fox')
def done = false
while (!done) {
  println ss.first()
  ss = ss.next()
  done = !ss
}

或者通过创建新进程进行交互(同样对于 Groovy 1.7 )快照):

@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.Script

def src = new File('temp.clj')
src.text = '''
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))
(println (factorial 4))
'''
def path = System.getProperty('user.home') + '/.groovy/grapes/org.clojure/clojure/jars/clojure-1.0.0.jar'
new AntBuilder().with {
    java(classname:Script.name, classpath:path) {
        arg(value:src.path)
    }
}

还有一个 Grails 的 Clojure 插件,它可以轻松访问从任何 Grails 工件(控制器、标签库、服务等...)执行 clojure 代码:

http://grails.org/plugin/clojure

As already mentioned, you can access Clojure classes from Groovy via pre-compilation.

You can access most popular scripting languages via Java 6's scripting API as well, though Scala and Clojure don't seem to be officially supported. Here are examples from Groovy:

http://groovy.codehaus.org/JSR-223+access+to+other+JVM+languages

You can also get access to Clojure's classes from Groovy, e.g. (for Groovy 1.7 snapshot):

@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.*

def ss = StringSeq.create('The quick brown fox')
def done = false
while (!done) {
  println ss.first()
  ss = ss.next()
  done = !ss
}

Or interact via creating a new Process (again for Groovy 1.7 snapshot):

@Grab(group='org.clojure', module='clojure', version='1.0.0')
import clojure.lang.Script

def src = new File('temp.clj')
src.text = '''
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))
(println (factorial 4))
'''
def path = System.getProperty('user.home') + '/.groovy/grapes/org.clojure/clojure/jars/clojure-1.0.0.jar'
new AntBuilder().with {
    java(classname:Script.name, classpath:path) {
        arg(value:src.path)
    }
}

There is also a Clojure plugin for Grails which provides easy access to execute clojure code from any Grails artifact (controllers, taglibs, services etc...):

http : / / grails.org/plugin/clojure

江心雾 2024-07-20 20:05:42

另一个有助于在 JVM 上混合语言的环境是 WebSphere sMash(又名 Project Zero)。 它包括:

Another environment which facilitates mixing languages on the JVM is WebSphere sMash (aka Project Zero). It includes:

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