0.0.9-babel.6 • Published 8 years ago

monadic.js-playground v0.0.9-babel.6

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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

# or (depend on NODE_ENV value during build stage)

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), []);

    // MonadPlus
    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 };
};

// DEMO:

log(demoList);
demoCont(log);
0.0.9-babel.6

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago