Created
April 8, 2022 04:06
-
-
Save zhuzhonghua/553e54a2a8d541947acac959bf1fdca6 to your computer and use it in GitHub Desktop.
Clojure Util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import clojure.lang.Compiler; | |
import clojure.lang.RT; | |
import clojure.lang.Symbol; | |
import clojure.lang.Var; | |
public class ClojureUtil { | |
public static void main() { | |
Var call = RT.var("clojure.main", "main"); | |
call.invoke(); | |
} | |
public static void require(String ns) { | |
Var call = RT.var("clojure.core", "require"); | |
call.invoke(Symbol.intern(ns)); | |
} | |
/** | |
* 从ClassPath加载file | |
* @param file | |
*/ | |
public static void load(String file) { | |
try { | |
RT.loadResourceScript(file); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
/** | |
* 执行一段Clojure脚本 | |
* @param expr | |
*/ | |
public static void eval(String expr) { | |
try { | |
Compiler.eval(RT.readString(expr)); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
/** | |
* 调用某个ns的某个方法,使用可变参数 | |
* @param ns | |
* @param func | |
* @param params | |
*/ | |
public static void call(String ns, String func, Object... params) { | |
Var call = RT.var(ns, func); | |
call.applyTo(RT.seq(params)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment