public AviatorObject call(Map<String, Object> env, AviatorObject... args) {
if (args.length != 2) {
throw new IllegalArgumentException("map(seq,fun)");
}
Object first = args[0].getValue(env);
AviatorFunction fun = FunctionUtils.getFunction(1, args, env, 1);
if (fun == null) {
throw new ExpressionRuntimeException("There is no function named " + ((AviatorJavaType) args[1]).getName());
}
if (first == null) {
throw new NullPointerException("null seq");
}
Class<?> clazz = first.getClass();
if (Collection.class.isAssignableFrom(clazz)) {
Collection result = null;
try {
result = (Collection) clazz.newInstance();
}
catch (Throwable t) {
// ignore
result = new ArrayList();
}
for (Object obj : (Collection<?>) first) {
result.add(fun.call(env, new AviatorRuntimeJavaType(obj)).getValue(env));
}
return new AviatorRuntimeJavaType(result);
}
else if (clazz.isArray()) {
Object[] seq = (Object[]) first;
Object result = Array.newInstance(Object.class, seq.length);
int index = 0;
for (Object obj : seq) {
Array.set(result, index++, fun.call(env, new AviatorRuntimeJavaType(obj)).getValue(env));
}
return new AviatorRuntimeJavaType(result);
}
else {
throw new IllegalArgumentException(args[0].desc(env) + " is not a seq");