<commit_before>foam.CLASS({
  package: 'foam.mlang',
  implements: ['foam.dao.Sink'],
  name: 'CountExpr',
  properties: [
    {
      name: 'value',
      defaultValue: 0
    }
  ],
  methods: [
    function put() {
      this.value++;
    }
  ]
});<commit_msg>Expand preliminary mlang library with some core expressions.<commit_after>foam.CLASS({
  package: 'foam.mlang',
  implements: ['foam.dao.Sink'],
  name: 'CountExpr',
  properties: [
    {
      name: 'value',
      defaultValue: 0
    }
  ],
  methods: [
    function put() {
      this.value++;
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'ExprArray',
  extends: 'Array',
  properties: [
    {
      name: 'adaptArrayElement',
      defaultValue: function(o) {
        if ( typeof o !== "object" ) return foam.mlang.ConstantExpr.create({ value: o });
        if ( Array.isArray(o) ) return foam.mlang.ConstantExpr.create({ value: o });
        if ( o === true ) return foam.mlang.TrueExpr.create();
        if ( o === false ) return foam.mlang.FalseExpr.create();
        return o;
      }
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'ExprArgument',
  extends: 'Property',
  properties: [
    {
      name: 'adapt',
      defaultValue: function(_, o) {
        if ( typeof o !== "object" ) return foam.mlang.ConstantExpr.create({ value: o });
        return o;
      }
    }
  ]
});


foam.CLASS({
  package: 'foam.mlang',
  name: 'Expr'
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'TrueExpr',
  implements: ['foam.mlang.Expr'],
  axioms: [ 'foam.patterns.Singleton' ],
  methods: [
    function f() { return true; }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'FalseExpr',
  implements: ['foam.mlang.Expr'],
  axioms: [ 'foam.patterns.Singleton' ],
  methods: [
    function f() { return false; }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'BINARY',
  implements: ['foam.mlang.Expr'],
  properties: [
    {
      name: 'arg1',
      class: 'foam.mlang.ExprArgument',
    },
    {
      name: 'arg2',
      class: 'foam.mlang.ExprArgument',
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'NARY',
  properties: [
    {
      class: 'foam.mlang.ExprArray',
      name: 'args'
    },
    {
      class: 'foam.mlang.ExprArgument',
      name: 'arg1'
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'OrExpr',
  implements: ['foam.mlang.Expr'],
  properties: [
    {
      class: 'foam.mlang.ExprArray',
      name: 'args'
    },
  ],
  methods: [
    function f(o) {
      for ( var i = 0 ; i < this.args.length ; i++ ) {
        if ( this.args[i].f(o) ) return true;
      }
      return false;
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'ContainsExpr',
  extends: 'foam.mlang.BINARY',
  methods: [
    function f(o) {
      if ( this.arg1.f(o).indexOf(this.arg2.f(o)) != -1 ) return true;
      return false;
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'ConstantExpr',
  implements: ['foam.mlang.Expr'],
  properties: [
    {
      name: 'value'
    },
  ],
  methods: [
    function f() { return this.value; }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'AndExpr',
  implements: ['foam.mlang.Expr'],
  properties: [
    {
      class: 'foam.mlang.ExprArray',
      name: 'args'
    }
  ],
  methods: [
    function f(o) {
      for ( var i = 0 ; i < this.args.length ; i++ ) {
        if ( ! this.args[i].f(o) ) return false;
      }
      return true;
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'InExpr',
  extends: 'foam.mlang.BINARY',
  methods: [
    function f(o) {
      // TODO: This just looks like a reverse ContainsExpr
      if ( this.arg2.f(o).indexOf(this.arg1.f(o)) != -1 ) return true;
      return false;
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'EqExpr',
  extends: 'foam.mlang.BINARY',
  methods: [
    function f(o) {
      return foam.util.equals(this.arg1.f(o), this.arg2.f(o));
    }
  ]
});

foam.CLASS({
  package: 'foam.mlang',
  name: 'Expressions',
  requires: [
    'foam.mlang.CountExpr',
    'foam.mlang.ConstantExpr',
    'foam.mlang.ContainsExpr',
    'foam.mlang.OrExpr',
    'foam.mlang.AndExpr',
    'foam.mlang.InExpr',
    'foam.mlang.EqExpr'
  ],
  methods: [
    function _nary_(name, args) { return this[name].create({ args: foam.array.argsToArray(args) }); },
    function _binary_(name, arg1, arg2) { return this[name].create({ arg1: arg1, arg2: arg2 }); },
    function OR() { return this._nary_("OrExpr", arguments); },
    function AND() { return this._nary_("AndExpr", arguments); },
    function IN(a, b) { return this._binary_("InExpr", a, b); },
    function CONTAINS(a, b) { return this._binary_("ContainsExpr", a, b); },
    function EQ(a, b) { return this._binary_("EqExpr", a, b); }
  ]
});

foam.CLASS({
  package: 'foam.core',
  name: 'With',
  methods: [
    {
      name: 'with',
      code: function(f) {
        var argNames = foam.fn.argsArray(f);
        var args = [];
        for ( var i = 0 ; i < argNames.length ; i++ ) {
          var a = this[argNames[i]];
          if ( typeof a === "function" ) a = a.bind(this);
          args.push(a);
        }
        return f.apply(this, args);
      }
    }
  ]
});
{'fraction_non_alphanumeric': 0.18579654510556623, 'fraction_numerical': 0.004222648752399232, 'mean_word_length': 2.4058823529411764, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6839876', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1986, 'n_words': 386}