UNPKG

fp-ts-wonka

Version:

[fp-ts](https://github.com/gcanti/fp-ts) bindings for [wonka](https://wonka.kitten.sh/)

442 lines 12.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTask = exports.apSW = exports.apS = exports.bind = exports.bindTo = exports.Do = exports.MonadSource = exports.MonadTask = exports.MonadIO = exports.Filterable = exports.Compactable = exports.Alternative = exports.Alt = exports.Monad = exports.chainFirst = exports.Chain = exports.Applicative = exports.Apply = exports.flap = exports.Pointed = exports.Functor = exports.getMonoid = exports.URI = exports.zero = exports.partition = exports.filter = exports.separate = exports.partitionMap = exports.compact = exports.filterMap = exports.alt = exports.flatten = exports.chain = exports.of = exports.apSecond = exports.apFirst = exports.apW = exports.ap = exports.map = exports.fromTask = exports.fromIO = exports.fromOption = void 0; var Apply_1 = require("fp-ts/Apply"); var E = __importStar(require("fp-ts/Either")); var function_1 = require("fp-ts/function"); var Functor_1 = require("fp-ts/Functor"); var Chain_1 = require("fp-ts/Chain"); var O = __importStar(require("fp-ts/Option")); var Wonka = __importStar(require("wonka")); var sources_1 = require("./sources"); // ------------------------------------------------------------------------------------- // natural transformations // ------------------------------------------------------------------------------------- /** * @since 0.1.0 * @category Natural transformations */ var fromOption = function (o) { return O.isNone(o) ? Wonka.empty : Wonka.fromValue(o.value); }; exports.fromOption = fromOption; /** * @since 0.1.0 * @category Natural transformations */ var fromIO = function (ma) { return (0, sources_1.defer)(function () { return Wonka.fromValue(ma()); }); }; exports.fromIO = fromIO; /** * @since 0.1.0 * @category Natural transformations */ var fromTask = function (ma) { return (0, sources_1.defer)(function () { return Wonka.fromPromise(ma()); }); }; exports.fromTask = fromTask; // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); }; var _ap = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); }; /* istanbul ignore next */ var _chain = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.chain)(f)); }; /* istanbul ignore next */ var _alt = function (me, that) { return (0, function_1.pipe)(me, (0, exports.alt)(that)); }; /* istanbul ignore next */ var _filter = function (fa, p) { return (0, function_1.pipe)(fa, (0, exports.filter)(p)); }; /* istanbul ignore next */ var _filterMap = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.filterMap)(f)); }; /* istanbul ignore next */ var _partition = function (fa, p) { return (0, function_1.pipe)(fa, (0, exports.partition)(p)); }; /* istanbul ignore next */ var _partitionMap = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.partitionMap)(f)); }; // ------------------------------------------------------------------------------------- // type class members // ------------------------------------------------------------------------------------- /** * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) * => F<B>` whose argument and return types use the type constructor `F` to * represent some computational context. * * @since 0.1.0 * @category Functor */ var map = function (f) { return function (fa) { return (0, function_1.pipe)(fa, Wonka.map(f)); }; }; exports.map = map; /** * Apply a function to an argument under a type constructor. * * @since 0.1.0 * @category Apply */ var ap = function (fa) { return function (fab) { return (0, function_1.pipe)(Wonka.combine(fab, fa), Wonka.map(function (_a) { var f = _a[0], a = _a[1]; return f(a); })); }; }; exports.ap = ap; /** * Apply a function to an argument under a type constructor. * * @since 0.1.0 * @category Apply */ exports.apW = exports.ap; /** * Combine two effectful actions, keeping only the result of the first. * * Derivable from `Apply`. * * @since 0.1.0 * @category Combinators */ var apFirst = function (fb) { return (0, function_1.flow)((0, exports.map)(function (a) { return function () { return a; }; }), (0, exports.ap)(fb)); }; exports.apFirst = apFirst; /** * Combine two effectful actions, keeping only the result of the second. * * Derivable from `Apply`. * * @since 0.1.0 * @category Combinators */ var apSecond = function (fb) { return (0, function_1.flow)((0, exports.map)(function () { return function (b) { return b; }; }), (0, exports.ap)(fb)); }; exports.apSecond = apSecond; /** * @since 0.1.0 * @category Pointed */ exports.of = Wonka.fromValue; /** * Composes computations in sequence, using the return value of one computation * to determine the next computation. * * @since 0.1.0 * @category Monad */ var chain = function (f) { return function (ma) { return (0, function_1.pipe)(ma, Wonka.mergeMap(f)); }; }; exports.chain = chain; /** * Derivable from `Monad`. * * @since 0.1.0 * @category Combinators */ exports.flatten = /*#__PURE__*/ (0, exports.chain)(function_1.identity); /** * Identifies an associative operation on a type constructor. It is similar to * `Semigroup`, except that it applies to types of kind `* -> *`. * * @since 0.1.0 * @category Alt */ var alt = function (that) { return function (fa) { return Wonka.merge([fa, that()]); }; }; exports.alt = alt; /** * @since 0.1.0 * @category Filterable */ var filterMap = function (f) { return function (fa) { return (0, function_1.pipe)(fa, Wonka.mergeMap(function (a) { return (0, function_1.pipe)(f(a), O.fold(function () { return Wonka.empty; }, exports.of)); })); }; }; exports.filterMap = filterMap; /** * @since 0.1.0 * @category Compactable */ exports.compact = /*#__PURE__*/ (0, exports.filterMap)(function_1.identity); /** * @since 0.1.0 * @category Filterable */ var partitionMap = function (f) { return function (fa) { return ({ left: (0, function_1.pipe)(fa, (0, exports.filterMap)(function (a) { return O.fromEither(E.swap(f(a))); })), right: (0, function_1.pipe)(fa, (0, exports.filterMap)(function (a) { return O.fromEither(f(a)); })), }); }; }; exports.partitionMap = partitionMap; /** * @since 0.1.0 * @category Compactable */ exports.separate = /*#__PURE__*/ (0, exports.partitionMap)(function_1.identity); /** * @since 0.1.0 * @category Filterable */ var filter = function (p) { return function (fa) { return (0, function_1.pipe)(fa, (0, exports.filterMap)(O.fromPredicate(p))); }; }; exports.filter = filter; /** * @since 0.1.0 * @category Filterable */ var partition = function (p) { return function (fa) { return (0, function_1.pipe)(fa, (0, exports.partitionMap)(E.fromPredicate(p, function_1.identity))); }; }; exports.partition = partition; /** * @since 0.1.0 * @category Alternative */ var zero = function () { return Wonka.empty; }; exports.zero = zero; // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- /** * @since 0.1.0 * @category Instances */ exports.URI = 'wonka/Source'; /** * @since 0.1.0 * @category Instances */ var getMonoid = function () { return ({ concat: function (x, y) { return Wonka.merge([x, y]); }, empty: Wonka.empty, }); }; exports.getMonoid = getMonoid; /** * @since 0.1.0 * @category Instances */ exports.Functor = { URI: exports.URI, map: _map, }; /** * @since 0.1.0 * @category Instances */ exports.Pointed = { URI: exports.URI, of: exports.of, }; /** * Derivable from `Functor`. * * @since 0.1.0 * @category Combinators */ exports.flap = /*#__PURE__*/ (0, Functor_1.flap)(exports.Functor); /** * @since 0.1.0 * @category Instances */ exports.Apply = { URI: exports.URI, map: _map, ap: _ap, }; /** * @since 0.1.0 * @category Instances */ exports.Applicative = { URI: exports.URI, map: _map, ap: _ap, of: exports.of, }; /** * @since 0.1.0 * @category Instances */ exports.Chain = { URI: exports.URI, map: _map, ap: _ap, chain: _chain, }; /** * Composes computations in sequence, using the return value of one computation * to determine the next computation and keeping only the result of the first. * * Derivable from `Monad`. * * @since 0.1.0 * @category Combinators */ exports.chainFirst = (0, Chain_1.chainFirst)(exports.Chain); /** * @since 0.1.0 * @category Instances */ exports.Monad = { URI: exports.URI, map: _map, ap: _ap, of: exports.of, chain: _chain, }; /** * @since 0.1.0 * @category Instances */ exports.Alt = { URI: exports.URI, map: _map, alt: _alt, }; /** * @since 0.1.0 * @category Instances */ exports.Alternative = { URI: exports.URI, map: _map, ap: _ap, of: exports.of, alt: _alt, zero: exports.zero, }; /** * @since 0.1.0 * @category Instances */ exports.Compactable = { URI: exports.URI, compact: exports.compact, separate: exports.separate, }; /** * @since 0.1.0 * @category Instances */ exports.Filterable = { URI: exports.URI, compact: exports.compact, separate: exports.separate, map: _map, filter: _filter, filterMap: _filterMap, partition: _partition, partitionMap: _partitionMap, }; /** * @since 0.1.0 * @category Instances */ exports.MonadIO = { URI: exports.URI, map: _map, ap: _ap, of: exports.of, chain: _chain, fromIO: exports.fromIO, }; /** * @since 0.1.0 * @category Instances */ exports.MonadTask = { URI: exports.URI, map: _map, ap: _ap, of: exports.of, chain: _chain, fromIO: exports.fromIO, fromTask: exports.fromTask, }; /** * @since 0.1.0 * @category Instances */ exports.MonadSource = { URI: exports.URI, map: _map, ap: _ap, of: exports.of, chain: _chain, fromIO: exports.fromIO, fromTask: exports.fromTask, fromSource: function_1.identity, }; // ------------------------------------------------------------------------------------- // do notation // ------------------------------------------------------------------------------------- /** @since 0.1.0 */ exports.Do = /*#__PURE__*/ (0, exports.of)({}); /** @since 0.1.0 */ exports.bindTo = /*#__PURE__*/ (0, Functor_1.bindTo)(exports.Functor); /** @since 0.1.0 */ exports.bind = /*#__PURE__*/ (0, Chain_1.bind)(exports.Chain); // ------------------------------------------------------------------------------------- // pipeable sequence S // ------------------------------------------------------------------------------------- /** @since 0.1.0 */ exports.apS = /*#__PURE__*/ (0, Apply_1.apS)(exports.Apply); /** @since 0.1.0 */ exports.apSW = exports.apS; // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- /** @since 0.1.0 */ var toTask = function (s) { return function () { return Wonka.toPromise(s); }; }; exports.toTask = toTask; //# sourceMappingURL=Source.js.map