<commit_before><html>
 <head>
  <script language="javascript" src="stdlib.js"></script>
  <script language="javascript" src="mm.js"></script>
  <script language="javascript" src="debug.js"></script>
  <title>FOAM By Example</title>
  <style>
    code {color:blue;}
  </style>
 </head>

 <body style="font-family:monospace;">

<script id='demo' language="xjavascript">
// Define a new class with foam.CLASS
foam.CLASS({
  name: 'Test',
  properties: [
    // short-form
    'a',
    // long-form
    {
      name: 'b'
    }
  ],
  methods: [
    // short-form
    function f1() { return 1; },
    // long-form
    {
      name: 'f2',
      code: function() { return 2; }
    }
  ]
});
log(Test);

// Use class.describe() to learn about the class
Test.describe();

// Create an instance of Test
var o = Test.create();
log(o);
log(o.a, o.b);

// Create an instance with a map argument to initialize properties
var o = Test.create({a:1, b:'foo'});
log(o.a, o.b);

// Objects have a reference to their class in .cls_
log(o.cls_.name);

// Test Class membership with Class.isInstance()
log(Test.isInstance(o), Test.isInstance('foo'));

// Call Methods
log(o.f1(), o.f2());

// Update Properties
o.a++;
o.b = 'bar';
log(o.a, o.b);

// Multiple properties can be updated at once using copyFrom().
o.copyFrom({a: 42, b: 'rosebud'});
log(o.a, o.b);

// Call toString on an object
log(o.toString());

// Call describe() on an object to see its Property values
o.describe();

// Properties and Methods are types of Axioms
// Get an array of all Axioms belonging to a Class by calling getAxioms.
Test.getAxioms().forEach(function(a) { console.log(a.cls_.name, a.name); });

// Find an Axiom for a class using getAxiomByName
log(Test.getAxiomByName('a'));

// Find all Axioms of a particular class using getAxiomsByClass
log(Test.getAxiomsByClass(Method));

// Properties are defined on the class as constants
log(Method.NAME);
Method.NAME.describe();

// Property constants contain map functions
log(Test.getAxiomsByClass(Method).map(Method.NAME.f));

// Property constants contain comparators
log(Test.getAxiomsByClass(Method).sort(Method.NAME.compare).map(Method.NAME.f));

// Default Values can be defined for Properties
foam.CLASS({
  name: 'DefaultValueTest',
  properties: [
    { name: 'a', defaultValue: 42 },
    { name: 'b', defaultValue: 'foo' },
    { name: 'c' }
  ]
});
var o = DefaultValueTest.create();<commit_msg>Added init() test.<commit_after><html>
 <head>
  <script language="javascript" src="stdlib.js"></script>
  <script language="javascript" src="mm.js"></script>
  <script language="javascript" src="debug.js"></script>
  <title>FOAM By Example</title>
  <style>
    code {color:blue;}
  </style>
 </head>

 <body style="font-family:monospace;">

<script id='demo' language="xjavascript">
// Define a new class with foam.CLASS
foam.CLASS({
  name: 'Test',
  properties: [
    // short-form
    'a',
    // long-form
    {
      name: 'b'
    }
  ],
  methods: [
    // short-form
    function f1() { return 1; },
    // long-form
    {
      name: 'f2',
      code: function() { return 2; }
    }
  ]
});
log(Test);

// Use class.describe() to learn about the class
Test.describe();

// Create an instance of Test
var o = Test.create();
log(o);
log(o.a, o.b);

// Create an instance with a map argument to initialize properties
var o = Test.create({a:1, b:'foo'});
log(o.a, o.b);

// Objects have a reference to their class in .cls_
log(o.cls_.name);

// Test Class membership with Class.isInstance()
log(Test.isInstance(o), Test.isInstance('foo'));

// Call Methods
log(o.f1(), o.f2());

// Update Properties
o.a++;
o.b = 'bar';
log(o.a, o.b);

// Multiple properties can be updated at once using copyFrom().
o.copyFrom({a: 42, b: 'rosebud'});
log(o.a, o.b);

// Call toString on an object
log(o.toString());

// Call describe() on an object to see its Property values
o.describe();

// Properties and Methods are types of Axioms
// Get an array of all Axioms belonging to a Class by calling getAxioms.
Test.getAxioms().forEach(function(a) { console.log(a.cls_.name, a.name); });

// Find an Axiom for a class using getAxiomByName
log(Test.getAxiomByName('a'));

// Find all Axioms of a particular class using getAxiomsByClass
log(Test.getAxiomsByClass(Method));

// Properties are defined on the class as constants
log(Method.NAME);
Method.NAME.describe();

// Property constants contain map functions
log(Test.getAxiomsByClass(Method).map(Method.NAME.f));

// Property constants contain comparators
log(Test.getAxiomsByClass(Method).sort(Method.NAME.compare).map(Method.NAME.f));

// If a Class defineds an init() method, itss called
// when an object is created. 
foam.CLASS({
  name: 'InitTest',
  methods: [ function init() { log('Just Born!'); } ]
});
InitTest.create();

// Default Values can be defined for Properties
foam.CLASS({
  name: 'DefaultValueTest',
  properties: [
    { name: 'a', defaultValue: 42 },
    { name: 'b', defaultValue: 'foo' },
    { name: 'c' }
  ]
});
var o = DefaultValueTest.create();
{'fraction_non_alphanumeric': 0.16686531585220502, 'fraction_numerical': 0.004370282081843465, 'mean_word_length': 3.615032080659945, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '4710499', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1697, 'n_words': 494}