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());
}
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");
}
}
Aggregations