monadic.js-playground
To install package from npm type:
npm install monadic.js-playground
To build type into root dir:
npm install
webpack
To run type into root dir:
node build/monadic.dev.js
node build/monadic.min.js
If you wanna change something, entry point is here: index.js
Enjoy ;)
How it looks like:
const unit = {};
const log = msg => console.log(msg);
const curry2 = fn => a => b => fn(a, b);
const fetch = curry2((url, cb) => setTimeout(cb, 100, url));
const apply = (fn, arg) => fn(arg);
const listMonad = apply(() => {
const mreturn = a => [a];
const mbind = (mval, fn) => mval
.map(val => fn(val))
.reduce((prev, next) => prev.concat(next), []);
const mzero = [];
const mplus = (a, b) => a.concat(b);
const guard = p => p ? mreturn(unit) : mzero;
return {
mreturn,
mbind,
mzero,
mplus,
guard
};
}, null);
const contMonad = {
mreturn: a => fn => fn(a),
mbind: (mval, fn) => c => mval(val => fn(val)(c))
};
const demoList = do listMonad {
a <- [1, 2, 3];
b <- do listMonad {
c <- [1, 2, 3];
d <- [1, 2, 3];
return c + d;
};
listMonad.guard(a + b > 7);
return a + b;
};
const demoCont = do contMonad {
data <- fetch("data");
moreData <- fetch("data_data");
evenMoreData <- fetch(moreData + "_data_data");
return { data, moreData, evenMoreData };
};
log(demoList);
demoCont(log);