µ-ffsm: Micro fluent API helper
Very tiny helper function to construct fluent interfaces.
> npm i mu-ffsm
Import
var FFSM = require('mu-ffsm');
Create language/machine.
// internal state is an Arrayvar Talker = ;
Construct sentences/instances:
var cowboyGreeting = ; // make dramaticconsole;
Idea
We consider a sort of 'state machine' that maintains an internal
state of type S
. We then consider three types of
functions on it:
- entry :
* ⟶ S
- transition :
S ⟶ * ⟶ S
- exit :
S ⟶ * ⟶ *
We write the entry function as 0
, the exit function as 1
and then
name all the transition functions however we like.
Then
var M = ;
Now
var i = // x : S <- 0(entry) // y : S <- a(x, trigger_0) // z : S <- b(y, trigger_1) ; // i : S <- a(z, trigger_2)
Finally
var y = ; // y <- 1(i, x)
So we have
-
First
M(entry)
creates a new machine instance of type M. It's initial state derived from entry (0(entry)
). -
Then
.a(trigger_0
) transitions the machine with transitiona
to a new state, using the previous state and the data fromtrigger_0
to compute the new state. -
Similarly
.b(t_1)
,.a(t_2)
. -
Finally, the
i(x)
call constructs an element out of the internal state and the argument using the exit function1(i,x)
.