UNPKG

@typed/fp

Version:

Data Structures and Resources for fp-ts

110 lines 3.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromNaturalTransformation = exports.provideAll = exports.useAll = exports.provideSome = exports.useSome = exports.ask = exports.of = exports.chainRec = exports.ap = exports.chain = exports.map = exports.toMonad = exports.getDo = exports.liftFx = void 0; const Either_1 = require("fp-ts/Either"); const function_1 = require("fp-ts/function"); const Fx_1 = require("./Fx"); /** * Create a lift function that will convert any F<A> into Fx<F<A>, A> * @category Constructor * @since 0.13.0 */ const liftFx = () => lift_; exports.liftFx = liftFx; function lift_(hkt) { return { [Symbol.iterator]: function* () { const value = yield hkt; return value; }, }; } function getDo() { const lift = (0, exports.liftFx)(); return (f) => (0, Fx_1.doFx)(() => f(lift)); } exports.getDo = getDo; /** * Using a ChainRec instance we can create a stack-safe interpreter for do-notation * using Fx + generators. */ function toMonad(M) { return function fxToMonad(fx) { return (0, function_1.pipe)(M.of(fx[Symbol.iterator]), M.map((f) => f()), M.chain((generator) => { const result = generator.next(); if (result.done) { return M.of(result.value); } return (0, function_1.pipe)(result.value, M.chain(M.chainRec((a) => { const result = generator.next(a); return result.done ? (0, function_1.pipe)(result.value, Either_1.right, M.of) : (0, function_1.pipe)(result.value, M.map(Either_1.left)); }))); })); }; } exports.toMonad = toMonad; function map() { return Fx_1.map; } exports.map = map; function chain() { return Fx_1.chain; } exports.chain = chain; function ap(M) { const lift = (0, exports.liftFx)(); const to = toMonad(M); return (fa) => (fab) => (0, function_1.pipe)(fab, to, (0, function_1.pipe)(fa, to, M.ap), lift); } exports.ap = ap; function chainRec(M) { return chainRec_(M); } exports.chainRec = chainRec; function chainRec_(M) { const to = toMonad(M); const lift = (0, exports.liftFx)(); return (f) => (a) => { const fbm = (0, function_1.pipe)(f(a), to, M.chain((0, Either_1.match)(M.chainRec((a) => (0, function_1.pipe)(a, f, to)), M.of))); return lift(fbm); }; } function of(M) { const lift = (0, exports.liftFx)(); return (value) => lift(M.of(value)); } exports.of = of; function ask(M) { const lift = (0, exports.liftFx)(); return () => lift(M.fromReader(function_1.identity)); } exports.ask = ask; function useSome(M) { const lift = (0, exports.liftFx)(); const to = toMonad(M); return (provided) => (fx) => (0, function_1.pipe)(fx, to, (x) => M.useSome(provided)(x), lift); } exports.useSome = useSome; function provideSome(M) { const lift = (0, exports.liftFx)(); const to = toMonad(M); return (provided) => (fx) => (0, function_1.pipe)(fx, to, (x) => M.provideSome(provided)(x), lift); } exports.provideSome = provideSome; function useAll(M) { const lift = (0, exports.liftFx)(); const to = toMonad(M); return (provided) => (fx) => (0, function_1.pipe)(fx, to, (x) => M.useAll(provided)(x), lift); } exports.useAll = useAll; function provideAll(M) { const lift = (0, exports.liftFx)(); const to = toMonad(M); return (provided) => (fx) => (0, function_1.pipe)(fx, to, (x) => M.provideAll(provided)(x), lift); } exports.provideAll = provideAll; function fromNaturalTransformation(transformation) { return (0, function_1.flow)(transformation, (0, exports.liftFx)()); } exports.fromNaturalTransformation = fromNaturalTransformation; //# sourceMappingURL=FxT.js.map