Functions

Monad (from Greek μονάς monas, "singularity" in turn from μόνος monos, "alone") refers, in cosmogony, to the Supreme Being, divinity or the totality of all things. A basic unit of perceptual reality is a "monad" in Gottfried Leibniz' Monadology , published in 1714. A single note in music theory is called a monad.

```function Monad (x) {
return function foo (func) {
if (func === "stop") return x
else  {
x = func(x);
return foo;
}
};
}```
In "Monad(6)(v=>v+7)(v=>v*4)(v=>v-10)", the expression "Monad(6)" creates a closure around "x" accessible to the return value, foo(). The expression "foo('stop')" causes foo to return the number held in the closure, 42.

As in Category Theory and the Haskell programming language, the monads described above encapsulate sequences of computations. They do it in a drastically different way, without relying on types.

Below, you can supply num in "Monad(num)(sum(7))(prod(4))(sum(-10))('stop')" where

```const prod = a => b => a*b;
const sum = a => b => a+b;
```

num is 6 so run(num) returns 42

### Encapsulating State

Named monads persist after they encounter "stop", ready to process more functions.
```var m = Monad(3)
m("stop");  // returns 3
m(v => v**3)  // Returns foo()
m("stop")  // returns 27```