forked from clojure/clojure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClojure.java
More file actions
102 lines (94 loc) · 3.19 KB
/
Clojure.java
File metadata and controls
102 lines (94 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Copyright (c) Rich Hickey and Contributors. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
package clojure.java.api;
import clojure.lang.IFn;
import clojure.lang.Symbol;
import clojure.lang.Var;
import clojure.lang.RT;
/**
* <p>The Clojure class provides a minimal interface to bootstrap Clojure access
* from other JVM languages. It provides:</p>
*
* <ol>
* <li>The ability to use Clojure's namespaces to locate an arbitrary
* <a href="http://clojure.org/vars">var</a>, returning the
* var's {@link clojure.lang.IFn} interface.</li>
* <li>A convenience method <code>read</code> for reading data using
* Clojure's edn reader</li>
* </ol>
*
* <p>To lookup and call a Clojure function:</p>
*
* <pre>
* IFn plus = Clojure.var("clojure.core", "+");
* plus.invoke(1, 2);</pre>
*
* <p>Functions in <code>clojure.core</code> are automatically loaded. Other
* namespaces can be loaded via <code>require</code>:</p>
*
* <pre>
* IFn require = Clojure.var("clojure.core", "require");
* require.invoke(Clojure.read("clojure.set"));</pre>
*
* <p><code>IFn</code>s can be passed to higher order functions, e.g. the
* example below passes <code>inc</code> to <code>map</code>:</p>
*
* <pre>
* IFn map = Clojure.var("clojure.core", "map");
* IFn inc = Clojure.var("clojure.core", "inc");
* map.invoke(inc, Clojure.read("[1 2 3]"));</pre>
*/
public class Clojure {
private Clojure() {}
private static Symbol asSym(Object o) {
Symbol s;
if (o instanceof String) {
s = Symbol.intern((String) o);
} else {
s = (Symbol) o;
}
return s;
}
/**
* Returns the var associated with qualifiedName.
*
* @param qualifiedName a String or clojure.lang.Symbol
* @return a clojure.lang.IFn
*/
public static IFn var(Object qualifiedName) {
Symbol s = asSym(qualifiedName);
return var(s.getNamespace(), s.getName());
}
/**
* Returns an IFn associated with the namespace and name.
*
* @param ns a String or clojure.lang.Symbol
* @param name a String or clojure.lang.Symbol
* @return a clojure.lang.IFn
*/
public static IFn var(Object ns, Object name) {
return Var.intern(asSym(ns), asSym(name));
}
/**
* Read one object from the String s. Reads data in the
* <a href="http://edn-format.org">edn format</a>.
* @param s a String
* @return an Object, or nil.
*/
public static Object read(String s) {
return EDN_READ_STRING.invoke(s);
}
static {
RT.init();
Symbol edn = (Symbol) var("clojure.core", "symbol").invoke("clojure.edn");
var("clojure.core", "require").invoke(edn);
}
private static final IFn EDN_READ_STRING = var("clojure.edn", "read-string");
}