jweinst1
5/15/2016 - 4:41 PM

an example of continuously chained automata in js.

an example of continuously chained automata in js.

//linear chained cellular automata--

var MathAutomatan = (function(){
	//storage of possible math operations to perform
	var math = {
		"+":function(first, second) {
        return first + second;
		},
		"-":function(first, second) {
        return first - second;
		},
		"*":function(first, second) {
        return first * second;
		},
		"/":function(first, second) {
        return first / second;
		},
		"%":function(first, second) {
        return first % second;
		}
	};
	function MathAutomatan(oper, operand, prev){
		this.prev = prev || null;
		this.nextproc = null;
		this.operand = operand;
		this.oper = oper;
		this.func = math[this.oper];
	}
	//extends the automatan chain by recursion
	MathAutomatan.prototype.extend = function(oper, operand){
		if(this.nextproc === null){
			this.nextproc = new MathAutomatan(oper, operand, this);
		}
		else{
			this.nextproc.extend(oper, operand);
		}
	};
	//applies all automatan in the chain
	MathAutomatan.prototype.apply = function(obj){
		obj.setValue(this.func(obj.value, this.operand));
		if(this.nextproc) {
			this.nextproc.apply(obj);
		}
	};
	return MathAutomatan;
})();

//value object for passing around
var ValObj = (function(){
	function ValObj(val){
		this.value = val;
	}
	ValObj.prototype.setValue = function(newval){
		this.value = newval;
	};
	return ValObj;
})();

/*
clear
Native Browser JavaScript
   
   var a = new MathAutomatan("+", 2);
   a
=> { prev: null, nextproc: null, operand: 2, oper: '+', func: [Function] }
   a.extend("*", 2)
   a.extend("*", 2)
   a.extend("*", 2)
   a.extend("*", 2)
   a.extend("*", 2)
   a.extend("*", 2)
   a.extend("*", 2)
   a
=> { prev: null,
  nextproc: 
   { prev: [Circular],
     nextproc: { prev: [Circular], nextproc: [Object], operand: 2, oper: '*', func: [Function] },
     operand: 2,
     oper: '*',
     func: [Function] },
  operand: 2,
  oper: '+',
  func: [Function] }
   var b = new ValObj(8);
   b
=> { value: 8 }
   a.apply(b)
   b
=> { value: 1280 }*/