如何访问clojure函数在Java? [英] How to access clojure functions in Java?

查看:179
本文介绍了如何访问clojure函数在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Clojure的新手。我试图在Clojure中构建我的计算单元(纯函数)并将所有这些函数绑定到一个使用Java的程序中。

I am quite a newbie to Clojure. I am trying to build my computational units (pure functions) in Clojure and bind all these functions into a program using Java.

对于在Java中访问Clojure,我做了一个事物即时间编译和类生成。但它看起来很麻烦,削弱了在我的应用程序中使用Clojure的想法。所以我的问题是有谁试图访问Java中的Clojure函数(不包括类生成和AOT编译)?如果不是,那么如何使用纯Clojure将这些计算单元(或Clojure函数)互连到一个程序(其中有几个方法互相链接)?

For accessing Clojure in Java, I have done one thing i.e. Ahead-of-time compilation and class generation. But it looks cumbersome and weakens the idea of using Clojure into my application. So my question is have anyone tried to access Clojure functions in Java (excluding class generation and AOT compilation)? If not, then how to interlink these computational units (or Clojure functions) into a program (where there are several methods interlinked with each other) using purely Clojure?

推荐答案

只是作为概述一般流程是:


  • 包括clojure运行时:

    import clojure.lang.RT ;

  • 使用运行时加载命名空间(将编译它):

    RT.loadResourceScript path / core.clj);

  • 获取要调用的函数的iFn对象:

    RT.var(mynamespace.core,main)

  • 并调用 invoke 对该对象的方法

  • include the clojure runtime:
    import clojure.lang.RT;
  • use the runtime to load your namespace (which will compile it):
    RT.loadResourceScript("path/core.clj");
  • get the iFn Object for the function you would like to call:
    RT.var("mynamespace.core", "main")
  • and call the invoke method on that object

这篇关于如何访问clojure函数在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆