Search in sources :

Example 16 with IFn

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)"));
}
Also used : IFn(clojure.lang.IFn) ISeq(clojure.lang.ISeq) IPersistentVector(clojure.lang.IPersistentVector) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 17 with IFn

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();
}
Also used : IFn(clojure.lang.IFn) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 18 with IFn

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"));
}
Also used : IFn(clojure.lang.IFn) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 19 with IFn

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());
}
Also used : IFn(clojure.lang.IFn) IPersistentMap(clojure.lang.IPersistentMap) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 20 with IFn

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)"));
}
Also used : IFn(clojure.lang.IFn) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Aggregations

IFn (clojure.lang.IFn)23 Test (org.junit.Test)16 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)15 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)15 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)15 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)15 Keyword (clojure.lang.Keyword)6 PersistentArrayMap (clojure.lang.PersistentArrayMap)6 ArrayList (java.util.ArrayList)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)4 ScriptEngine (javax.script.ScriptEngine)3 RubyProc (org.jruby.RubyProc)2 APersistentVector (clojure.lang.APersistentVector)1 IPersistentMap (clojure.lang.IPersistentMap)1 IPersistentVector (clojure.lang.IPersistentVector)1 ISeq (clojure.lang.ISeq)1 Closure (groovy.lang.Closure)1 FunctionFn2 (org.enumerable.lambda.support.javascript.LambdaJavaScript.FunctionFn2)1 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)1 ScalaInterpreter (org.enumerable.lambda.support.scala.ScalaTest.ScalaInterpreter)1