1 module orelang.operator.MapOperator; 2 import orelang.expression.ImmediateValue, 3 orelang.expression.IExpression, 4 orelang.operator.IOperator, 5 orelang.Closure, 6 orelang.Engine, 7 orelang.Value; 8 import std.algorithm, 9 std.array; 10 11 class MapOperator : IOperator { 12 /** 13 * call 14 */ 15 public Value call(Engine engine, Value[] args) { 16 17 Value efunc = engine.eval(args[0]); 18 Value eargs1 = engine.eval(args[1]); 19 20 if (eargs1.type == ValueType.Array) { 21 Value[] array = eargs1.getArray; 22 23 if (efunc.type == ValueType.Closure) { 24 Value[] ret; 25 26 foreach (elem; array) { 27 ret ~= efunc.getClosure.eval([elem]); 28 } 29 30 return new Value(ret); 31 } else { 32 Value[] ret; 33 34 foreach (elem; array) { 35 ret ~= efunc.getIOperator.call(engine, [elem]); 36 } 37 38 return new Value(ret); 39 } 40 } else { 41 if (!(eargs1.type == ValueType.ImmediateValue) && !(eargs1.getImmediateValue.value.type == ValueType.Array)) { 42 throw new Exception("map requires array and function as a Operator"); 43 } 44 45 Value[] array = eargs1.getImmediateValue.value.getArray; 46 47 if (efunc.type == ValueType.Closure) { 48 Value[] ret; 49 50 foreach (elem; array) { 51 ret ~= efunc.getClosure.eval([elem]); 52 } 53 54 return new Value(ret); 55 } else { 56 Value[] ret; 57 58 foreach (elem; array) { 59 ret ~= efunc.getIOperator.call(engine, [elem]); 60 } 61 62 return new Value(ret); 63 } 64 } 65 } 66 }