UNPKG

fp-ts

Version:

Functional programming in TypeScript

333 lines (332 loc) 9.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.apS = exports.bind = exports.bindTo = exports.Do = exports.identity = exports.ChainRec = exports.Comonad = exports.Alt = exports.Traversable = exports.Foldable = exports.Monad = exports.Applicative = exports.Functor = exports.getEq = exports.getShow = exports.URI = exports.alt = exports.altW = exports.sequence = exports.traverse = exports.reduceRight = exports.foldMap = exports.reduce = exports.flatten = exports.duplicate = exports.extract = exports.extend = exports.chainFirst = exports.chain = exports.of = exports.apSecond = exports.apFirst = exports.ap = exports.map = void 0; var ChainRec_1 = require("./ChainRec"); var function_1 = require("./function"); // ------------------------------------------------------------------------------------- // non-pipeables // ------------------------------------------------------------------------------------- var map_ = function (fa, f) { return function_1.pipe(fa, exports.map(f)); }; var ap_ = function (fab, fa) { return function_1.pipe(fab, exports.ap(fa)); }; var chain_ = function (ma, f) { return function_1.pipe(ma, exports.chain(f)); }; /* istanbul ignore next */ var reduce_ = function (fa, b, f) { return function_1.pipe(fa, exports.reduce(b, f)); }; /* istanbul ignore next */ var foldMap_ = function (M) { return function (fa, f) { return function_1.pipe(fa, exports.foldMap(M)(f)); }; }; /* istanbul ignore next */ var reduceRight_ = function (fa, b, f) { return function_1.pipe(fa, exports.reduceRight(b, f)); }; /* istanbul ignore next */ var alt_ = function (fa, that) { return function_1.pipe(fa, exports.alt(that)); }; /* istanbul ignore next */ var extend_ = function (wa, f) { return function_1.pipe(wa, exports.extend(f)); }; /* istanbul ignore next */ var traverse_ = function (F) { var traverseF = exports.traverse(F); return function (ta, f) { return function_1.pipe(ta, traverseF(f)); }; }; var chainRec_ = ChainRec_1.tailRec; // ------------------------------------------------------------------------------------- // pipeables // ------------------------------------------------------------------------------------- /** * `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. * * @category Functor * @since 2.0.0 */ var map = function (f) { return function (fa) { return f(fa); }; }; exports.map = map; /** * Apply a function to an argument under a type constructor. * * @category Apply * @since 2.0.0 */ var ap = function (fa) { return function (fab) { return fab(fa); }; }; exports.ap = ap; /** * Combine two effectful actions, keeping only the result of the first. * * Derivable from `Apply`. * * @category combinators * @since 2.0.0 */ var apFirst = function (fb) { return function_1.flow(exports.map(function (a) { return function () { return a; }; }), exports.ap(fb)); }; exports.apFirst = apFirst; /** * Combine two effectful actions, keeping only the result of the second. * * Derivable from `Apply`. * * @category combinators * @since 2.0.0 */ var apSecond = function (fb) { return function_1.flow(exports.map(function () { return function (b) { return b; }; }), exports.ap(fb)); }; exports.apSecond = apSecond; /** * Wrap a value into the type constructor. * * @category Applicative * @since 2.0.0 */ exports.of = function_1.identity; /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * * @category Monad * @since 2.0.0 */ var chain = function (f) { return function (ma) { return f(ma); }; }; exports.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`. * * @category combinators * @since 2.0.0 */ var chainFirst = function (f) { return exports.chain(function (a) { return function_1.pipe(f(a), exports.map(function () { return a; })); }); }; exports.chainFirst = chainFirst; /** * @category Extend * @since 2.0.0 */ var extend = function (f) { return function (wa) { return f(wa); }; }; exports.extend = extend; /** * @category Extract * @since 2.6.2 */ exports.extract = function_1.identity; /** * Derivable from `Extend`. * * @category combinators * @since 2.0.0 */ exports.duplicate = /*#__PURE__*/ exports.extend(function_1.identity); /** * Derivable from `Monad`. * * @category combinators * @since 2.0.0 */ exports.flatten = /*#__PURE__*/ exports.chain(function_1.identity); /** * @category Foldable * @since 2.0.0 */ var reduce = function (b, f) { return function (fa) { return f(b, fa); }; }; exports.reduce = reduce; /** * @category Foldable * @since 2.0.0 */ var foldMap = function () { return function (f) { return function (fa) { return f(fa); }; }; }; exports.foldMap = foldMap; /** * @category Foldable * @since 2.0.0 */ var reduceRight = function (b, f) { return function (fa) { return f(fa, b); }; }; exports.reduceRight = reduceRight; /** * @since 2.6.3 */ var traverse = function (F) { return function (f) { return function (ta) { return F.map(f(ta), function_1.identity); }; }; }; exports.traverse = traverse; /** * @since 2.6.3 */ var sequence = function (F) { return function (ta) { return F.map(ta, function_1.identity); }; }; exports.sequence = sequence; /** * Less strict version of [`alt`](#alt). * * @category Alt * @since 2.9.0 */ var altW = function () { return function_1.identity; }; exports.altW = altW; /** * 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 = exports.altW; // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- /** * @category instances * @since 2.0.0 */ exports.URI = 'Identity'; /** * @category instances * @since 2.0.0 */ exports.getShow = function_1.identity; /** * @category instances * @since 2.0.0 */ exports.getEq = function_1.identity; /** * @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_ }; /** * @category instances * @since 2.7.0 */ exports.Foldable = { URI: exports.URI, reduce: reduce_, foldMap: foldMap_, reduceRight: reduceRight_ }; /** * @category instances * @since 2.7.0 */ exports.Traversable = { URI: exports.URI, map: map_, reduce: reduce_, foldMap: foldMap_, reduceRight: reduceRight_, traverse: traverse_, sequence: exports.sequence }; /** * @category instances * @since 2.7.0 */ exports.Alt = { URI: exports.URI, map: map_, alt: alt_ }; /** * @category instances * @since 2.7.0 */ exports.Comonad = { URI: exports.URI, map: map_, extend: extend_, extract: exports.extract }; /** * @category instances * @since 2.7.0 */ exports.ChainRec = { URI: exports.URI, map: map_, ap: ap_, chain: chain_, chainRec: chainRec_ }; // TODO: remove in v3 /** * @category instances * @since 2.0.0 */ exports.identity = { URI: exports.URI, map: map_, ap: ap_, of: exports.of, chain: chain_, reduce: reduce_, foldMap: foldMap_, reduceRight: reduceRight_, traverse: traverse_, sequence: exports.sequence, alt: alt_, extract: exports.extract, extend: extend_, chainRec: chainRec_ }; // ------------------------------------------------------------------------------------- // do notation // ------------------------------------------------------------------------------------- /** * @since 2.9.0 */ exports.Do = /*#__PURE__*/ exports.of({}); /** * @since 2.8.0 */ var bindTo = function (name) { return exports.map(function_1.bindTo_(name)); }; exports.bindTo = bindTo; /** * @since 2.8.0 */ var 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); })); }); }; exports.bind = bind; // ------------------------------------------------------------------------------------- // pipeable sequence S // ------------------------------------------------------------------------------------- /** * @since 2.8.0 */ var 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)); }; exports.apS = apS;