Search in sources :

Example 6 with ISeq

use of clojure.lang.ISeq in project enumerable by hraberg.

the class ClojureTest method basicSequenceOperations.

@Test
public void basicSequenceOperations() throws Exception {
    ISeq map = (map(fn(s, s.toUpperCase()), list("hello", "world")));
    assertEquals(list("HELLO", "WORLD"), map);
    ISeq filter = (filter(fn(n, n % 2 == 0), list(1, 2, 3, 4, 5)));
    assertEquals(list(2, 4), filter);
    Integer reduce = (reduce(fn(n, m, n * m), list(1, 2, 3, 4, 5)));
    assertEquals(120, reduce.intValue());
}
Also used : ISeq(clojure.lang.ISeq) 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 7 with ISeq

use of clojure.lang.ISeq in project http-kit by http-kit.

the class HttpUtils method bodyBuffer.

public static ByteBuffer bodyBuffer(Object body) throws IOException {
    if (body == null) {
        return null;
    } else if (body instanceof String) {
        byte[] b = ((String) body).getBytes(UTF_8);
        return ByteBuffer.wrap(b);
    } else if (body instanceof InputStream) {
        DynamicBytes b = readAll((InputStream) body);
        return ByteBuffer.wrap(b.get(), 0, b.length());
    } else if (body instanceof File) {
        // serving file is better be done by Nginx
        return readAll((File) body);
    } else if (body instanceof Seqable) {
        ISeq seq = ((Seqable) body).seq();
        if (seq == null) {
            return null;
        } else {
            DynamicBytes b = new DynamicBytes(seq.count() * 512);
            while (seq != null) {
                b.append(seq.first().toString(), UTF_8);
                seq = seq.next();
            }
            return ByteBuffer.wrap(b.get(), 0, b.length());
        }
    // makes ultimate optimization possible: no copy
    } else if (body instanceof ByteBuffer) {
        return (ByteBuffer) body;
    } else {
        throw new RuntimeException(body.getClass() + " is not understandable");
    }
}
Also used : ISeq(clojure.lang.ISeq) Seqable(clojure.lang.Seqable) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Aggregations

ISeq (clojure.lang.ISeq)7 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)5 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)5 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)5 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)5 Test (org.junit.Test)5 Seqable (clojure.lang.Seqable)2 IFn (clojure.lang.IFn)1 IPersistentVector (clojure.lang.IPersistentVector)1 Var (clojure.lang.Var)1 ByteBuffer (java.nio.ByteBuffer)1 MappedByteBuffer (java.nio.MappedByteBuffer)1