UNPKG

mezzanine

Version:

Fantasy land union types with pattern matching

125 lines (96 loc) 3.99 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.maybe_name = exports.Free = undefined; var _ramda = require('ramda'); var _union = require('../union'); var _union2 = _interopRequireDefault(_union); var _type = require('../type'); var _type2 = _interopRequireDefault(_type); var _maybe = require('./maybe'); var _maybe2 = _interopRequireDefault(_maybe); function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} var { Just, Nothing } = _maybe2.default; var compose = (f, g) => x => f(g(x)); var id = x => x; var kleisli_comp = (f, g) => x => f(x).chain(g //=============FREE============= );var Pure = _type2.default`Pure`(_ramda.T); var Impure = _type2.default`Impure`({ value: _ramda.T, f: Function }); var Free = exports.Free = _union2.default`Free`({ Pure, Impure /*{ chain: ctx => (f) => ctx.case({ Impure: ({ value, f: fn }) => Free({ value, f: kleisli_comp(value, fn) }), Pure : x => f(x) }), map: ctx => (f) => ctx.chain(a => Free.of(f(a))) }*/ // const Free = daggy.taggedSum({Impure: ['x', 'f'], Pure: ['x']}) // const {Impure, Pure} = Free }); Free.of = Pure; /* Free.prototype.chain = function(f) { return this.cata({ Impure: (x, g) => Impure({ x, f: kleisli_comp(g, f) }), Pure : x => f(x) }) }*/ /* Free.prototype.map = function(f) { return this.chain(a => Free.of(f(a))) } */ var liftF = command => Free({ value: command, f: Free.of //=============IO via FREE============= });var IO = _type2.default`IO`({ f: () => true }); var io = compose(liftF, IO); var unsafePerformIO = free => free.case({ Pure: id, Impure: ({ value, f }) => unsafePerformIO(f(value.f())) //=============Maybe via FREE============= }); var just = compose(liftF, Just); var nothing = liftF(Nothing); var runMaybe = free => free.case({ Pure: x => x, Impure: ({ value, f }) => value.case({ Just: value => runMaybe(f(value)), Nothing: () => nothing }) //=============Examples============= }); var safeProp = (k, isCh = false) => data => { console.log(data, k // if(o.map){ // const m = o.map(e => typeof e) // m // } );data; var edit = isCh ? _maybe2.default : id; var result = data[k]; result; return edit(result //? just(data[k]) : nothing ); }; var toUpperCase = text => text.toUpperCase // const localStorage = io({ f: () => ({ preferences: 'blue' }) }) // const printLn = x => io({ f: () => console.log(x) }) //maybe_name :: Free (Maybe String) ();var maybe_name = exports.maybe_name = (0, _maybe2.default)({ user: { name: 'jerry' // .map(id) } }).map(safeProp('user')).chain(safeProp('name', true)).chain(name => name === 'bob' ? (0, _maybe2.default)(name) : (0, _maybe2.default)()).map(toUpperCase).toJSON // .map(({ value }) => ({ value: n.value })) (); maybe_name; // console.log(Maybe) // const result = runMaybe(maybe_name) // result // JERRY //i :: Free (IO ()) // const i = localStorage // .map(l => l.preferences) // .chain(printLn) // unsafePerformIO(i) // blue //# sourceMappingURL=free.draft.js.map //# sourceMappingURL=free.draft.js.map