"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.apS = exports.apSW = exports.bind = exports.bindW = exports.bindTo = exports.bracket = exports.ioEither = exports.MonadThrow = exports.MonadIO = exports.Alt = exports.Monad = exports.Applicative = exports.ApplicativeSeq = exports.ApplicativePar = exports.Bifunctor = exports.Functor = exports.getFilterable = exports.getIOValidation = exports.getAltIOValidation = exports.getApplicativeIOValidation = exports.getApplyMonoid = exports.getApplySemigroup = exports.getSemigroup = exports.URI = exports.throwError = exports.fromIO = exports.alt = exports.flatten = exports.chainFirst = exports.chainFirstW = exports.chain = exports.chainW = exports.of = exports.apSecond = exports.apFirst = exports.ap = exports.apW = exports.mapLeft = exports.bimap = exports.map = exports.chainEitherK = exports.chainEitherKW = exports.fromEitherK = exports.filterOrElse = exports.swap = exports.orElse = exports.getOrElse = exports.getOrElseW = exports.fold = exports.tryCatch = exports.fromPredicate = exports.fromOption = exports.fromEither = exports.leftIO = exports.rightIO = exports.right = exports.left = void 0; var E = __importStar(require("./Either")); var Filterable_1 = require("./Filterable"); var function_1 = require("./function"); var I = __importStar(require("./IO")); // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- /** * @category constructors * @since 2.0.0 */ exports.left = /*#__PURE__*/ function_1.flow(E.left, I.of); /** * @category constructors * @since 2.0.0 */ exports.right = /*#__PURE__*/ function_1.flow(E.right, I.of); /** * @category constructors * @since 2.0.0 */ exports.rightIO = /*#__PURE__*/ I.map(E.right); /** * @category constructors * @since 2.0.0 */ exports.leftIO = /*#__PURE__*/ I.map(E.left); /** * Derivable from `MonadThrow`. * * @category constructors * @since 2.0.0 */ exports.fromEither = /*#__PURE__*/ E.fold(exports.left, function (a) { return exports.right(a); }); /** * Derivable from `MonadThrow`. * * @category constructors * @since 2.0.0 */ exports.fromOption = function (onNone) { return function (ma) { return ma._tag === 'None' ? exports.left(onNone()) : exports.right(ma.value); }; }; /** * Derivable from `MonadThrow`. * * @category constructors * @since 2.0.0 */ exports.fromPredicate = function (predicate, onFalse) { return function (a) { return (predicate(a) ? exports.right(a) : exports.left(onFalse(a))); }; }; /** * Constructs a new `IOEither` from a function that performs a side effect and might throw * * @category constructors * @since 2.0.0 */ function tryCatch(f, onError) { return function () { return E.tryCatch(f, onError); }; } exports.tryCatch = tryCatch; // ------------------------------------------------------------------------------------- // destructors // ------------------------------------------------------------------------------------- /** * @category destructors * @since 2.0.0 */ exports.fold = /*#__PURE__*/ function_1.flow(E.fold, I.chain); /** * Less strict version of [`getOrElse`](#getOrElse). * * @category destructors * @since 2.6.0 */ exports.getOrElseW = function (onLeft) { return function (ma) { return function_1.pipe(ma, I.chain(E.fold(onLeft, I.of))); }; }; /** * @category destructors * @since 2.0.0 */ exports.getOrElse = exports.getOrElseW; // ------------------------------------------------------------------------------------- // combinators // ------------------------------------------------------------------------------------- /** * @category combinators * @since 2.0.0 */ exports.orElse = function (f) { return I.chain(E.fold(f, exports.right)); }; /** * @category combinators * @since 2.0.0 */ exports.swap = /*#__PURE__*/ I.map(E.swap); /** * Derivable from `MonadThrow`. * * @category combinators * @since 2.0.0 */ exports.filterOrElse = function (predicate, onFalse) { return exports.chain(function (a) { return (predicate(a) ? exports.right(a) : exports.left(onFalse(a))); }); }; /** * @category combinators * @since 2.4.0 */ function fromEitherK(f) { return function () { var a = []; for (var _i = 0; _i < arguments.length; _i++) { a[_i] = arguments[_i]; } return exports.fromEither(f.apply(void 0, a)); }; } exports.fromEitherK = fromEitherK; /** * Less strict version of [`chainEitherK`](#chainEitherK). * * @category combinators * @since 2.6.1 */ exports.chainEitherKW = function (f) { return exports.chainW(fromEitherK(f)); }; /** * @category combinators * @since 2.4.0 */ exports.chainEitherK = exports.chainEitherKW; // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- /* istanbul ignore next */ var map_ = function (fa, f) { return function_1.pipe(fa, exports.map(f)); }; /* istanbul ignore next */ var ap_ = function (fab, fa) { return function_1.pipe(fab, exports.ap(fa)); }; var apSeq_ = function (fab, fa) { return function_1.pipe(fab, exports.chain(function (f) { return function_1.pipe(fa, exports.map(f)); })); }; /* istanbul ignore next */ var chain_ = function (ma, f) { return function_1.pipe(ma, exports.chain(f)); }; /* istanbul ignore next */ var bimap_ = function (fa, f, g) { return function_1.pipe(fa, exports.bimap(f, g)); }; /* istanbul ignore next */ var mapLeft_ = function (fa, f) { return function_1.pipe(fa, exports.mapLeft(f)); }; /* istanbul ignore next */ var alt_ = function (fa, that) { return function_1.pipe(fa, exports.alt(that)); }; // ------------------------------------------------------------------------------------- // pipeables // ------------------------------------------------------------------------------------- /** * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types * use the type constructor `F` to represent some computational context. * * @category Functor * @since 2.0.0 */ exports.map = function (f) { return I.map(E.map(f)); }; /** * Map a pair of functions over the two type arguments of the bifunctor. * * @category Bifunctor * @since 2.0.0 */ exports.bimap = /*#__PURE__*/ function_1.flow(E.bimap, I.map); /** * Map a function over the first type argument of a bifunctor. * * @category Bifunctor * @since 2.0.0 */ exports.mapLeft = function (f) { return I.map(E.mapLeft(f)); }; /** * Less strict version of [`ap`](#ap). * * @category Apply * @since 2.8.0 */ exports.apW = function (fa) { return function_1.flow(I.map(function (gab) { return function (ga) { return E.apW(ga)(gab); }; }), I.ap(fa)); }; /** * Apply a function to an argument under a type constructor. * * @category Apply * @since 2.0.0 */ exports.ap = exports.apW; /** * Combine two effectful actions, keeping only the result of the first. * * Derivable from `Apply`. * * @category combinators * @since 2.0.0 */ exports.apFirst = function (fb) { return function_1.flow(exports.map(function (a) { return function () { return a; }; }), exports.ap(fb)); }; /** * Combine two effectful actions, keeping only the result of the second. * * Derivable from `Apply`. * * @category combinators * @since 2.0.0 */ exports.apSecond = function (fb) { return function_1.flow(exports.map(function () { return function (b) { return b; }; }), exports.ap(fb)); }; /** * Wrap a value into the type constructor. * * Equivalent to [`right`](#right). * * @category Applicative * @since 2.8.5 */ exports.of = exports.right; /** * Less strict version of [`chain`](#chain). * * @category Monad * @since 2.6.0 */ exports.chainW = function (f) { return function (ma) { return function_1.pipe(ma, I.chain(E.fold(exports.left, f))); }; }; /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * * @category Monad * @since 2.0.0 */ exports.chain = exports.chainW; /** * Less strict version of [`chainFirst`](#chainFirst). * * Derivable from `Monad`. * * @category combinators * @since 2.8.0 */ exports.chainFirstW = function (f) { return exports.chainW(function (a) { return function_1.pipe(f(a), exports.map(function () { return a; })); }); }; /** * 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`. * * @category combinators * @since 2.0.0 */ exports.chainFirst = exports.chainFirstW; /** * Derivable from `Monad`. * * @category combinators * @since 2.0.0 */ exports.flatten = /*#__PURE__*/ 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 `* -> *`. * * @category Alt * @since 2.0.0 */ exports.alt = function (that) { return I.chain(E.fold(that, exports.right)); }; /** * @category MonadIO * @since 2.7.0 */ exports.fromIO = exports.rightIO; /** * @category MonadThrow * @since 2.7.0 */ exports.throwError = exports.left; // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- /** * @category instances * @since 2.0.0 */ exports.URI = 'IOEither'; /** * Semigroup returning the left-most non-`Left` value. If both operands are `Right`s then the inner values are * concatenated using the provided `Semigroup` * * @category instances * @since 2.0.0 */ function getSemigroup(S) { return I.getSemigroup(E.getSemigroup(S)); } exports.getSemigroup = getSemigroup; /** * Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values * are concatenated using the provided `Semigroup` * * @category instances * @since 2.0.0 */ function getApplySemigroup(S) { return I.getSemigroup(E.getApplySemigroup(S)); } exports.getApplySemigroup = getApplySemigroup; /** * @category instances * @since 2.0.0 */ function getApplyMonoid(M) { return { concat: getApplySemigroup(M).concat, empty: exports.right(M.empty) }; } exports.getApplyMonoid = getApplyMonoid; /** * @category instances * @since 2.7.0 */ function getApplicativeIOValidation(SE) { var AV = E.getApplicativeValidation(SE); var ap = function (fga) { return function_1.flow(I.map(function (gab) { return function (ga) { return AV.ap(gab, ga); }; }), I.ap(fga)); }; return { URI: exports.URI, _E: undefined, map: map_, ap: function (fab, fa) { return function_1.pipe(fab, ap(fa)); }, of: exports.of }; } exports.getApplicativeIOValidation = getApplicativeIOValidation; /** * @category instances * @since 2.7.0 */ function getAltIOValidation(SE) { var A = E.getAltValidation(SE); return { URI: exports.URI, _E: undefined, map: map_, alt: function (me, that) { return function () { return A.alt(me(), function () { return that()(); }); }; } }; } exports.getAltIOValidation = getAltIOValidation; // TODO: remove in v3 /** * @category instances * @since 2.0.0 */ function getIOValidation(SE) { var applicativeIOValidation = getApplicativeIOValidation(SE); var altIOValidation = getAltIOValidation(SE); return { URI: exports.URI, _E: undefined, map: map_, ap: applicativeIOValidation.ap, of: exports.of, chain: chain_, bimap: bimap_, mapLeft: mapLeft_, alt: altIOValidation.alt, fromIO: exports.fromIO, throwError: exports.throwError }; } exports.getIOValidation = getIOValidation; /** * @category instances * @since 2.1.0 */ function getFilterable(M) { var W = E.getWitherable(M); var F = Filterable_1.getFilterableComposition(I.Monad, W); return { URI: exports.URI, _E: undefined, map: map_, compact: F.compact, separate: F.separate, filter: F.filter, filterMap: F.filterMap, partition: F.partition, partitionMap: F.partitionMap }; } exports.getFilterable = getFilterable; /** * @category instances * @since 2.7.0 */ exports.Functor = { URI: exports.URI, map: map_ }; /** * @category instances * @since 2.7.0 */ exports.Bifunctor = { URI: exports.URI, bimap: bimap_, mapLeft: mapLeft_ }; /** * @category instances * @since 2.8.4 */ exports.ApplicativePar = { URI: exports.URI, map: map_, ap: ap_, of: exports.of }; /** * @category instances * @since 2.8.4 */ exports.ApplicativeSeq = { URI: exports.URI, map: map_, ap: apSeq_, of: exports.of }; /** * Use `ApplicativePar` instead * * @since 2.7.0 * @category instances * @deprecated */ exports.Applicative = exports.ApplicativePar; /** * @category instances * @since 2.7.0 */ exports.Monad = { URI: exports.URI, map: map_, ap: ap_, of: exports.of, chain: chain_ }; /** * @category instances * @since 2.7.0 */ exports.Alt = { URI: exports.URI, map: map_, alt: alt_ }; /** * @category instances * @since 2.7.0 */ exports.MonadIO = { URI: exports.URI, map: map_, ap: ap_, of: exports.of, chain: chain_, fromIO: exports.fromIO }; /** * @category instances * @since 2.7.0 */ exports.MonadThrow = { URI: exports.URI, map: map_, ap: ap_, of: exports.of, chain: chain_, throwError: exports.throwError }; // TODO: remove in v3 /** * @category instances * @since 2.0.0 */ exports.ioEither = { URI: exports.URI, bimap: bimap_, mapLeft: mapLeft_, map: map_, of: exports.of, ap: ap_, chain: chain_, alt: alt_, fromIO: exports.fromIO, throwError: exports.throwError }; // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- /** * Make sure that a resource is cleaned up in the event of an exception (\*). The release action is called regardless of * whether the body action throws (\*) or returns. * * (\*) i.e. returns a `Left` * * Derivable from `MonadThrow`. * * @since 2.0.0 */ exports.bracket = function (acquire, use, release) { return function_1.pipe(acquire, exports.chain(function (a) { return function_1.pipe(function_1.pipe(use(a), I.map(E.right)), exports.chain(function (e) { return function_1.pipe(release(a, e), exports.chain(function () { return (E.isLeft(e) ? exports.left(e.left) : exports.of(e.right)); })); })); })); }; // ------------------------------------------------------------------------------------- // do notation // ------------------------------------------------------------------------------------- /** * @since 2.8.0 */ exports.bindTo = function (name) { return exports.map(function_1.bindTo_(name)); }; /** * @since 2.8.0 */ exports.bindW = function (name, f) { return exports.chainW(function (a) { return function_1.pipe(f(a), exports.map(function (b) { return function_1.bind_(a, name, b); })); }); }; /** * @since 2.8.0 */ exports.bind = exports.bindW; // ------------------------------------------------------------------------------------- // pipeable sequence S // ------------------------------------------------------------------------------------- /** * @since 2.8.0 */ exports.apSW = function (name, fb) { return function_1.flow(exports.map(function (a) { return function (b) { return function_1.bind_(a, name, b); }; }), exports.apW(fb)); }; /** * @since 2.8.0 */ exports.apS = exports.apSW;