Functions

Object Monads


ret(3).bnd(=>ret(x**3)).bnd(x=>ret(x*2)).bnd(x=>ret(x-12)).x = 42

In this example, Monad returns simple objects I call "monads". Each monad "m" has two components: "x" can be any JavaScript value and "bnd" is a method that operates on functions. Let m = new Monad(v), then m.x = v and m.bnd(func) for some function func returns a monad "m2", which can be anonymous, whose components are func(v) and the method "bnd". m2.bnd(func2) for some function "func2" returns a monad with components func2(func(v)) and bnd().

The interactive example above shows a

var Monad = function Monad(z = 0) {
  var _this = this;
  this.x = z;
  this.bnd = function (func) {
    return new Monad(func(this.x).x);
  };
}

function ret (v) { 
  return new Monad(v);
} 
    
var foo = v => ret(v).bnd(x => ret(x**3)).bnd(x => ret(x*2)).bnd(x=>ret(x-12)).x;
let val = 3;


let value = 42
let submit = false
let vv = 3;
	
const handleSubmit = () => {
  submit = true
}
	
const handleKeyup = () => {
  submit = false
  if (event.code == 'Enter') {
    event.preventDefault()
    vv = parseInt(event.target.value, 10);
    value = foo(vv);
    return false
  }
}