"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.apS = exports.bind = exports.bindTo = exports.execute = exports.evaluate = exports.execState = exports.evalState = exports.state = exports.Monad = exports.Applicative = exports.Functor = exports.URI = exports.flatten = exports.chainFirst = exports.chain = exports.of = exports.apSecond = exports.apFirst = exports.ap = exports.map = exports.gets = exports.modify = exports.put = exports.get = void 0; /** * @since 2.0.0 */ var function_1 = require("./function"); /* tslint:enable:readonly-array */ // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- /** * Get the current state * * @category constructors * @since 2.0.0 */ exports.get = function () { return function (s) { return [s, s]; }; }; /** * Set the state * * @category constructors * @since 2.0.0 */ exports.put = function (s) { return function () { return [undefined, s]; }; }; /** * Modify the state by applying a function to the current state * * @category constructors * @since 2.0.0 */ exports.modify = function (f) { return function (s) { return [undefined, f(s)]; }; }; /** * Get a value which depends on the current state * * @category constructors * @since 2.0.0 */ exports.gets = function (f) { return function (s) { return [f(s), s]; }; }; // ------------------------------------------------------------------------------------- // 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)); }; /* istanbul ignore next */ var chain_ = function (ma, f) { return function_1.pipe(ma, exports.chain(f)); }; // ------------------------------------------------------------------------------------- // 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 function (fa) { return function (s1) { var _a = fa(s1), a = _a[0], s2 = _a[1]; return [f(a), s2]; }; }; }; /** * Apply a function to an argument under a type constructor. * * @category Apply * @since 2.0.0 */ exports.ap = function (fa) { return function (fab) { return function (s1) { var _a = fab(s1), f = _a[0], s2 = _a[1]; var _b = fa(s2), a = _b[0], s3 = _b[1]; return [f(a), s3]; }; }; }; /** * 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. * * @category Applicative * @since 2.0.0 */ exports.of = function (a) { return function (s) { return [a, s]; }; }; /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * * @category Monad * @since 2.0.0 */ exports.chain = function (f) { return function (ma) { return function (s1) { var _a = ma(s1), a = _a[0], s2 = _a[1]; return f(a)(s2); }; }; }; /** * 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 = function (f) { return exports.chain(function (a) { return function_1.pipe(f(a), exports.map(function () { return a; })); }); }; /** * Derivable from `Monad`. * * @category combinators * @since 2.0.0 */ exports.flatten = /*#__PURE__*/ exports.chain(function_1.identity); // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- /** * @category instances * @since 2.0.0 */ exports.URI = 'State'; /** * @category instances * @since 2.7.0 */ exports.Functor = { URI: exports.URI, map: map_ }; /** * @category instances * @since 2.7.0 */ exports.Applicative = { URI: exports.URI, map: map_, ap: ap_, of: exports.of }; /** * @category instances * @since 2.7.0 */ exports.Monad = { URI: exports.URI, map: map_, ap: ap_, of: exports.of, chain: chain_ }; // TODO: remove in v3 /** * @category instances * @since 2.0.0 */ exports.state = exports.Monad; // ------------------------------------------------------------------------------------- // utils // ------------------------------------------------------------------------------------- /** * Use `evaluate` instead * * @since 2.0.0 * @deprecated */ exports.evalState = function (ma, s) { return ma(s)[0]; }; /** * Use `execute` instead * * @since 2.0.0 * @deprecated */ exports.execState = function (ma, s) { return ma(s)[1]; }; /** * Run a computation in the `State` monad, discarding the final state * * @since 2.8.0 */ exports.evaluate = function (s) { return function (ma) { return ma(s)[0]; }; }; /** * Run a computation in the `State` monad discarding the result * * @since 2.8.0 */ exports.execute = function (s) { return function (ma) { return ma(s)[1]; }; }; // ------------------------------------------------------------------------------------- // do notation // ------------------------------------------------------------------------------------- /** * @since 2.8.0 */ exports.bindTo = function (name) { return exports.map(function_1.bindTo_(name)); }; /** * @since 2.8.0 */ exports.bind = function (name, f) { return exports.chain(function (a) { return function_1.pipe(f(a), exports.map(function (b) { return function_1.bind_(a, name, b); })); }); }; // ------------------------------------------------------------------------------------- // pipeable sequence S // ------------------------------------------------------------------------------------- /** * @since 2.8.0 */ exports.apS = function (name, fb) { return function_1.flow(exports.map(function (a) { return function (b) { return function_1.bind_(a, name, b); }; }), exports.ap(fb)); };