版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

526 wiersze
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 { getApplicativeMonoid } from './Applicative';
  42. import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_, getApplySemigroup } from './Apply';
  43. import { bind as bind_, chainFirst as chainFirst_ } from './Chain';
  44. import * as E from './Either';
  45. import { constant, flow, identity, pipe } from './function';
  46. import { let as let__, bindTo as bindTo_, flap as flap_ } from './Functor';
  47. import * as _ from './internal';
  48. // -------------------------------------------------------------------------------------
  49. // constructors
  50. // -------------------------------------------------------------------------------------
  51. /**
  52. * Reads the current context
  53. *
  54. * @category constructors
  55. * @since 2.0.0
  56. */
  57. export var ask = function () { return identity; };
  58. /**
  59. * Projects a value from the global context in a Reader
  60. *
  61. * @category constructors
  62. * @since 2.0.0
  63. */
  64. export var asks = identity;
  65. // -------------------------------------------------------------------------------------
  66. // combinators
  67. // -------------------------------------------------------------------------------------
  68. /**
  69. * Changes the value of the local context during the execution of the action `ma` (similar to `Contravariant`'s
  70. * `contramap`).
  71. *
  72. * @example
  73. * import { pipe } from 'fp-ts/function'
  74. * import * as R from 'fp-ts/Reader'
  75. * import * as string from 'fp-ts/string'
  76. *
  77. * const calculateContentLen: R.Reader<string, number> = pipe(
  78. * R.Do,
  79. * R.bind('content', () => R.ask<string>()),
  80. * R.map(({ content }) => string.size(content))
  81. * )
  82. *
  83. * // Calls calculateContentLen after adding a prefix to the Reader content.
  84. * const calculateModifiedContentLen: R.Reader<string, number> = pipe(
  85. * calculateContentLen,
  86. * R.local((s) => 'Prefix ' + s)
  87. * )
  88. *
  89. * const s = '12345'
  90. *
  91. * assert.deepStrictEqual(
  92. * "Modified 's' length: " + calculateModifiedContentLen(s) + '\n' + "Original 's' length: " + calculateContentLen(s),
  93. * "Modified 's' length: 12\nOriginal 's' length: 5"
  94. * )
  95. *
  96. * @since 2.0.0
  97. */
  98. export var local = function (f) { return function (ma) { return function (r2) {
  99. return ma(f(r2));
  100. }; }; };
  101. /**
  102. * Less strict version of [`asksReader`](#asksreader).
  103. *
  104. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  105. *
  106. * @category constructors
  107. * @since 2.11.0
  108. */
  109. export var asksReaderW = function (f) {
  110. return function (r) {
  111. return f(r)(r);
  112. };
  113. };
  114. /**
  115. * Effectfully accesses the environment.
  116. *
  117. * @category constructors
  118. * @since 2.11.0
  119. */
  120. export var asksReader = asksReaderW;
  121. /* istanbul ignore next */
  122. var _map = function (fa, f) { return pipe(fa, map(f)); };
  123. /* istanbul ignore next */
  124. var _ap = function (fab, fa) { return pipe(fab, ap(fa)); };
  125. /* istanbul ignore next */
  126. var _chain = function (ma, f) { return pipe(ma, chain(f)); };
  127. var _compose = function (bc, ab) { return pipe(bc, compose(ab)); };
  128. var _promap = function (fea, f, g) { return pipe(fea, promap(f, g)); };
  129. /**
  130. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  131. * use the type constructor `F` to represent some computational context.
  132. *
  133. * @category mapping
  134. * @since 2.0.0
  135. */
  136. export var map = function (f) { return function (fa) { return function (r) { return f(fa(r)); }; }; };
  137. /**
  138. * Less strict version of [`ap`](#ap).
  139. *
  140. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  141. *
  142. * @since 2.8.0
  143. */
  144. export var apW = function (fa) { return function (fab) { return function (r) {
  145. return fab(r)(fa(r));
  146. }; }; };
  147. /**
  148. * @since 2.0.0
  149. */
  150. export var ap = apW;
  151. /**
  152. * @category constructors
  153. * @since 2.0.0
  154. */
  155. export var of = constant;
  156. /**
  157. * Less strict version of [`chain`](#chain).
  158. *
  159. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  160. *
  161. * @category sequencing
  162. * @since 2.6.0
  163. */
  164. export var chainW = function (f) { return function (fa) { return function (r) {
  165. return f(fa(r))(r);
  166. }; }; };
  167. /**
  168. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  169. *
  170. * @category sequencing
  171. * @since 2.0.0
  172. */
  173. export var chain = chainW;
  174. /**
  175. * Less strict version of [`flatten`](#flatten).
  176. *
  177. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  178. *
  179. * @category sequencing
  180. * @since 2.11.0
  181. */
  182. export var flattenW =
  183. /*#__PURE__*/ chainW(identity);
  184. /**
  185. * @category sequencing
  186. * @since 2.0.0
  187. */
  188. export var flatten = flattenW;
  189. /**
  190. * @since 2.0.0
  191. */
  192. export var compose = function (ab) { return function (bc) { return flow(ab, bc); }; };
  193. /**
  194. * @since 2.0.0
  195. */
  196. export var promap = function (f, g) { return function (fea) { return function (a) {
  197. return g(fea(f(a)));
  198. }; }; };
  199. /**
  200. * @category constructors
  201. * @since 2.0.0
  202. */
  203. export var id = function () { return identity; };
  204. /**
  205. * @since 2.10.0
  206. */
  207. export var first = function (pab) {
  208. return function (_a) {
  209. var a = _a[0], c = _a[1];
  210. return [pab(a), c];
  211. };
  212. };
  213. /**
  214. * @since 2.10.0
  215. */
  216. export var second = function (pbc) {
  217. return function (_a) {
  218. var a = _a[0], b = _a[1];
  219. return [a, pbc(b)];
  220. };
  221. };
  222. /**
  223. * @since 2.10.0
  224. */
  225. export var left = function (pab) { return E.fold(function (a) { return _.left(pab(a)); }, E.right); };
  226. /**
  227. * @since 2.10.0
  228. */
  229. export var right = function (pbc) { return E.fold(E.left, function (b) { return _.right(pbc(b)); }); };
  230. /**
  231. * @category type lambdas
  232. * @since 2.0.0
  233. */
  234. export var URI = 'Reader';
  235. /**
  236. * @category instances
  237. * @since 2.7.0
  238. */
  239. export var Functor = {
  240. URI: URI,
  241. map: _map
  242. };
  243. /**
  244. * @category mapping
  245. * @since 2.10.0
  246. */
  247. export var flap = /*#__PURE__*/ flap_(Functor);
  248. /**
  249. * @category instances
  250. * @since 2.10.0
  251. */
  252. export var Pointed = {
  253. URI: URI,
  254. of: of
  255. };
  256. /**
  257. * @category instances
  258. * @since 2.10.0
  259. */
  260. export var Apply = {
  261. URI: URI,
  262. map: _map,
  263. ap: _ap
  264. };
  265. /**
  266. * Combine two effectful actions, keeping only the result of the first.
  267. *
  268. * @since 2.0.0
  269. */
  270. export var apFirst = /*#__PURE__*/ apFirst_(Apply);
  271. /**
  272. * Less strict version of [`apFirst`](#apfirst).
  273. *
  274. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  275. *
  276. * @since 2.12.0
  277. */
  278. export var apFirstW = apFirst;
  279. /**
  280. * Combine two effectful actions, keeping only the result of the second.
  281. *
  282. * @since 2.0.0
  283. */
  284. export var apSecond = /*#__PURE__*/ apSecond_(Apply);
  285. /**
  286. * Less strict version of [`apSecond`](#apsecond).
  287. *
  288. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  289. *
  290. * @since 2.12.0
  291. */
  292. export var apSecondW = apSecond;
  293. /**
  294. * @category instances
  295. * @since 2.7.0
  296. */
  297. export var Applicative = {
  298. URI: URI,
  299. map: _map,
  300. ap: _ap,
  301. of: of
  302. };
  303. /**
  304. * @category instances
  305. * @since 2.10.0
  306. */
  307. export var Chain = {
  308. URI: URI,
  309. map: _map,
  310. ap: _ap,
  311. chain: _chain
  312. };
  313. /**
  314. * @category instances
  315. * @since 2.7.0
  316. */
  317. export var Monad = {
  318. URI: URI,
  319. map: _map,
  320. of: of,
  321. ap: _ap,
  322. chain: _chain
  323. };
  324. /**
  325. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  326. * keeping only the result of the first.
  327. *
  328. * @category sequencing
  329. * @since 2.0.0
  330. */
  331. export var chainFirst =
  332. /*#__PURE__*/ chainFirst_(Chain);
  333. /**
  334. * Less strict version of [`chainFirst`](#chainfirst).
  335. *
  336. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  337. *
  338. * @category sequencing
  339. * @since 2.11.0
  340. */
  341. export var chainFirstW = chainFirst;
  342. /**
  343. * @category instances
  344. * @since 2.7.0
  345. */
  346. export var Profunctor = {
  347. URI: URI,
  348. map: _map,
  349. promap: _promap
  350. };
  351. /**
  352. * @category instances
  353. * @since 2.7.0
  354. */
  355. export var Category = {
  356. URI: URI,
  357. compose: _compose,
  358. id: id
  359. };
  360. /**
  361. * @category instances
  362. * @since 2.8.3
  363. */
  364. export var Strong = {
  365. URI: URI,
  366. map: _map,
  367. promap: _promap,
  368. first: first,
  369. second: second
  370. };
  371. /**
  372. * @category instances
  373. * @since 2.8.3
  374. */
  375. export var Choice = {
  376. URI: URI,
  377. map: _map,
  378. promap: _promap,
  379. left: left,
  380. right: right
  381. };
  382. // -------------------------------------------------------------------------------------
  383. // do notation
  384. // -------------------------------------------------------------------------------------
  385. /**
  386. * @category do notation
  387. * @since 2.8.0
  388. */
  389. export var bindTo = /*#__PURE__*/ bindTo_(Functor);
  390. var let_ = /*#__PURE__*/ let__(Functor);
  391. export {
  392. /**
  393. * @category do notation
  394. * @since 2.13.0
  395. */
  396. let_ as let };
  397. /**
  398. * @category do notation
  399. * @since 2.8.0
  400. */
  401. export var bind = /*#__PURE__*/ bind_(Chain);
  402. /**
  403. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  404. *
  405. * @category do notation
  406. * @since 2.8.0
  407. */
  408. export var bindW = bind;
  409. /**
  410. * @category do notation
  411. * @since 2.9.0
  412. */
  413. export var Do = /*#__PURE__*/ of(_.emptyRecord);
  414. /**
  415. * @category do notation
  416. * @since 2.8.0
  417. */
  418. export var apS = /*#__PURE__*/ apS_(Apply);
  419. /**
  420. * Less strict version of [`apS`](#aps).
  421. *
  422. * The `W` suffix (short for **W**idening) means that the environment types will be merged.
  423. *
  424. * @category do notation
  425. * @since 2.8.0
  426. */
  427. export var apSW = apS;
  428. /**
  429. * @since 2.11.0
  430. */
  431. export var ApT = /*#__PURE__*/ of(_.emptyReadonlyArray);
  432. // -------------------------------------------------------------------------------------
  433. // array utils
  434. // -------------------------------------------------------------------------------------
  435. /**
  436. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
  437. *
  438. * @category traversing
  439. * @since 2.11.0
  440. */
  441. export var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
  442. return function (as) {
  443. return function (r) {
  444. var out = [f(0, _.head(as))(r)];
  445. for (var i = 1; i < as.length; i++) {
  446. out.push(f(i, as[i])(r));
  447. }
  448. return out;
  449. };
  450. };
  451. };
  452. /**
  453. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  454. *
  455. * @category traversing
  456. * @since 2.11.0
  457. */
  458. export var traverseReadonlyArrayWithIndex = function (f) {
  459. var g = traverseReadonlyNonEmptyArrayWithIndex(f);
  460. return function (as) { return (_.isNonEmpty(as) ? g(as) : ApT); };
  461. };
  462. /**
  463. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  464. *
  465. * @category traversing
  466. * @since 2.9.0
  467. */
  468. export var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
  469. /**
  470. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  471. *
  472. * @category traversing
  473. * @since 2.9.0
  474. */
  475. export var traverseArray = function (f) { return traverseReadonlyArrayWithIndex(function (_, a) { return f(a); }); };
  476. /**
  477. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  478. *
  479. * @category traversing
  480. * @since 2.9.0
  481. */
  482. export var sequenceArray =
  483. /*#__PURE__*/ traverseArray(identity);
  484. // -------------------------------------------------------------------------------------
  485. // deprecated
  486. // -------------------------------------------------------------------------------------
  487. /**
  488. * This instance is deprecated, use small, specific instances instead.
  489. * For example if a function needs a `Functor` instance, pass `R.Functor` instead of `R.reader`
  490. * (where `R` is from `import R from 'fp-ts/Reader'`)
  491. *
  492. * @category zone of death
  493. * @since 2.0.0
  494. * @deprecated
  495. */
  496. export var reader = {
  497. URI: URI,
  498. map: _map,
  499. of: of,
  500. ap: _ap,
  501. chain: _chain,
  502. promap: _promap,
  503. compose: _compose,
  504. id: id,
  505. first: first,
  506. second: second,
  507. left: left,
  508. right: right
  509. };
  510. /**
  511. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  512. *
  513. * @category zone of death
  514. * @since 2.0.0
  515. * @deprecated
  516. */
  517. export var getSemigroup = /*#__PURE__*/ getApplySemigroup(Apply);
  518. /**
  519. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  520. *
  521. * @category zone of death
  522. * @since 2.0.0
  523. * @deprecated
  524. */
  525. export var getMonoid = /*#__PURE__*/ getApplicativeMonoid(Applicative);