版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

467 regels
13 KiB

  1. /**
  2. * The `Reader` monad (also called the Environment monad). Represents a computation, which can read values from a shared environment,
  3. * pass values from function to function, and execute sub-computations in a modified environment.
  4. * Using `Reader` monad for such computations is often clearer and easier than using the `State` monad.
  5. *
  6. * In this example the `Reader` monad provides access to variable bindings. `Bindings` are a map of `number` variables.
  7. * The variable count contains number of variables in the bindings. You can see how to run a `Reader` monad and retrieve
  8. * data from it, how to access the `Reader` data with `ask` and `asks`.
  9. *
  10. * @example
  11. * import { pipe } from 'fp-ts/function'
  12. * import * as O from 'fp-ts/Option'
  13. * import * as R from 'fp-ts/Reader'
  14. * import * as RR from 'fp-ts/ReadonlyRecord'
  15. *
  16. * interface Bindings extends RR.ReadonlyRecord<string, number> {}
  17. *
  18. * // The Reader monad, which implements this complicated check.
  19. * const isCountCorrect: R.Reader<Bindings, boolean> = pipe(
  20. * R.Do,
  21. * R.bind('count', () => R.asks(lookupVar('count'))),
  22. * R.bind('bindings', () => R.ask()),
  23. * R.map(({ count, bindings }) => count === RR.size(bindings))
  24. * )
  25. *
  26. * // The selector function to use with 'asks'.
  27. * // Returns value of the variable with specified name.
  28. * const lookupVar = (name: string) => (bindings: Bindings): number =>
  29. * pipe(
  30. * bindings,
  31. * RR.lookup(name),
  32. * O.getOrElse(() => 0)
  33. * )
  34. *
  35. * const sampleBindings: Bindings = { count: 3, a: 1, b: 2 }
  36. *
  37. * assert.deepStrictEqual(isCountCorrect(sampleBindings), true)
  38. *
  39. * @since 2.0.0
  40. */
  41. import { Applicative2 } from './Applicative'
  42. import { Apply2 } from './Apply'
  43. import { Category2 } from './Category'
  44. import { Chain2 } from './Chain'
  45. import { Choice2 } from './Choice'
  46. import { Functor2 } from './Functor'
  47. import { Monad2 } from './Monad'
  48. import { Monoid } from './Monoid'
  49. import { Pointed2 } from './Pointed'
  50. import { Profunctor2 } from './Profunctor'
  51. import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray'
  52. import { Semigroup } from './Semigroup'
  53. import { Strong2 } from './Strong'
  54. /**
  55. * @category model
  56. * @since 2.0.0
  57. */
  58. export interface Reader<R, A> {
  59. (r: R): A
  60. }
  61. /**
  62. * Reads the current context
  63. *
  64. * @category constructors
  65. * @since 2.0.0
  66. */
  67. export declare const ask: <R>() => Reader<R, R>
  68. /**
  69. * Projects a value from the global context in a Reader
  70. *
  71. * @category constructors
  72. * @since 2.0.0
  73. */
  74. export declare const asks: <R, A>(f: (r: R) => A) => Reader<R, A>
  75. /**
  76. * Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s
  77. * `contramap`).
  78. *
  79. * @example
  80. * import { pipe } from 'fp-ts/function'
  81. * import * as R from 'fp-ts/Reader'
  82. * import * as string from 'fp-ts/string'
  83. *
  84. * const calculateContentLen: R.Reader<string, number> = pipe(
  85. * R.Do,
  86. * R.bind('content', () => R.ask<string>()),
  87. * R.map(({ content }) => string.size(content))
  88. * )
  89. *
  90. * // Calls calculateContentLen after adding a prefix to the Reader content.
  91. * const calculateModifiedContentLen: R.Reader<string, number> = pipe(
  92. * calculateContentLen,
  93. * R.local((s) => 'Prefix ' + s)
  94. * )
  95. *
  96. * const s = '12345'
  97. *
  98. * assert.deepStrictEqual(
  99. * "Modified 's' length: " + calculateModifiedContentLen(s) + '\n' + "Original 's' length: " + calculateContentLen(s),
  100. * "Modified 's' length: 12\nOriginal 's' length: 5"
  101. * )
  102. *
  103. * @since 2.0.0
  104. */
  105. export declare const local: <R2, R1>(f: (r2: R2) => R1) => <A>(ma: Reader<R1, A>) => Reader<R2, A>
  106. /**
  107. * Less strict version of [`asksReader`](#asksreader).
  108. *
  109. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  110. *
  111. * @category constructors
  112. * @since 2.11.0
  113. */
  114. export declare const asksReaderW: <R1, R2, A>(f: (r1: R1) => Reader<R2, A>) => Reader<R1 & R2, A>
  115. /**
  116. * Effectfully accesses the environment.
  117. *
  118. * @category constructors
  119. * @since 2.11.0
  120. */
  121. export declare const asksReader: <R, A>(f: (r: R) => Reader<R, A>) => Reader<R, A>
  122. /**
  123. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  124. * use the type constructor `F` to represent some computational context.
  125. *
  126. * @category mapping
  127. * @since 2.0.0
  128. */
  129. export declare const map: <A, B>(f: (a: A) => B) => <R>(fa: Reader<R, A>) => Reader<R, B>
  130. /**
  131. * Less strict version of [`ap`](#ap).
  132. *
  133. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  134. *
  135. * @since 2.8.0
  136. */
  137. export declare const apW: <R2, A>(fa: Reader<R2, A>) => <R1, B>(fab: Reader<R1, (a: A) => B>) => Reader<R1 & R2, B>
  138. /**
  139. * @since 2.0.0
  140. */
  141. export declare const ap: <R, A>(fa: Reader<R, A>) => <B>(fab: Reader<R, (a: A) => B>) => Reader<R, B>
  142. /**
  143. * @category constructors
  144. * @since 2.0.0
  145. */
  146. export declare const of: <R = unknown, A = never>(a: A) => Reader<R, A>
  147. /**
  148. * Less strict version of [`chain`](#chain).
  149. *
  150. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  151. *
  152. * @category sequencing
  153. * @since 2.6.0
  154. */
  155. export declare const chainW: <R2, A, B>(f: (a: A) => Reader<R2, B>) => <R1>(ma: Reader<R1, A>) => Reader<R1 & R2, B>
  156. /**
  157. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  158. *
  159. * @category sequencing
  160. * @since 2.0.0
  161. */
  162. export declare const chain: <A, R, B>(f: (a: A) => Reader<R, B>) => (ma: Reader<R, A>) => Reader<R, B>
  163. /**
  164. * Less strict version of [`flatten`](#flatten).
  165. *
  166. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  167. *
  168. * @category sequencing
  169. * @since 2.11.0
  170. */
  171. export declare const flattenW: <R1, R2, A>(mma: Reader<R1, Reader<R2, A>>) => Reader<R1 & R2, A>
  172. /**
  173. * @category sequencing
  174. * @since 2.0.0
  175. */
  176. export declare const flatten: <R, A>(mma: Reader<R, Reader<R, A>>) => Reader<R, A>
  177. /**
  178. * @since 2.0.0
  179. */
  180. export declare const compose: <A, B>(ab: Reader<A, B>) => <C>(bc: Reader<B, C>) => Reader<A, C>
  181. /**
  182. * @since 2.0.0
  183. */
  184. export declare const promap: <E, A, D, B>(f: (d: D) => E, g: (a: A) => B) => (fea: Reader<E, A>) => Reader<D, B>
  185. /**
  186. * @category constructors
  187. * @since 2.0.0
  188. */
  189. export declare const id: Category2<URI>['id']
  190. /**
  191. * @since 2.10.0
  192. */
  193. export declare const first: Strong2<URI>['first']
  194. /**
  195. * @since 2.10.0
  196. */
  197. export declare const second: Strong2<URI>['second']
  198. /**
  199. * @since 2.10.0
  200. */
  201. export declare const left: Choice2<URI>['left']
  202. /**
  203. * @since 2.10.0
  204. */
  205. export declare const right: Choice2<URI>['right']
  206. /**
  207. * @category type lambdas
  208. * @since 2.0.0
  209. */
  210. export declare const URI = 'Reader'
  211. /**
  212. * @category type lambdas
  213. * @since 2.0.0
  214. */
  215. export declare type URI = typeof URI
  216. declare module './HKT' {
  217. interface URItoKind2<E, A> {
  218. readonly [URI]: Reader<E, A>
  219. }
  220. }
  221. /**
  222. * @category instances
  223. * @since 2.7.0
  224. */
  225. export declare const Functor: Functor2<URI>
  226. /**
  227. * @category mapping
  228. * @since 2.10.0
  229. */
  230. export declare const flap: <A>(a: A) => <E, B>(fab: Reader<E, (a: A) => B>) => Reader<E, B>
  231. /**
  232. * @category instances
  233. * @since 2.10.0
  234. */
  235. export declare const Pointed: Pointed2<URI>
  236. /**
  237. * @category instances
  238. * @since 2.10.0
  239. */
  240. export declare const Apply: Apply2<URI>
  241. /**
  242. * Combine two effectful actions, keeping only the result of the first.
  243. *
  244. * @since 2.0.0
  245. */
  246. export declare const apFirst: <E, B>(second: Reader<E, B>) => <A>(first: Reader<E, A>) => Reader<E, A>
  247. /**
  248. * Less strict version of [`apFirst`](#apfirst).
  249. *
  250. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  251. *
  252. * @since 2.12.0
  253. */
  254. export declare const apFirstW: <R2, B>(second: Reader<R2, B>) => <R1, A>(first: Reader<R1, A>) => Reader<R1 & R2, A>
  255. /**
  256. * Combine two effectful actions, keeping only the result of the second.
  257. *
  258. * @since 2.0.0
  259. */
  260. export declare const apSecond: <E, B>(second: Reader<E, B>) => <A>(first: Reader<E, A>) => Reader<E, B>
  261. /**
  262. * Less strict version of [`apSecond`](#apsecond).
  263. *
  264. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  265. *
  266. * @since 2.12.0
  267. */
  268. export declare const apSecondW: <R2, B>(second: Reader<R2, B>) => <R1, A>(first: Reader<R1, A>) => Reader<R1 & R2, B>
  269. /**
  270. * @category instances
  271. * @since 2.7.0
  272. */
  273. export declare const Applicative: Applicative2<URI>
  274. /**
  275. * @category instances
  276. * @since 2.10.0
  277. */
  278. export declare const Chain: Chain2<URI>
  279. /**
  280. * @category instances
  281. * @since 2.7.0
  282. */
  283. export declare const Monad: Monad2<URI>
  284. /**
  285. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  286. * keeping only the result of the first.
  287. *
  288. * @category sequencing
  289. * @since 2.0.0
  290. */
  291. export declare const chainFirst: <A, R, B>(f: (a: A) => Reader<R, B>) => (first: Reader<R, A>) => Reader<R, A>
  292. /**
  293. * Less strict version of [`chainFirst`](#chainfirst).
  294. *
  295. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  296. *
  297. * @category sequencing
  298. * @since 2.11.0
  299. */
  300. export declare const chainFirstW: <R2, A, B>(
  301. f: (a: A) => Reader<R2, B>
  302. ) => <R1>(ma: Reader<R1, A>) => Reader<R1 & R2, A>
  303. /**
  304. * @category instances
  305. * @since 2.7.0
  306. */
  307. export declare const Profunctor: Profunctor2<URI>
  308. /**
  309. * @category instances
  310. * @since 2.7.0
  311. */
  312. export declare const Category: Category2<URI>
  313. /**
  314. * @category instances
  315. * @since 2.8.3
  316. */
  317. export declare const Strong: Strong2<URI>
  318. /**
  319. * @category instances
  320. * @since 2.8.3
  321. */
  322. export declare const Choice: Choice2<URI>
  323. /**
  324. * @category do notation
  325. * @since 2.8.0
  326. */
  327. export declare const bindTo: <N extends string>(
  328. name: N
  329. ) => <E, A>(fa: Reader<E, A>) => Reader<E, { readonly [K in N]: A }>
  330. declare const let_: <N extends string, A, B>(
  331. name: Exclude<N, keyof A>,
  332. f: (a: A) => B
  333. ) => <E>(fa: Reader<E, A>) => Reader<E, { readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  334. export {
  335. /**
  336. * @category do notation
  337. * @since 2.13.0
  338. */
  339. let_ as let
  340. }
  341. /**
  342. * @category do notation
  343. * @since 2.8.0
  344. */
  345. export declare const bind: <N extends string, A, E, B>(
  346. name: Exclude<N, keyof A>,
  347. f: (a: A) => Reader<E, B>
  348. ) => (ma: Reader<E, A>) => Reader<E, { readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  349. /**
  350. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  351. *
  352. * @category do notation
  353. * @since 2.8.0
  354. */
  355. export declare const bindW: <N extends string, A, R2, B>(
  356. name: Exclude<N, keyof A>,
  357. f: (a: A) => Reader<R2, B>
  358. ) => <R1>(fa: Reader<R1, A>) => Reader<
  359. R1 & R2,
  360. {
  361. readonly [K in keyof A | N]: K extends keyof A ? A[K] : B
  362. }
  363. >
  364. /**
  365. * @category do notation
  366. * @since 2.9.0
  367. */
  368. export declare const Do: Reader<unknown, {}>
  369. /**
  370. * @category do notation
  371. * @since 2.8.0
  372. */
  373. export declare const apS: <N extends string, A, E, B>(
  374. name: Exclude<N, keyof A>,
  375. fb: Reader<E, B>
  376. ) => (fa: Reader<E, A>) => Reader<E, { readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  377. /**
  378. * Less strict version of [`apS`](#aps).
  379. *
  380. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  381. *
  382. * @category do notation
  383. * @since 2.8.0
  384. */
  385. export declare const apSW: <A, N extends string, R2, B>(
  386. name: Exclude<N, keyof A>,
  387. fb: Reader<R2, B>
  388. ) => <R1>(fa: Reader<R1, A>) => Reader<
  389. R1 & R2,
  390. {
  391. readonly [K in keyof A | N]: K extends keyof A ? A[K] : B
  392. }
  393. >
  394. /**
  395. * @since 2.11.0
  396. */
  397. export declare const ApT: Reader<unknown, readonly []>
  398. /**
  399. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
  400. *
  401. * @category traversing
  402. * @since 2.11.0
  403. */
  404. export declare const traverseReadonlyNonEmptyArrayWithIndex: <A, R, B>(
  405. f: (index: number, a: A) => Reader<R, B>
  406. ) => (as: ReadonlyNonEmptyArray<A>) => Reader<R, ReadonlyNonEmptyArray<B>>
  407. /**
  408. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  409. *
  410. * @category traversing
  411. * @since 2.11.0
  412. */
  413. export declare const traverseReadonlyArrayWithIndex: <A, R, B>(
  414. f: (index: number, a: A) => Reader<R, B>
  415. ) => (as: readonly A[]) => Reader<R, readonly B[]>
  416. /**
  417. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  418. *
  419. * @category traversing
  420. * @since 2.9.0
  421. */
  422. export declare const traverseArrayWithIndex: <R, A, B>(
  423. f: (index: number, a: A) => Reader<R, B>
  424. ) => (as: ReadonlyArray<A>) => Reader<R, ReadonlyArray<B>>
  425. /**
  426. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  427. *
  428. * @category traversing
  429. * @since 2.9.0
  430. */
  431. export declare const traverseArray: <R, A, B>(
  432. f: (a: A) => Reader<R, B>
  433. ) => (as: readonly A[]) => Reader<R, readonly B[]>
  434. /**
  435. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  436. *
  437. * @category traversing
  438. * @since 2.9.0
  439. */
  440. export declare const sequenceArray: <R, A>(arr: ReadonlyArray<Reader<R, A>>) => Reader<R, ReadonlyArray<A>>
  441. /**
  442. * This instance is deprecated, use small, specific instances instead.
  443. * For example if a function needs a `Functor` instance, pass `R.Functor` instead of `R.reader`
  444. * (where `R` is from `import R from 'fp-ts/Reader'`)
  445. *
  446. * @category zone of death
  447. * @since 2.0.0
  448. * @deprecated
  449. */
  450. export declare const reader: Monad2<URI> & Profunctor2<URI> & Category2<URI> & Strong2<URI> & Choice2<URI>
  451. /**
  452. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  453. *
  454. * @category zone of death
  455. * @since 2.0.0
  456. * @deprecated
  457. */
  458. export declare const getSemigroup: <R, A>(S: Semigroup<A>) => Semigroup<Reader<R, A>>
  459. /**
  460. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  461. *
  462. * @category zone of death
  463. * @since 2.0.0
  464. * @deprecated
  465. */
  466. export declare const getMonoid: <R, A>(M: Monoid<A>) => Monoid<Reader<R, A>>