/** * 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 {} * * // The Reader monad, which implements this complicated check. * const isCountCorrect: R.Reader = 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 */ import { Applicative2 } from './Applicative' import { Apply2 } from './Apply' import { Category2 } from './Category' import { Chain2 } from './Chain' import { Choice2 } from './Choice' import { Functor2 } from './Functor' import { Monad2 } from './Monad' import { Monoid } from './Monoid' import { Pointed2 } from './Pointed' import { Profunctor2 } from './Profunctor' import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray' import { Semigroup } from './Semigroup' import { Strong2 } from './Strong' /** * @category model * @since 2.0.0 */ export interface Reader { (r: R): A } /** * Reads the current context * * @category constructors * @since 2.0.0 */ export declare const ask: () => Reader /** * Projects a value from the global context in a Reader * * @category constructors * @since 2.0.0 */ export declare const asks: (f: (r: R) => A) => Reader /** * 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 = pipe( * R.Do, * R.bind('content', () => R.ask()), * R.map(({ content }) => string.size(content)) * ) * * // Calls calculateContentLen after adding a prefix to the Reader content. * const calculateModifiedContentLen: R.Reader = 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 */ export declare const local: (f: (r2: R2) => R1) => (ma: Reader) => Reader /** * 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 */ export declare const asksReaderW: (f: (r1: R1) => Reader) => Reader /** * Effectfully accesses the environment. * * @category constructors * @since 2.11.0 */ export declare const asksReader: (f: (r: R) => Reader) => Reader /** * `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 mapping * @since 2.0.0 */ export declare const map: (f: (a: A) => B) => (fa: Reader) => Reader /** * 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 */ export declare const apW: (fa: Reader) => (fab: Reader B>) => Reader /** * @since 2.0.0 */ export declare const ap: (fa: Reader) => (fab: Reader B>) => Reader /** * @category constructors * @since 2.0.0 */ export declare const of: (a: A) => Reader /** * 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 */ export declare const chainW: (f: (a: A) => Reader) => (ma: Reader) => Reader /** * Composes computations in sequence, using the return value of one computation to determine the next computation. * * @category sequencing * @since 2.0.0 */ export declare const chain: (f: (a: A) => Reader) => (ma: Reader) => Reader /** * 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 */ export declare const flattenW: (mma: Reader>) => Reader /** * @category sequencing * @since 2.0.0 */ export declare const flatten: (mma: Reader>) => Reader /** * @since 2.0.0 */ export declare const compose: (ab: Reader) => (bc: Reader) => Reader /** * @since 2.0.0 */ export declare const promap: (f: (d: D) => E, g: (a: A) => B) => (fea: Reader) => Reader /** * @category constructors * @since 2.0.0 */ export declare const id: Category2['id'] /** * @since 2.10.0 */ export declare const first: Strong2['first'] /** * @since 2.10.0 */ export declare const second: Strong2['second'] /** * @since 2.10.0 */ export declare const left: Choice2['left'] /** * @since 2.10.0 */ export declare const right: Choice2['right'] /** * @category type lambdas * @since 2.0.0 */ export declare const URI = 'Reader' /** * @category type lambdas * @since 2.0.0 */ export declare type URI = typeof URI declare module './HKT' { interface URItoKind2 { readonly [URI]: Reader } } /** * @category instances * @since 2.7.0 */ export declare const Functor: Functor2 /** * @category mapping * @since 2.10.0 */ export declare const flap: (a: A) => (fab: Reader B>) => Reader /** * @category instances * @since 2.10.0 */ export declare const Pointed: Pointed2 /** * @category instances * @since 2.10.0 */ export declare const Apply: Apply2 /** * Combine two effectful actions, keeping only the result of the first. * * @since 2.0.0 */ export declare const apFirst: (second: Reader) => (first: Reader) => Reader /** * 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 */ export declare const apFirstW: (second: Reader) => (first: Reader) => Reader /** * Combine two effectful actions, keeping only the result of the second. * * @since 2.0.0 */ export declare const apSecond: (second: Reader) => (first: Reader) => Reader /** * 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 */ export declare const apSecondW: (second: Reader) => (first: Reader) => Reader /** * @category instances * @since 2.7.0 */ export declare const Applicative: Applicative2 /** * @category instances * @since 2.10.0 */ export declare const Chain: Chain2 /** * @category instances * @since 2.7.0 */ export declare const Monad: Monad2 /** * 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 */ export declare const chainFirst: (f: (a: A) => Reader) => (first: Reader) => Reader /** * 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 */ export declare const chainFirstW: ( f: (a: A) => Reader ) => (ma: Reader) => Reader /** * @category instances * @since 2.7.0 */ export declare const Profunctor: Profunctor2 /** * @category instances * @since 2.7.0 */ export declare const Category: Category2 /** * @category instances * @since 2.8.3 */ export declare const Strong: Strong2 /** * @category instances * @since 2.8.3 */ export declare const Choice: Choice2 /** * @category do notation * @since 2.8.0 */ export declare const bindTo: ( name: N ) => (fa: Reader) => Reader declare const let_: ( name: Exclude, f: (a: A) => B ) => (fa: Reader) => Reader export { /** * @category do notation * @since 2.13.0 */ let_ as let } /** * @category do notation * @since 2.8.0 */ export declare const bind: ( name: Exclude, f: (a: A) => Reader ) => (ma: Reader) => Reader /** * The `W` suffix (short for **W**idening) means that the environment types will be merged. * * @category do notation * @since 2.8.0 */ export declare const bindW: ( name: Exclude, f: (a: A) => Reader ) => (fa: Reader) => Reader< R1 & R2, { readonly [K in keyof A | N]: K extends keyof A ? A[K] : B } > /** * @category do notation * @since 2.9.0 */ export declare const Do: Reader /** * @category do notation * @since 2.8.0 */ export declare const apS: ( name: Exclude, fb: Reader ) => (fa: Reader) => Reader /** * 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 */ export declare const apSW: ( name: Exclude, fb: Reader ) => (fa: Reader) => Reader< R1 & R2, { readonly [K in keyof A | N]: K extends keyof A ? A[K] : B } > /** * @since 2.11.0 */ export declare const ApT: Reader /** * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`. * * @category traversing * @since 2.11.0 */ export declare const traverseReadonlyNonEmptyArrayWithIndex: ( f: (index: number, a: A) => Reader ) => (as: ReadonlyNonEmptyArray) => Reader> /** * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. * * @category traversing * @since 2.11.0 */ export declare const traverseReadonlyArrayWithIndex: ( f: (index: number, a: A) => Reader ) => (as: readonly A[]) => Reader /** * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`. * * @category traversing * @since 2.9.0 */ export declare const traverseArrayWithIndex: ( f: (index: number, a: A) => Reader ) => (as: ReadonlyArray) => Reader> /** * Equivalent to `ReadonlyArray#traverse(Applicative)`. * * @category traversing * @since 2.9.0 */ export declare const traverseArray: ( f: (a: A) => Reader ) => (as: readonly A[]) => Reader /** * Equivalent to `ReadonlyArray#sequence(Applicative)`. * * @category traversing * @since 2.9.0 */ export declare const sequenceArray: (arr: ReadonlyArray>) => Reader> /** * 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 */ export declare const reader: Monad2 & Profunctor2 & Category2 & Strong2 & Choice2 /** * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ export declare const getSemigroup: (S: Semigroup) => Semigroup> /** * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead. * * @category zone of death * @since 2.0.0 * @deprecated */ export declare const getMonoid: (M: Monoid) => Monoid>