(function(root) {
// add :: Number -> Number -> Number
const add =
x => y => x + y
// inc :: Number -> Number
const inc =
add(1)
root.helpers = {
add, inc
}
})(window)
<!DOCTYPE html>
<html>
<head>
<script data-require="crocks@*" data-semver="0.8.1" src="https://unpkg.com/crocks@0.8.1/dist/crocks.min.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<script src="logger.js"></script>
<script src="helpers.js"></script>
<script src="script.js"></script>
</body>
</html>
(function(e){const n=console.log;const o=document.querySelector(e.selector);const t=e.globalName||"logger";i(o,{background:e.colors.background,color:e.colors.foreground,fontFamily:"monospace"});function r(e){return{}.toString.call(e)==="[object Array]"}function l(e){return{}.toString.call(e)==="[object Object]"}function c(e){return e&&typeof e.inspect==="function"}function i(e,n){Object.assign(e.style,n);return e}function u(e){const n=document.createElement("span");if(e.tagName){n.appendChild(e)}else{n.innerText=e}return n}function a(n){const o=document.createElement("span");o.appendChild(u("{ "));Object.keys(n).reduce(function(o,t,r,l){o.appendChild(i(u(t+": "),{color:e.colors.key}));o.appendChild(s(n[t]));if(r<l.length-1){o.appendChild(u(", "))}return o},o);o.appendChild(u(" }"));return o}function d(e){const n=document.createElement("span");n.appendChild(u("[ "));e.reduce(function(n,o,t){n.appendChild(s(o));if(t<e.length-1){n.appendChild(u(", "))}return n},n);n.appendChild(u(" ]"));return n}function s(n){if(c(n)){return s(n.inspect())}else if(typeof n==="number"){return i(u(n),{color:e.colors.number})}else if(typeof n==="string"){return i(u("'"+n+"'"),{color:e.colors.string})}else if(typeof n==="boolean"){return i(u(n),{color:e.colors.boolean})}else if(typeof n==="function"){return i(u("Function"))}else if(r(n)){return u(d(n))}else if(l(n)){return u(a(n))}else if(n===undefined){return i(u("undefined"),{fontStyle:"italic",color:e.colors.boolean})}else if(n===null){return i(u("null"),{fontStyle:"italic",color:e.colors.boolean})}}function f(n,o){const t=document.createElement("dl");const r=document.createElement("dt");const l=document.createElement("dd");i(t,{padding:"0",margin:"0",marginBottom:"0.6em",fontSize:e.fontSize});i(r,{color:e.colors.label,fontWeight:600,fontSize:"1.1em",margin:"0"});i(l,{padding:"0 0.75em",margin:"0"});r.innerText=n+":";l.appendChild(s(o));t.appendChild(r);t.appendChild(l);return t}function p(n){return i(u(s(n)),{display:"block",margin:"0",marginBottom:"0.6em",fontSize:e.fontSize})}function g(){o.appendChild(arguments.length>1?f(arguments[0],arguments[1]):p(arguments[0]));n.apply(console,arguments)}e.root[t]=g;console.log=g})({
globalName:"log",
root:window,
selector:"body",
fontSize:"28px",
colors: {
background:"transparent",
foreground:"gray",
label:"blue",
nil:"green",
number:"violet",
string:"red",
key:"blue",
"boolean":"green"
}
});
const { State } = crocks
const { get, modify } = State
const { constant } = crocks
const { add, inc } = helpers
// State s a
// State.of :: a -> State s a
log(
'State of instance',
State.of(10)
)
log(
'State of runWith',
State.of(10)
.runWith(2)
)
log(
'State of execWith',
State.of(10)
.execWith(2)
)
log(
'State of evalWith',
State.of(10)
.evalWith(2)
)
// computeMap :: Number -> State Number
const computeMap = n =>
State.of(n)
.map(add(2))
log(
'computeMap evalWith 2',
computeMap(10)
.evalWith(2)
)
// chain :: State s a ~> (a -> State s b) -> State s b
// computeChain :: Number -> State Number
const computeChain = n =>
State.of(n)
.chain(x => get(add(x)))
log(
'computeChain evalWith 5',
computeChain(10)
.evalWith(5)
)
log(
'computeChain evalWith 2',
computeChain(10)
.evalWith(2)
)
// addState :: Number -> State Number
const addState = n =>
get(add(n))
// computeAddState :: Number -> State Number
const computeAddState = n =>
State.of(n)
.chain(addState)
log(
'computeAddState evalWith 2',
computeAddState(10)
.evalWith(2)
)
log(
'computeAddState evalWith 5',
computeAddState(10)
.evalWith(5)
)
log(
'computeAddState execWith 2',
computeAddState(10)
.execWith(2)
)
log(
'computeInc execWith 2',
computeAddState(10)
.chain(() => modify(inc))
.execWith(2)
)
log(
'computeInc evalWith 2',
computeAddState(10)
.chain(() => modify(inc))
.evalWith(2)
)
// incState :: Number -> State Number
const incState = n =>
modify(inc)
.map(constant(n))
log(
'incState evalWith 2',
computeAddState(10)
.chain(incState)
.evalWith(2)
)
log(
'incState execWith 2',
computeAddState(10)
.chain(incState)
.execWith(2)
)
// compute :: Number -> State Number
const compute = n =>
State.of(n)
.chain(addState)
.chain(incState)
.chain(addState)
log(
'compute evalWith 2',
compute(10)
.evalWith(2)
)
log(
'compute execWith 2',
compute(10)
.execWith(2)
)
log(
'compute runWith 2',
compute(10)
.runWith(2)
)