jweinst1
5/30/2016 - 2:23 AM

int machine type of closure that allows subtraction or addition modes

int machine type of closure that allows subtraction or addition modes

//integer machine

//can add values to itself via a closure, or change its mode
//supports addition or subtraction
function IntMachine(value){
	var state = "+";
	var funcs = {
		"+":function(num){
			value += num;
		},
		"-":function(num){
			value -= num;
		}
	};
	return function(elem){
		if(elem in funcs){
			state = elem;
		}
		else if(typeof elem === 'number') {
			funcs[state](elem);
			return value;
		}
	};
}