564 lines
16 KiB
JavaScript
564 lines
16 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (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.traverseArray = exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex = exports.traverseReadonlyNonEmptyArrayWithIndex = exports.ApT = exports.apSW = exports.apS = exports.Do = exports.bindW = exports.bind = exports.let = exports.bindTo = exports.Choice = exports.Strong = exports.Category = exports.Profunctor = exports.chainFirstW = exports.chainFirst = exports.Monad = exports.Chain = exports.Applicative = exports.apSecondW = exports.apSecond = exports.apFirstW = exports.apFirst = exports.Apply = exports.Pointed = exports.flap = exports.Functor = exports.URI = exports.right = exports.left = exports.second = exports.first = exports.id = exports.promap = exports.compose = exports.flatten = exports.flattenW = exports.chain = exports.chainW = exports.of = exports.ap = exports.apW = exports.map = exports.asksReader = exports.asksReaderW = exports.local = exports.asks = exports.ask = void 0;
|
|
exports.getMonoid = exports.getSemigroup = exports.reader = exports.sequenceArray = void 0;
|
|
/**
|
|
* The `Reader` monad (also called the Environment monad). Represents a computation, which can read values from a shared environment,
|
|
* pass values from function to function, and execute sub-computations in a modified environment.
|
|
* Using `Reader` monad for such computations is often clearer and easier than using the `State` monad.
|
|
*
|
|
* In this example the `Reader` monad provides access to variable bindings. `Bindings` are a map of `number` variables.
|
|
* The variable count contains number of variables in the bindings. You can see how to run a `Reader` monad and retrieve
|
|
* data from it, how to access the `Reader` data with `ask` and `asks`.
|
|
*
|
|
* @example
|
|
* import { pipe } from 'fp-ts/function'
|
|
* import * as O from 'fp-ts/Option'
|
|
* import * as R from 'fp-ts/Reader'
|
|
* import * as RR from 'fp-ts/ReadonlyRecord'
|
|
*
|
|
* interface Bindings extends RR.ReadonlyRecord<string, number> {}
|
|
*
|
|
* // The Reader monad, which implements this complicated check.
|
|
* const isCountCorrect: R.Reader<Bindings, boolean> = pipe(
|
|
* R.Do,
|
|
* R.bind('count', () => R.asks(lookupVar('count'))),
|
|
* R.bind('bindings', () => R.ask()),
|
|
* R.map(({ count, bindings }) => count === RR.size(bindings))
|
|
* )
|
|
*
|
|
* // The selector function to use with 'asks'.
|
|
* // Returns value of the variable with specified name.
|
|
* const lookupVar = (name: string) => (bindings: Bindings): number =>
|
|
* pipe(
|
|
* bindings,
|
|
* RR.lookup(name),
|
|
* O.getOrElse(() => 0)
|
|
* )
|
|
*
|
|
* const sampleBindings: Bindings = { count: 3, a: 1, b: 2 }
|
|
*
|
|
* assert.deepStrictEqual(isCountCorrect(sampleBindings), true)
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
var Applicative_1 = require("./Applicative");
|
|
var Apply_1 = require("./Apply");
|
|
var Chain_1 = require("./Chain");
|
|
var E = __importStar(require("./Either"));
|
|
var function_1 = require("./function");
|
|
var Functor_1 = require("./Functor");
|
|
var _ = __importStar(require("./internal"));
|
|
// -------------------------------------------------------------------------------------
|
|
// constructors
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Reads the current context
|
|
*
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
var ask = function () { return function_1.identity; };
|
|
exports.ask = ask;
|
|
/**
|
|
* Projects a value from the global context in a Reader
|
|
*
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
exports.asks = function_1.identity;
|
|
// -------------------------------------------------------------------------------------
|
|
// combinators
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s
|
|
* `contramap`).
|
|
*
|
|
* @example
|
|
* import { pipe } from 'fp-ts/function'
|
|
* import * as R from 'fp-ts/Reader'
|
|
* import * as string from 'fp-ts/string'
|
|
*
|
|
* const calculateContentLen: R.Reader<string, number> = pipe(
|
|
* R.Do,
|
|
* R.bind('content', () => R.ask<string>()),
|
|
* R.map(({ content }) => string.size(content))
|
|
* )
|
|
*
|
|
* // Calls calculateContentLen after adding a prefix to the Reader content.
|
|
* const calculateModifiedContentLen: R.Reader<string, number> = pipe(
|
|
* calculateContentLen,
|
|
* R.local((s) => 'Prefix ' + s)
|
|
* )
|
|
*
|
|
* const s = '12345'
|
|
*
|
|
* assert.deepStrictEqual(
|
|
* "Modified 's' length: " + calculateModifiedContentLen(s) + '\n' + "Original 's' length: " + calculateContentLen(s),
|
|
* "Modified 's' length: 12\nOriginal 's' length: 5"
|
|
* )
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
var local = function (f) { return function (ma) { return function (r2) {
|
|
return ma(f(r2));
|
|
}; }; };
|
|
exports.local = local;
|
|
/**
|
|
* Less strict version of [`asksReader`](#asksreader).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @category constructors
|
|
* @since 2.11.0
|
|
*/
|
|
var asksReaderW = function (f) {
|
|
return function (r) {
|
|
return f(r)(r);
|
|
};
|
|
};
|
|
exports.asksReaderW = asksReaderW;
|
|
/**
|
|
* Effectfully accesses the environment.
|
|
*
|
|
* @category constructors
|
|
* @since 2.11.0
|
|
*/
|
|
exports.asksReader = exports.asksReaderW;
|
|
/* istanbul ignore next */
|
|
var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
|
|
/* istanbul ignore next */
|
|
var _ap = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); };
|
|
/* istanbul ignore next */
|
|
var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
|
|
var _compose = function (bc, ab) { return (0, function_1.pipe)(bc, (0, exports.compose)(ab)); };
|
|
var _promap = function (fea, f, g) { return (0, function_1.pipe)(fea, (0, exports.promap)(f, g)); };
|
|
/**
|
|
* `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 mapping
|
|
* @since 2.0.0
|
|
*/
|
|
var map = function (f) { return function (fa) { return function (r) { return f(fa(r)); }; }; };
|
|
exports.map = map;
|
|
/**
|
|
* Less strict version of [`ap`](#ap).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @since 2.8.0
|
|
*/
|
|
var apW = function (fa) { return function (fab) { return function (r) {
|
|
return fab(r)(fa(r));
|
|
}; }; };
|
|
exports.apW = apW;
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
exports.ap = exports.apW;
|
|
/**
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
exports.of = function_1.constant;
|
|
/**
|
|
* Less strict version of [`chain`](#chain).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.6.0
|
|
*/
|
|
var chainW = function (f) { return function (fa) { return function (r) {
|
|
return f(fa(r))(r);
|
|
}; }; };
|
|
exports.chainW = chainW;
|
|
/**
|
|
* Composes computations in sequence, using the return value of one computation to determine the next computation.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
exports.chain = exports.chainW;
|
|
/**
|
|
* Less strict version of [`flatten`](#flatten).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.11.0
|
|
*/
|
|
exports.flattenW =
|
|
/*#__PURE__*/ (0, exports.chainW)(function_1.identity);
|
|
/**
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
exports.flatten = exports.flattenW;
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
var compose = function (ab) { return function (bc) { return (0, function_1.flow)(ab, bc); }; };
|
|
exports.compose = compose;
|
|
/**
|
|
* @since 2.0.0
|
|
*/
|
|
var promap = function (f, g) { return function (fea) { return function (a) {
|
|
return g(fea(f(a)));
|
|
}; }; };
|
|
exports.promap = promap;
|
|
/**
|
|
* @category constructors
|
|
* @since 2.0.0
|
|
*/
|
|
var id = function () { return function_1.identity; };
|
|
exports.id = id;
|
|
/**
|
|
* @since 2.10.0
|
|
*/
|
|
var first = function (pab) {
|
|
return function (_a) {
|
|
var a = _a[0], c = _a[1];
|
|
return [pab(a), c];
|
|
};
|
|
};
|
|
exports.first = first;
|
|
/**
|
|
* @since 2.10.0
|
|
*/
|
|
var second = function (pbc) {
|
|
return function (_a) {
|
|
var a = _a[0], b = _a[1];
|
|
return [a, pbc(b)];
|
|
};
|
|
};
|
|
exports.second = second;
|
|
/**
|
|
* @since 2.10.0
|
|
*/
|
|
var left = function (pab) { return E.fold(function (a) { return _.left(pab(a)); }, E.right); };
|
|
exports.left = left;
|
|
/**
|
|
* @since 2.10.0
|
|
*/
|
|
var right = function (pbc) { return E.fold(E.left, function (b) { return _.right(pbc(b)); }); };
|
|
exports.right = right;
|
|
/**
|
|
* @category type lambdas
|
|
* @since 2.0.0
|
|
*/
|
|
exports.URI = 'Reader';
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
exports.Functor = {
|
|
URI: exports.URI,
|
|
map: _map
|
|
};
|
|
/**
|
|
* @category mapping
|
|
* @since 2.10.0
|
|
*/
|
|
exports.flap = (0, Functor_1.flap)(exports.Functor);
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
exports.Pointed = {
|
|
URI: exports.URI,
|
|
of: exports.of
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
exports.Apply = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
ap: _ap
|
|
};
|
|
/**
|
|
* Combine two effectful actions, keeping only the result of the first.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
exports.apFirst = (0, Apply_1.apFirst)(exports.Apply);
|
|
/**
|
|
* Less strict version of [`apFirst`](#apfirst).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @since 2.12.0
|
|
*/
|
|
exports.apFirstW = exports.apFirst;
|
|
/**
|
|
* Combine two effectful actions, keeping only the result of the second.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
exports.apSecond = (0, Apply_1.apSecond)(exports.Apply);
|
|
/**
|
|
* Less strict version of [`apSecond`](#apsecond).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @since 2.12.0
|
|
*/
|
|
exports.apSecondW = exports.apSecond;
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
exports.Applicative = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
ap: _ap,
|
|
of: exports.of
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.10.0
|
|
*/
|
|
exports.Chain = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
ap: _ap,
|
|
chain: _chain
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
exports.Monad = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
of: exports.of,
|
|
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.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.0.0
|
|
*/
|
|
exports.chainFirst =
|
|
/*#__PURE__*/ (0, Chain_1.chainFirst)(exports.Chain);
|
|
/**
|
|
* Less strict version of [`chainFirst`](#chainfirst).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @category sequencing
|
|
* @since 2.11.0
|
|
*/
|
|
exports.chainFirstW = exports.chainFirst;
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
exports.Profunctor = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
promap: _promap
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.7.0
|
|
*/
|
|
exports.Category = {
|
|
URI: exports.URI,
|
|
compose: _compose,
|
|
id: exports.id
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.8.3
|
|
*/
|
|
exports.Strong = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
promap: _promap,
|
|
first: exports.first,
|
|
second: exports.second
|
|
};
|
|
/**
|
|
* @category instances
|
|
* @since 2.8.3
|
|
*/
|
|
exports.Choice = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
promap: _promap,
|
|
left: exports.left,
|
|
right: exports.right
|
|
};
|
|
// -------------------------------------------------------------------------------------
|
|
// do notation
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
|
|
var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
|
|
exports.let = let_;
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
exports.bind = (0, Chain_1.bind)(exports.Chain);
|
|
/**
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
exports.bindW = exports.bind;
|
|
/**
|
|
* @category do notation
|
|
* @since 2.9.0
|
|
*/
|
|
exports.Do = (0, exports.of)(_.emptyRecord);
|
|
/**
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
exports.apS = (0, Apply_1.apS)(exports.Apply);
|
|
/**
|
|
* Less strict version of [`apS`](#aps).
|
|
*
|
|
* The `W` suffix (short for **W**idening) means that the environment types will be merged.
|
|
*
|
|
* @category do notation
|
|
* @since 2.8.0
|
|
*/
|
|
exports.apSW = exports.apS;
|
|
/**
|
|
* @since 2.11.0
|
|
*/
|
|
exports.ApT = (0, exports.of)(_.emptyReadonlyArray);
|
|
// -------------------------------------------------------------------------------------
|
|
// array utils
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
|
|
return function (as) {
|
|
return function (r) {
|
|
var out = [f(0, _.head(as))(r)];
|
|
for (var i = 1; i < as.length; i++) {
|
|
out.push(f(i, as[i])(r));
|
|
}
|
|
return out;
|
|
};
|
|
};
|
|
};
|
|
exports.traverseReadonlyNonEmptyArrayWithIndex = traverseReadonlyNonEmptyArrayWithIndex;
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.11.0
|
|
*/
|
|
var traverseReadonlyArrayWithIndex = function (f) {
|
|
var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndex)(f);
|
|
return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
|
|
};
|
|
exports.traverseReadonlyArrayWithIndex = traverseReadonlyArrayWithIndex;
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex;
|
|
/**
|
|
* Equivalent to `ReadonlyArray#traverse(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
var traverseArray = function (f) { return (0, exports.traverseReadonlyArrayWithIndex)(function (_, a) { return f(a); }); };
|
|
exports.traverseArray = traverseArray;
|
|
/**
|
|
* Equivalent to `ReadonlyArray#sequence(Applicative)`.
|
|
*
|
|
* @category traversing
|
|
* @since 2.9.0
|
|
*/
|
|
exports.sequenceArray =
|
|
/*#__PURE__*/ (0, exports.traverseArray)(function_1.identity);
|
|
// -------------------------------------------------------------------------------------
|
|
// deprecated
|
|
// -------------------------------------------------------------------------------------
|
|
/**
|
|
* This instance is deprecated, use small, specific instances instead.
|
|
* For example if a function needs a `Functor` instance, pass `R.Functor` instead of `R.reader`
|
|
* (where `R` is from `import R from 'fp-ts/Reader'`)
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
exports.reader = {
|
|
URI: exports.URI,
|
|
map: _map,
|
|
of: exports.of,
|
|
ap: _ap,
|
|
chain: _chain,
|
|
promap: _promap,
|
|
compose: _compose,
|
|
id: exports.id,
|
|
first: exports.first,
|
|
second: exports.second,
|
|
left: exports.left,
|
|
right: exports.right
|
|
};
|
|
/**
|
|
* Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
exports.getSemigroup = (0, Apply_1.getApplySemigroup)(exports.Apply);
|
|
/**
|
|
* Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
|
|
*
|
|
* @category zone of death
|
|
* @since 2.0.0
|
|
* @deprecated
|
|
*/
|
|
exports.getMonoid = (0, Applicative_1.getApplicativeMonoid)(exports.Applicative);
|