compare method

List compare(
  1. List list,
  2. String operation,
  3. dynamic object
)

compare values inside an array with given object and operations. function will return a new boolen array

var arr = [[1,1,1],[2,2,2],[3,3,3]];
var compare = m2d.compare(arr,'>',2);
print(compare);
//[[false, false, false], [false, false, false], [true, true, true]]

Implementation

List compare(List list, String operation, object) {
  var shapee = this.shape(list);
  if (!_checkArray(list)) {
    throw new Exception('Uneven array dimension');
  }

  if (shapee.length < 2) {
    throw new Exception(
        'Currently support 2D operations or put that values inside a list of list');
  }
  var res = fill(shapee[0], shapee[1], true);
  for (var i = 0; i < shapee[0]; i++) {
    for (var j = 0; j < shapee[1]; j++) {
      try {
        var val = list[i][j];
        switch (operation) {
          case '>':
            res[i][j] = val > object;
            break;
          case '<':
            res[i][j] = val < object;
            break;
          case '>=':
            res[i][j] = val >= object;
            break;
          case '<=':
            res[i][j] = val <= object;
            break;
          case '==':
            res[i][j] = val == object;
            break;
          case '!=':
            res[i][j] = val != object;
            break;
          default:
            throw new Exception('Current operation is not support');
        }
      } catch (e) {
        throw new Exception(
            'Sorry can\'t compare string with number (Not javascript)');
      }
    }
  }
  return res;
}