版博士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.

Reader.js 16 KiB

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