State Machines with Tagged Unions in JS
const { stringify, parse } = require("serialize-daggy");
const { taggedSum, tagged } = require("daggy");
const { curry, pipe } = require("ramda");
const cataDaggy = curry((T, spec) => T.cata(spec));
// Simple State Machine:
// Begin[] --forward(x)--> End["result"]
// | |
// <------ reset ------
const
Simple = taggedSum("Simple", {
End: ["result"],
Begin: ["item"]
}),
{ Begin, End } = Simple,
transitions = {
forward: {
Begin: x => Simple.End(x),
End: result => End(result)
},
reset: {
Begin: _ => Begin(_),
End: result => Begin(null)
}
},
s0 = Begin(null);
console.log(String(Simple.Begin(0)));
console.log(cataDaggy(s0, transitions["forward"]).toString());
no comments