use of clojure.lang.IFn in project enumerable by hraberg.
the class ClojureTest method interactingWithClojure.
@Test
public void interactingWithClojure() throws Exception {
eval("(def v [1 2 3 4 5])");
IPersistentVector v = eval("v");
IFn times = defn("times", fn(n, m, n * m));
Integer factorial = 120;
assertEquals(factorial, (reduce(times, 1, v)));
assertEquals(factorial, eval(reduce, times, 1, v));
assertEquals(factorial, eval("(reduce times 1 v)"));
IFn isOdd = eval("odd?");
ISeq odd = list(1L, 3L, 5L);
assertEquals(odd, (filter(isOdd, v)));
assertEquals(odd, eval(filter, isOdd, v));
assertEquals(odd, eval("(filter odd? v)"));
IFn isEven = defn("is-even?", toIFn(Lambda.λ(n, n % 2 == 0)));
ISeq even = list(2L, 4L);
assertEquals(even, (filter(isEven, v)));
assertEquals(even, eval(filter, isEven, v));
assertEquals(even, eval("(filter is-even? v)"));
}
use of clojure.lang.IFn in project enumerable by hraberg.
the class ClojureTest method convertedFnToIFnThrowsArityWhenCalledWithTooFewArguments.
@Test(expected = IllegalArgumentException.class)
public void convertedFnToIFnThrowsArityWhenCalledWithTooFewArguments() throws Exception {
IFn fn = toIFn(Lambda.λ(s, s.toUpperCase()));
fn.invoke();
}
use of clojure.lang.IFn in project enumerable by hraberg.
the class ClojureTest method convertFnToIFn.
@Test
public void convertFnToIFn() throws Exception {
IFn fn = toIFn(Lambda.λ(s, s.toUpperCase()));
assertEquals("HELLO", fn.invoke("hello"));
}
use of clojure.lang.IFn in project enumerable by hraberg.
the class ClojureTest method convertFnToIFnHandlesWithMeta.
@Test
public void convertFnToIFnHandlesWithMeta() throws Exception {
IFn fn = (IFn) toIFn(Lambda.λ(s = "world", s.toUpperCase()));
defn("to-upper-case", fn);
IFn fnWithMeta = (IFn) clj.eval("(with-meta to-upper-case {:hello \"world\"})");
assertNotSame(fn, fnWithMeta);
defn("to-upper-case-with-meta", fnWithMeta);
IPersistentMap meta = (IPersistentMap) clj.eval("(meta to-upper-case-with-meta)");
assertEquals(1, meta.count());
assertEquals("world", meta.valAt(clj.eval(":hello")));
assertEquals("HELLO", fnWithMeta.invoke("hello"));
assertEquals("WORLD", fnWithMeta.invoke());
}
use of clojure.lang.IFn in project enumerable by hraberg.
the class ScalaTest method interactingWithClojure.
@Test
public void interactingWithClojure() throws Exception {
IFn star = (IFn) ClojureTest.getClojureEngine().eval("*");
Function2<Object, Object, Object> times = toFunction(LambdaClojure.toFn2(star));
assertEquals(6L, times.apply(2, 3));
scala.bind("timesClojure", "Function2[Any, Any, Any]", times);
assertEquals(120L, scala.eval("List(1, 2, 3, 4, 5).reduceLeft(timesClojure)"));
}
Aggregations