Mflow
Mflow is the monad-style flow control library for nodejs.
This library is inspired by followings:
- Haskell Monads
- F# Computation Expressions
- visionmedia/co
Supported monad-style flows are followings:
- async
- lazy
- maybe
- state
Caution
Mflow uses ECMAScript 6 generators.
Currently you must use the --harmony-generators
flag when running node 0.11.x.
Installation
$ npm install mflow
Example
This is an async flow.
var fs = ;var mflow = ;var async = mflowasync; var flow = async { var a = { fs; }; var b = { fs; }; return a + b;}; ;
API
Mflow provides following top-level functions:
- async
- lazy
- maybe
- state
These functions accept a generator function which has no parameter.
async(fn)
Makes an async flow.
fn
: a generator function- return: an async function
The yieldable values are followings:
- a function which has one parameter and passes it to an async function
- an async flow
yield
returns the async function's result.
var fs = ;var mflow = ;var async = mflowasync; var flow = async { var a = { fs; }; var b = { fs; }; return a + b;}; ;
async.join(fn...)
Runs multiple async functions in parallel.
fn...
: multiple functions or an array of function- return: a yieldable function. When this function is yielded, all joined function's results are returned as an array.
var fs = ;var mflow = ;var async = mflowasync; var flow = async { var { fs; }; var { fs; }; var results = async; // instead of multiple arguments, you can use an array. // var results = yield async.join([a, b]); return results0 + results1;}; ;
lazy(fn)
Makes a lazy flow.
fn
: a generator function- return: a function which wraps an expression. When this function is called or is yielded, the expression is evaluated only once.
The yieldable values are followings:
- a function which has no parameter and returns a value
- a lazy flow
- non-function value: When it is yielded, the value is returned without any change.
var mflow = ;var lazy = mflowlazy; var x = 10;var result = ; console; // 20
When a non-function value is yielded, the value is returned without any change.
var mflow = ;var lazy = mflowlazy; var x = 10;var lazyValue1 = ;var lazyValue2 = ; { return ;}; console; // 120console; // 3
maybe(fn)
Runs a flow which results a successful value or null
.
fn
: a generator function- return: a successful value or
null
The yieldable values are followings:
null
undefined
- other values
When null
or undefined
is yielded, the flow is stopped and results null
.
Otherwise the yielded value is returned without any change.
var mflow = ;var maybe = mflowmaybe; { return ;} console; // 3console; // null
state(fn)
Makes a state flow.
fn
: a generator function- return: a function which has one parameter and returns an array. The parameter is a flow's state. The returned array contains the flow's result as 1st elment and the flow's state as 2nd element.
The yieldable values are followings:
- a function which has one parameter and returns an array. The parameter is a flow's state. The returned array contains the flow's result as 1st elment and the flow's state as 2nd element.
- a state flow
var mflow = ;var state = mflowstate; { return { return s s; };} { return { s; return null s; };} var flow = ; var result = ;console; // 10console; // [8, 3, 0, 2, 1, 0]
state.get()
Gets a flow's state.
- return: a yieldable function. When this function is apply to
yield
, a flow's state are returned.
pop
and push
functions in abobe code can be replaced with following implementations using state.get
and state.put
:
var { return ;}; var { return ;};
state.put(s)
Puts a new state as a flow's state.
s
: a new state- return: a yieldable function. When this function is apply to
yield
, a flow's state is replaced with the argument.
See state.get()
.
state.eval(flow, s)
Runs a flow and returns the flow's result.
flow
: a state flows
: a flow's state
Followings are equivalent:
var result = state;console; // 10
var result = ;console; // 10
state.exec(flow, s)
Runs a flow and returns the flow's state.
flow
: a state flows
: a flow's state
Followings are equivalent:
var result = state;console; // [8, 3, 0, 2, 1, 0]
var result = ;console; // [8, 3, 0, 2, 1, 0]
Test
$ mocha --harmony-generators
License
MIT