public static bool EqualElements(ArrayValue arr1, ArrayValue arr2) { if (arr1 == arr2) { return true; } if (arr1 == null || arr2 == null) { return false; } if (arr1.Rows != arr2.Rows || arr1.Cols != arr2.Cols) { return false; } for (int c = 0; c < arr1.Cols; c++) { for (int r = 0; r < arr1.Rows; r++) { Value v1 = arr1[c, r], v2 = arr2[c, r]; if (v1 != v2) { if (v1 == null || v2 == null) { return false; } else if (!(v1.Equals(v2))) { return false; } } } } return true; }
// Generalized n-argument map public static Value Map(Value[] vs) { int n = vs.Length - 1; if (n < 1) { return ErrorValue.argCountError; } if (vs[0] is ErrorValue) { return vs[0]; } FunctionValue fv = vs[0] as FunctionValue; if (fv == null) { return ErrorValue.argTypeError; } if (fv.Arity != n) { return ErrorValue.argCountError; } ArrayValue[] arrs = new ArrayValue[n]; for (int i = 0; i < n; i++) { Value vi = vs[i + 1]; if (vi is ArrayValue) { arrs[i] = vi as ArrayValue; } else if (vi is ErrorValue) { return vi; } else { return ErrorValue.argTypeError; } } int cols = arrs[0].Cols, rows = arrs[0].Rows; for (int i = 1; i < n; i++) { if (arrs[i].Cols != cols || arrs[i].Rows != rows) { return ErrorValue.Make("#ERR: Array shapes differ"); } } Value[] args = new Value[n]; Value[,] result = new Value[cols, rows]; for (int c = 0; c < cols; c++) { for (int r = 0; r < rows; r++) { for (int i = 0; i < n; i++) { args[i] = arrs[i][c, r]; } result[c, r] = fv.Apply(args); } } return new ArrayExplicit(result); }