版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

1229 строки
32 KiB

  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [op[0] & 2, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. import { getApplicativeMonoid } from './Applicative';
  38. import { ap as ap_, apFirst as apFirst_, apS as apS_, apSecond as apSecond_, getApplySemigroup as getApplySemigroup_ } from './Apply';
  39. import { bind as bind_, chainFirst as chainFirst_ } from './Chain';
  40. import { compact as compact_, separate as separate_ } from './Compactable';
  41. import * as E from './Either';
  42. import * as ET from './EitherT';
  43. import { filter as filter_, filterMap as filterMap_, partition as partition_, partitionMap as partitionMap_ } from './Filterable';
  44. import { chainEitherK as chainEitherK_, chainFirstEitherK as chainFirstEitherK_, chainOptionK as chainOptionK_, filterOrElse as filterOrElse_, fromEitherK as fromEitherK_, fromOption as fromOption_, fromOptionK as fromOptionK_, fromPredicate as fromPredicate_ } from './FromEither';
  45. import { chainFirstIOK as chainFirstIOK_, chainIOK as chainIOK_, fromIOK as fromIOK_ } from './FromIO';
  46. import { chainFirstTaskK as chainFirstTaskK_, chainTaskK as chainTaskK_, fromTaskK as fromTaskK_ } from './FromTask';
  47. import { flow, identity, pipe, SK } from './function';
  48. import { bindTo as bindTo_, flap as flap_, let as let__ } from './Functor';
  49. import * as _ from './internal';
  50. import * as T from './Task';
  51. // -------------------------------------------------------------------------------------
  52. // constructors
  53. // -------------------------------------------------------------------------------------
  54. /**
  55. * @category constructors
  56. * @since 2.0.0
  57. */
  58. export var left = /*#__PURE__*/ ET.left(T.Pointed);
  59. /**
  60. * @category constructors
  61. * @since 2.0.0
  62. */
  63. export var right = /*#__PURE__*/ ET.right(T.Pointed);
  64. /**
  65. * @category constructors
  66. * @since 2.0.0
  67. */
  68. export var rightTask = /*#__PURE__*/ ET.rightF(T.Functor);
  69. /**
  70. * @category constructors
  71. * @since 2.0.0
  72. */
  73. export var leftTask = /*#__PURE__*/ ET.leftF(T.Functor);
  74. /**
  75. * @category constructors
  76. * @since 2.0.0
  77. */
  78. export var rightIO = /*#__PURE__*/ flow(T.fromIO, rightTask);
  79. /**
  80. * @category constructors
  81. * @since 2.0.0
  82. */
  83. export var leftIO = /*#__PURE__*/ flow(T.fromIO, leftTask);
  84. // -------------------------------------------------------------------------------------
  85. // conversions
  86. // -------------------------------------------------------------------------------------
  87. /**
  88. * @category conversions
  89. * @since 2.7.0
  90. */
  91. export var fromIO = rightIO;
  92. /**
  93. * @category conversions
  94. * @since 2.7.0
  95. */
  96. export var fromTask = rightTask;
  97. /**
  98. * @category conversions
  99. * @since 2.0.0
  100. */
  101. export var fromEither = T.of;
  102. /**
  103. * @category conversions
  104. * @since 2.0.0
  105. */
  106. export var fromIOEither = T.fromIO;
  107. /**
  108. * @category conversions
  109. * @since 2.11.0
  110. */
  111. export var fromTaskOption = function (onNone) {
  112. return T.map(E.fromOption(onNone));
  113. };
  114. /**
  115. * @category pattern matching
  116. * @since 2.10.0
  117. */
  118. export var match =
  119. /*#__PURE__*/ ET.match(T.Functor);
  120. /**
  121. * Less strict version of [`match`](#match).
  122. *
  123. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  124. *
  125. * @category pattern matching
  126. * @since 2.10.0
  127. */
  128. export var matchW = match;
  129. /**
  130. * The `E` suffix (short for **E**ffect) means that the handlers return an effect (`Task`).
  131. *
  132. * @category pattern matching
  133. * @since 2.10.0
  134. */
  135. export var matchE = /*#__PURE__*/ ET.matchE(T.Monad);
  136. /**
  137. * Alias of [`matchE`](#matche).
  138. *
  139. * @category pattern matching
  140. * @since 2.0.0
  141. */
  142. export var fold = matchE;
  143. /**
  144. * Less strict version of [`matchE`](#matche).
  145. *
  146. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  147. *
  148. * @category pattern matching
  149. * @since 2.10.0
  150. */
  151. export var matchEW = matchE;
  152. /**
  153. * Alias of [`matchEW`](#matchew).
  154. *
  155. * @category pattern matching
  156. * @since 2.10.0
  157. */
  158. export var foldW = matchEW;
  159. /**
  160. * @category error handling
  161. * @since 2.0.0
  162. */
  163. export var getOrElse =
  164. /*#__PURE__*/ ET.getOrElse(T.Monad);
  165. /**
  166. * Less strict version of [`getOrElse`](#getorelse).
  167. *
  168. * The `W` suffix (short for **W**idening) means that the handler return type will be merged.
  169. *
  170. * @category error handling
  171. * @since 2.6.0
  172. */
  173. export var getOrElseW = getOrElse;
  174. /**
  175. * Transforms a `Promise` that may reject to a `Promise` that never rejects and returns an `Either` instead.
  176. *
  177. * See also [`tryCatchK`](#trycatchk).
  178. *
  179. * @example
  180. * import { left, right } from 'fp-ts/Either'
  181. * import { tryCatch } from 'fp-ts/TaskEither'
  182. *
  183. * tryCatch(() => Promise.resolve(1), String)().then(result => {
  184. * assert.deepStrictEqual(result, right(1))
  185. * })
  186. * tryCatch(() => Promise.reject('error'), String)().then(result => {
  187. * assert.deepStrictEqual(result, left('error'))
  188. * })
  189. *
  190. * @category interop
  191. * @since 2.0.0
  192. */
  193. export var tryCatch = function (f, onRejected) {
  194. return function () { return __awaiter(void 0, void 0, void 0, function () {
  195. var reason_1;
  196. return __generator(this, function (_a) {
  197. switch (_a.label) {
  198. case 0:
  199. _a.trys.push([0, 2, , 3]);
  200. return [4 /*yield*/, f().then(_.right)];
  201. case 1: return [2 /*return*/, _a.sent()];
  202. case 2:
  203. reason_1 = _a.sent();
  204. return [2 /*return*/, _.left(onRejected(reason_1))];
  205. case 3: return [2 /*return*/];
  206. }
  207. });
  208. }); };
  209. };
  210. /**
  211. * Converts a function returning a `Promise` to one returning a `TaskEither`.
  212. *
  213. * @category interop
  214. * @since 2.5.0
  215. */
  216. export var tryCatchK = function (f, onRejected) {
  217. return function () {
  218. var a = [];
  219. for (var _i = 0; _i < arguments.length; _i++) {
  220. a[_i] = arguments[_i];
  221. }
  222. return tryCatch(function () { return f.apply(void 0, a); }, onRejected);
  223. };
  224. };
  225. /**
  226. * @category conversions
  227. * @since 2.10.0
  228. */
  229. export var toUnion = /*#__PURE__*/ ET.toUnion(T.Functor);
  230. /**
  231. * @category conversions
  232. * @since 2.12.0
  233. */
  234. export var fromNullable = /*#__PURE__*/ ET.fromNullable(T.Pointed);
  235. /**
  236. * @category lifting
  237. * @since 2.12.0
  238. */
  239. export var fromNullableK = /*#__PURE__*/ ET.fromNullableK(T.Pointed);
  240. /**
  241. * @category sequencing
  242. * @since 2.12.0
  243. */
  244. export var chainNullableK =
  245. /*#__PURE__*/ ET.chainNullableK(T.Monad);
  246. // -------------------------------------------------------------------------------------
  247. // combinators
  248. // -------------------------------------------------------------------------------------
  249. /**
  250. * Returns `ma` if is a `Right` or the value returned by `onLeft` otherwise.
  251. *
  252. * See also [alt](#alt).
  253. *
  254. * @example
  255. * import * as E from 'fp-ts/Either'
  256. * import { pipe } from 'fp-ts/function'
  257. * import * as TE from 'fp-ts/TaskEither'
  258. *
  259. * async function test() {
  260. * const errorHandler = TE.orElse((error: string) => TE.right(`recovering from ${error}...`))
  261. * assert.deepStrictEqual(await pipe(TE.right('ok'), errorHandler)(), E.right('ok'))
  262. * assert.deepStrictEqual(await pipe(TE.left('ko'), errorHandler)(), E.right('recovering from ko...'))
  263. * }
  264. *
  265. * test()
  266. *
  267. * @category error handling
  268. * @since 2.0.0
  269. */
  270. export var orElse =
  271. /*#__PURE__*/ ET.orElse(T.Monad);
  272. /**
  273. * Less strict version of [`orElse`](#orelse).
  274. *
  275. * The `W` suffix (short for **W**idening) means that the return types will be merged.
  276. *
  277. * @category error handling
  278. * @since 2.10.0
  279. */
  280. export var orElseW = orElse;
  281. /**
  282. * @category error handling
  283. * @since 2.11.0
  284. */
  285. export var orElseFirst =
  286. /*#__PURE__*/ ET.orElseFirst(T.Monad);
  287. /**
  288. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  289. *
  290. * @category error handling
  291. * @since 2.11.0
  292. */
  293. export var orElseFirstW = orElseFirst;
  294. /**
  295. * @category error handling
  296. * @since 2.12.0
  297. */
  298. export var orElseFirstIOK = function (onLeft) { return orElseFirst(fromIOK(onLeft)); };
  299. /**
  300. * @category error handling
  301. * @since 2.12.0
  302. */
  303. export var orElseFirstTaskK = function (onLeft) { return orElseFirst(fromTaskK(onLeft)); };
  304. /**
  305. * @category error handling
  306. * @since 2.11.0
  307. */
  308. export var orLeft =
  309. /*#__PURE__*/ ET.orLeft(T.Monad);
  310. /**
  311. * @since 2.0.0
  312. */
  313. export var swap = /*#__PURE__*/ ET.swap(T.Functor);
  314. /**
  315. * @category lifting
  316. * @since 2.11.0
  317. */
  318. export var fromTaskOptionK = function (onNone) {
  319. var from = fromTaskOption(onNone);
  320. return function (f) { return flow(f, from); };
  321. };
  322. /**
  323. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  324. *
  325. * @category sequencing
  326. * @since 2.12.3
  327. */
  328. export var chainTaskOptionKW = function (onNone) {
  329. return function (f) {
  330. return function (ma) {
  331. return pipe(ma, chain(fromTaskOptionK(onNone)(f)));
  332. };
  333. };
  334. };
  335. /**
  336. * @category sequencing
  337. * @since 2.11.0
  338. */
  339. export var chainTaskOptionK = chainTaskOptionKW;
  340. /**
  341. * @category lifting
  342. * @since 2.4.0
  343. */
  344. export var fromIOEitherK = function (f) { return flow(f, fromIOEither); };
  345. /**
  346. * Less strict version of [`chainIOEitherK`](#chainioeitherk).
  347. *
  348. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  349. *
  350. * @category sequencing
  351. * @since 2.6.1
  352. */
  353. export var chainIOEitherKW = function (f) { return chainW(fromIOEitherK(f)); };
  354. /**
  355. * @category sequencing
  356. * @since 2.4.0
  357. */
  358. export var chainIOEitherK = chainIOEitherKW;
  359. var _map = function (fa, f) { return pipe(fa, map(f)); };
  360. var _apPar = function (fab, fa) { return pipe(fab, ap(fa)); };
  361. var _apSeq = function (fab, fa) {
  362. return pipe(fab, chain(function (f) { return pipe(fa, map(f)); }));
  363. };
  364. /* istanbul ignore next */
  365. var _chain = function (ma, f) { return pipe(ma, chain(f)); };
  366. /* istanbul ignore next */
  367. var _bimap = function (fa, f, g) { return pipe(fa, bimap(f, g)); };
  368. /* istanbul ignore next */
  369. var _mapLeft = function (fa, f) { return pipe(fa, mapLeft(f)); };
  370. /* istanbul ignore next */
  371. var _alt = function (fa, that) { return pipe(fa, alt(that)); };
  372. /**
  373. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  374. * use the type constructor `F` to represent some computational context.
  375. *
  376. * @category mapping
  377. * @since 2.0.0
  378. */
  379. export var map = /*#__PURE__*/ ET.map(T.Functor);
  380. /**
  381. * Map a pair of functions over the two type arguments of the bifunctor.
  382. *
  383. * @category mapping
  384. * @since 2.0.0
  385. */
  386. export var bimap =
  387. /*#__PURE__*/ ET.bimap(T.Functor);
  388. /**
  389. * Map a function over the first type argument of a bifunctor.
  390. *
  391. * @category error handling
  392. * @since 2.0.0
  393. */
  394. export var mapLeft =
  395. /*#__PURE__*/ ET.mapLeft(T.Functor);
  396. /**
  397. * @since 2.0.0
  398. */
  399. export var ap =
  400. /*#__PURE__*/ ET.ap(T.ApplyPar);
  401. /**
  402. * Less strict version of [`ap`](#ap).
  403. *
  404. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  405. *
  406. * @since 2.8.0
  407. */
  408. export var apW = ap;
  409. /**
  410. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  411. *
  412. * @category sequencing
  413. * @since 2.0.0
  414. */
  415. export var chain =
  416. /*#__PURE__*/ ET.chain(T.Monad);
  417. /**
  418. * Less strict version of [`chain`](#chain).
  419. *
  420. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  421. *
  422. * @category sequencing
  423. * @since 2.6.0
  424. */
  425. export var chainW = chain;
  426. /**
  427. * Less strict version of [`flatten`](#flatten).
  428. *
  429. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  430. *
  431. * @category sequencing
  432. * @since 2.11.0
  433. */
  434. export var flattenW =
  435. /*#__PURE__*/ chainW(identity);
  436. /**
  437. * @category sequencing
  438. * @since 2.0.0
  439. */
  440. export var flatten = flattenW;
  441. /**
  442. * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
  443. * types of kind `* -> *`.
  444. *
  445. * In case of `TaskEither` returns `fa` if is a `Right` or the value returned by `that` otherwise.
  446. *
  447. * See also [orElse](#orelse).
  448. *
  449. * @example
  450. * import * as E from 'fp-ts/Either'
  451. * import { pipe } from 'fp-ts/function'
  452. * import * as TE from 'fp-ts/TaskEither'
  453. *
  454. * async function test() {
  455. * assert.deepStrictEqual(
  456. * await pipe(
  457. * TE.right(1),
  458. * TE.alt(() => TE.right(2))
  459. * )(),
  460. * E.right(1)
  461. * )
  462. * assert.deepStrictEqual(
  463. * await pipe(
  464. * TE.left('a'),
  465. * TE.alt(() => TE.right(2))
  466. * )(),
  467. * E.right(2)
  468. * )
  469. * assert.deepStrictEqual(
  470. * await pipe(
  471. * TE.left('a'),
  472. * TE.alt(() => TE.left('b'))
  473. * )(),
  474. * E.left('b')
  475. * )
  476. * }
  477. *
  478. * test()
  479. *
  480. * @category error handling
  481. * @since 2.0.0
  482. */
  483. export var alt =
  484. /*#__PURE__*/ ET.alt(T.Monad);
  485. /**
  486. * Less strict version of [`alt`](#alt).
  487. *
  488. * The `W` suffix (short for **W**idening) means that the error and the return types will be merged.
  489. *
  490. * @category error handling
  491. * @since 2.9.0
  492. */
  493. export var altW = alt;
  494. /**
  495. * @category constructors
  496. * @since 2.0.0
  497. */
  498. export var of = right;
  499. /**
  500. * @since 2.7.0
  501. */
  502. export var throwError = left;
  503. /**
  504. * @category type lambdas
  505. * @since 2.0.0
  506. */
  507. export var URI = 'TaskEither';
  508. /**
  509. * The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
  510. * get all errors you need to provide a way to concatenate them via a `Semigroup`.
  511. *
  512. * @example
  513. * import * as E from 'fp-ts/Either'
  514. * import { pipe } from 'fp-ts/function'
  515. * import * as RA from 'fp-ts/ReadonlyArray'
  516. * import * as S from 'fp-ts/Semigroup'
  517. * import * as string from 'fp-ts/string'
  518. * import * as T from 'fp-ts/Task'
  519. * import * as TE from 'fp-ts/TaskEither'
  520. *
  521. * interface User {
  522. * readonly id: string
  523. * readonly name: string
  524. * }
  525. *
  526. * const remoteDatabase: ReadonlyArray<User> = [
  527. * { id: 'id1', name: 'John' },
  528. * { id: 'id2', name: 'Mary' },
  529. * { id: 'id3', name: 'Joey' }
  530. * ]
  531. *
  532. * const fetchUser = (id: string): TE.TaskEither<string, User> =>
  533. * pipe(
  534. * remoteDatabase,
  535. * RA.findFirst((user) => user.id === id),
  536. * TE.fromOption(() => `${id} not found`)
  537. * )
  538. *
  539. * async function test() {
  540. * assert.deepStrictEqual(
  541. * await pipe(['id4', 'id5'], RA.traverse(TE.ApplicativePar)(fetchUser))(),
  542. * E.left('id4 not found') // <= first error
  543. * )
  544. *
  545. * const Applicative = TE.getApplicativeTaskValidation(
  546. * T.ApplyPar,
  547. * pipe(string.Semigroup, S.intercalate(', '))
  548. * )
  549. *
  550. * assert.deepStrictEqual(
  551. * await pipe(['id4', 'id5'], RA.traverse(Applicative)(fetchUser))(),
  552. * E.left('id4 not found, id5 not found') // <= all errors
  553. * )
  554. * }
  555. *
  556. * test()
  557. *
  558. * @category error handling
  559. * @since 2.7.0
  560. */
  561. export function getApplicativeTaskValidation(A, S) {
  562. var ap = ap_(A, E.getApplicativeValidation(S));
  563. return {
  564. URI: URI,
  565. _E: undefined,
  566. map: _map,
  567. ap: function (fab, fa) { return pipe(fab, ap(fa)); },
  568. of: of
  569. };
  570. }
  571. /**
  572. * The default [`Alt`](#alt) instance returns the last error, if you want to
  573. * get all errors you need to provide a way to concatenate them via a `Semigroup`.
  574. *
  575. * See [`getAltValidation`](./Either.ts.html#getaltvalidation).
  576. *
  577. * @category error handling
  578. * @since 2.7.0
  579. */
  580. export function getAltTaskValidation(S) {
  581. var alt = ET.altValidation(T.Monad, S);
  582. return {
  583. URI: URI,
  584. _E: undefined,
  585. map: _map,
  586. alt: function (fa, that) { return pipe(fa, alt(that)); }
  587. };
  588. }
  589. /**
  590. * @category filtering
  591. * @since 2.10.0
  592. */
  593. export var getCompactable = function (M) {
  594. var C = E.getCompactable(M);
  595. return {
  596. URI: URI,
  597. _E: undefined,
  598. compact: compact_(T.Functor, C),
  599. separate: separate_(T.Functor, C, E.Functor)
  600. };
  601. };
  602. /**
  603. * @category filtering
  604. * @since 2.1.0
  605. */
  606. export function getFilterable(M) {
  607. var F = E.getFilterable(M);
  608. var C = getCompactable(M);
  609. var filter = filter_(T.Functor, F);
  610. var filterMap = filterMap_(T.Functor, F);
  611. var partition = partition_(T.Functor, F);
  612. var partitionMap = partitionMap_(T.Functor, F);
  613. return {
  614. URI: URI,
  615. _E: undefined,
  616. map: _map,
  617. compact: C.compact,
  618. separate: C.separate,
  619. filter: function (fa, predicate) { return pipe(fa, filter(predicate)); },
  620. filterMap: function (fa, f) { return pipe(fa, filterMap(f)); },
  621. partition: function (fa, predicate) { return pipe(fa, partition(predicate)); },
  622. partitionMap: function (fa, f) { return pipe(fa, partitionMap(f)); }
  623. };
  624. }
  625. /**
  626. * @category instances
  627. * @since 2.7.0
  628. */
  629. export var Functor = {
  630. URI: URI,
  631. map: _map
  632. };
  633. /**
  634. * @category mapping
  635. * @since 2.10.0
  636. */
  637. export var flap = /*#__PURE__*/ flap_(Functor);
  638. /**
  639. * @category instances
  640. * @since 2.10.0
  641. */
  642. export var Pointed = {
  643. URI: URI,
  644. of: of
  645. };
  646. /**
  647. * Runs computations in parallel.
  648. *
  649. * @category instances
  650. * @since 2.10.0
  651. */
  652. export var ApplyPar = {
  653. URI: URI,
  654. map: _map,
  655. ap: _apPar
  656. };
  657. /**
  658. * Combine two effectful actions, keeping only the result of the first.
  659. *
  660. * @since 2.0.0
  661. */
  662. export var apFirst = /*#__PURE__*/ apFirst_(ApplyPar);
  663. /**
  664. * Less strict version of [`apFirst`](#apfirst).
  665. *
  666. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  667. *
  668. * @since 2.12.0
  669. */
  670. export var apFirstW = apFirst;
  671. /**
  672. * Combine two effectful actions, keeping only the result of the second.
  673. *
  674. * @since 2.0.0
  675. */
  676. export var apSecond = /*#__PURE__*/ apSecond_(ApplyPar);
  677. /**
  678. * Less strict version of [`apSecond`](#apsecond).
  679. *
  680. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  681. *
  682. * @since 2.12.0
  683. */
  684. export var apSecondW = apSecond;
  685. /**
  686. * Runs computations in parallel.
  687. *
  688. * @category instances
  689. * @since 2.7.0
  690. */
  691. export var ApplicativePar = {
  692. URI: URI,
  693. map: _map,
  694. ap: _apPar,
  695. of: of
  696. };
  697. /**
  698. * Runs computations sequentially.
  699. *
  700. * @category instances
  701. * @since 2.10.0
  702. */
  703. export var ApplySeq = {
  704. URI: URI,
  705. map: _map,
  706. ap: _apSeq
  707. };
  708. /**
  709. * Runs computations sequentially.
  710. *
  711. * @category instances
  712. * @since 2.7.0
  713. */
  714. export var ApplicativeSeq = {
  715. URI: URI,
  716. map: _map,
  717. ap: _apSeq,
  718. of: of
  719. };
  720. /**
  721. * @category instances
  722. * @since 2.10.0
  723. */
  724. export var Chain = {
  725. URI: URI,
  726. map: _map,
  727. ap: _apPar,
  728. chain: _chain
  729. };
  730. /**
  731. * @category instances
  732. * @since 2.10.0
  733. */
  734. export var Monad = {
  735. URI: URI,
  736. map: _map,
  737. ap: _apPar,
  738. chain: _chain,
  739. of: of
  740. };
  741. /**
  742. * @category instances
  743. * @since 2.10.0
  744. */
  745. export var MonadIO = {
  746. URI: URI,
  747. map: _map,
  748. ap: _apPar,
  749. chain: _chain,
  750. of: of,
  751. fromIO: fromIO
  752. };
  753. /**
  754. * @category instances
  755. * @since 2.10.0
  756. */
  757. export var MonadTask = {
  758. URI: URI,
  759. map: _map,
  760. ap: _apPar,
  761. chain: _chain,
  762. of: of,
  763. fromIO: fromIO,
  764. fromTask: fromTask
  765. };
  766. /**
  767. * @category instances
  768. * @since 2.10.0
  769. */
  770. export var MonadThrow = {
  771. URI: URI,
  772. map: _map,
  773. ap: _apPar,
  774. chain: _chain,
  775. of: of,
  776. throwError: throwError
  777. };
  778. /**
  779. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  780. * keeping only the result of the first.
  781. *
  782. * @category sequencing
  783. * @since 2.0.0
  784. */
  785. export var chainFirst =
  786. /*#__PURE__*/ chainFirst_(Chain);
  787. /**
  788. * Less strict version of [`chainFirst`](#chainfirst).
  789. *
  790. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  791. *
  792. * @category sequencing
  793. * @since 2.8.0
  794. */
  795. export var chainFirstW = chainFirst;
  796. /**
  797. * @category instances
  798. * @since 2.7.0
  799. */
  800. export var Bifunctor = {
  801. URI: URI,
  802. bimap: _bimap,
  803. mapLeft: _mapLeft
  804. };
  805. /**
  806. * @category instances
  807. * @since 2.7.0
  808. */
  809. export var Alt = {
  810. URI: URI,
  811. map: _map,
  812. alt: _alt
  813. };
  814. /**
  815. * @category instances
  816. * @since 2.10.0
  817. */
  818. export var FromEither = {
  819. URI: URI,
  820. fromEither: fromEither
  821. };
  822. /**
  823. * @category conversions
  824. * @since 2.0.0
  825. */
  826. export var fromOption =
  827. /*#__PURE__*/ fromOption_(FromEither);
  828. /**
  829. * @category lifting
  830. * @since 2.10.0
  831. */
  832. export var fromOptionK =
  833. /*#__PURE__*/ fromOptionK_(FromEither);
  834. /**
  835. * @category sequencing
  836. * @since 2.10.0
  837. */
  838. export var chainOptionK = /*#__PURE__*/ chainOptionK_(FromEither, Chain);
  839. /**
  840. * @category sequencing
  841. * @since 2.4.0
  842. */
  843. export var chainEitherK =
  844. /*#__PURE__*/ chainEitherK_(FromEither, Chain);
  845. /**
  846. * Less strict version of [`chainEitherK`](#chaineitherk).
  847. *
  848. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  849. *
  850. * @category sequencing
  851. * @since 2.6.1
  852. */
  853. export var chainEitherKW = chainEitherK;
  854. /**
  855. * @category sequencing
  856. * @since 2.12.0
  857. */
  858. export var chainFirstEitherK =
  859. /*#__PURE__*/ chainFirstEitherK_(FromEither, Chain);
  860. /**
  861. * Less strict version of [`chainFirstEitherK`](#chainfirsteitherk).
  862. *
  863. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  864. *
  865. * @category sequencing
  866. * @since 2.12.0
  867. */
  868. export var chainFirstEitherKW = chainFirstEitherK;
  869. /**
  870. * @category lifting
  871. * @since 2.0.0
  872. */
  873. export var fromPredicate = /*#__PURE__*/ fromPredicate_(FromEither);
  874. /**
  875. * @category filtering
  876. * @since 2.0.0
  877. */
  878. export var filterOrElse = /*#__PURE__*/ filterOrElse_(FromEither, Chain);
  879. /**
  880. * Less strict version of [`filterOrElse`](#filterorelse).
  881. *
  882. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  883. *
  884. * @category filtering
  885. * @since 2.9.0
  886. */
  887. export var filterOrElseW = filterOrElse;
  888. /**
  889. * @category lifting
  890. * @since 2.4.0
  891. */
  892. export var fromEitherK = /*#__PURE__*/ fromEitherK_(FromEither);
  893. /**
  894. * @category instances
  895. * @since 2.10.0
  896. */
  897. export var FromIO = {
  898. URI: URI,
  899. fromIO: fromIO
  900. };
  901. /**
  902. * @category lifting
  903. * @since 2.10.0
  904. */
  905. export var fromIOK = /*#__PURE__*/ fromIOK_(FromIO);
  906. /**
  907. * @category sequencing
  908. * @since 2.10.0
  909. */
  910. export var chainIOK =
  911. /*#__PURE__*/ chainIOK_(FromIO, Chain);
  912. /**
  913. * @category sequencing
  914. * @since 2.10.0
  915. */
  916. export var chainFirstIOK =
  917. /*#__PURE__*/ chainFirstIOK_(FromIO, Chain);
  918. /**
  919. * @category instances
  920. * @since 2.10.0
  921. */
  922. export var FromTask = {
  923. URI: URI,
  924. fromIO: fromIO,
  925. fromTask: fromTask
  926. };
  927. /**
  928. * @category lifting
  929. * @since 2.10.0
  930. */
  931. export var fromTaskK = /*#__PURE__*/ fromTaskK_(FromTask);
  932. /**
  933. * @category sequencing
  934. * @since 2.10.0
  935. */
  936. export var chainTaskK =
  937. /*#__PURE__*/ chainTaskK_(FromTask, Chain);
  938. /**
  939. * @category sequencing
  940. * @since 2.10.0
  941. */
  942. export var chainFirstTaskK =
  943. /*#__PURE__*/ chainFirstTaskK_(FromTask, Chain);
  944. export function taskify(f) {
  945. return function () {
  946. var args = Array.prototype.slice.call(arguments);
  947. return function () {
  948. return new Promise(function (resolve) {
  949. var cbResolver = function (e, r) { return (e != null ? resolve(_.left(e)) : resolve(_.right(r))); };
  950. f.apply(null, args.concat(cbResolver));
  951. });
  952. };
  953. };
  954. }
  955. /**
  956. * Make sure that a resource is cleaned up in the event of an exception (\*). The release action is called regardless of
  957. * whether the body action throws (\*) or returns.
  958. *
  959. * (\*) i.e. returns a `Left`
  960. *
  961. * @since 2.0.0
  962. */
  963. export var bracket = function (acquire, use, release) { return bracketW(acquire, use, release); };
  964. /**
  965. * Less strict version of [`bracket`](#bracket).
  966. *
  967. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  968. *
  969. * @since 2.12.0
  970. */
  971. export var bracketW = function (acquire, use, release) {
  972. return pipe(acquire, chainW(function (a) {
  973. return pipe(use(a), T.chain(function (e) {
  974. return pipe(release(a, e), chainW(function () { return T.of(e); }));
  975. }));
  976. }));
  977. };
  978. // -------------------------------------------------------------------------------------
  979. // do notation
  980. // -------------------------------------------------------------------------------------
  981. /**
  982. * @category do notation
  983. * @since 2.9.0
  984. */
  985. export var Do = /*#__PURE__*/ of(_.emptyRecord);
  986. /**
  987. * @category do notation
  988. * @since 2.8.0
  989. */
  990. export var bindTo = /*#__PURE__*/ bindTo_(Functor);
  991. var let_ = /*#__PURE__*/ let__(Functor);
  992. export {
  993. /**
  994. * @category do notation
  995. * @since 2.13.0
  996. */
  997. let_ as let };
  998. /**
  999. * @category do notation
  1000. * @since 2.8.0
  1001. */
  1002. export var bind = /*#__PURE__*/ bind_(Chain);
  1003. /**
  1004. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1005. *
  1006. * @category do notation
  1007. * @since 2.8.0
  1008. */
  1009. export var bindW = bind;
  1010. /**
  1011. * @category do notation
  1012. * @since 2.8.0
  1013. */
  1014. export var apS = /*#__PURE__*/ apS_(ApplyPar);
  1015. /**
  1016. * Less strict version of [`apS`](#aps).
  1017. *
  1018. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1019. *
  1020. * @category do notation
  1021. * @since 2.8.0
  1022. */
  1023. export var apSW = apS;
  1024. /**
  1025. * @since 2.11.0
  1026. */
  1027. export var ApT = /*#__PURE__*/ of(_.emptyReadonlyArray);
  1028. // -------------------------------------------------------------------------------------
  1029. // array utils
  1030. // -------------------------------------------------------------------------------------
  1031. /**
  1032. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`.
  1033. *
  1034. * @category traversing
  1035. * @since 2.11.0
  1036. */
  1037. export var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
  1038. return flow(T.traverseReadonlyNonEmptyArrayWithIndex(f), T.map(E.traverseReadonlyNonEmptyArrayWithIndex(SK)));
  1039. };
  1040. /**
  1041. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`.
  1042. *
  1043. * @category traversing
  1044. * @since 2.11.0
  1045. */
  1046. export var traverseReadonlyArrayWithIndex = function (f) {
  1047. var g = traverseReadonlyNonEmptyArrayWithIndex(f);
  1048. return function (as) { return (_.isNonEmpty(as) ? g(as) : ApT); };
  1049. };
  1050. /**
  1051. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
  1052. *
  1053. * @category traversing
  1054. * @since 2.11.0
  1055. */
  1056. export var traverseReadonlyNonEmptyArrayWithIndexSeq = function (f) {
  1057. return function (as) {
  1058. return function () {
  1059. return _.tail(as).reduce(function (acc, a, i) {
  1060. return acc.then(function (ebs) {
  1061. return _.isLeft(ebs)
  1062. ? acc
  1063. : f(i + 1, a)().then(function (eb) {
  1064. if (_.isLeft(eb)) {
  1065. return eb;
  1066. }
  1067. ebs.right.push(eb.right);
  1068. return ebs;
  1069. });
  1070. });
  1071. }, f(0, _.head(as))().then(E.map(_.singleton)));
  1072. };
  1073. };
  1074. };
  1075. /**
  1076. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
  1077. *
  1078. * @category traversing
  1079. * @since 2.11.0
  1080. */
  1081. export var traverseReadonlyArrayWithIndexSeq = function (f) {
  1082. var g = traverseReadonlyNonEmptyArrayWithIndexSeq(f);
  1083. return function (as) { return (_.isNonEmpty(as) ? g(as) : ApT); };
  1084. };
  1085. /**
  1086. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  1087. *
  1088. * @category traversing
  1089. * @since 2.9.0
  1090. */
  1091. export var traverseArrayWithIndex = traverseReadonlyArrayWithIndex;
  1092. /**
  1093. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  1094. *
  1095. * @category traversing
  1096. * @since 2.9.0
  1097. */
  1098. export var traverseArray = function (f) { return traverseReadonlyArrayWithIndex(function (_, a) { return f(a); }); };
  1099. /**
  1100. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  1101. *
  1102. * @category traversing
  1103. * @since 2.9.0
  1104. */
  1105. export var sequenceArray =
  1106. /*#__PURE__*/ traverseArray(identity);
  1107. /**
  1108. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
  1109. *
  1110. * @category traversing
  1111. * @since 2.9.0
  1112. */
  1113. export var traverseSeqArrayWithIndex = traverseReadonlyArrayWithIndexSeq;
  1114. /**
  1115. * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`.
  1116. *
  1117. * @category traversing
  1118. * @since 2.9.0
  1119. */
  1120. export var traverseSeqArray = function (f) { return traverseReadonlyArrayWithIndexSeq(function (_, a) { return f(a); }); };
  1121. /**
  1122. * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`.
  1123. *
  1124. * @category traversing
  1125. * @since 2.9.0
  1126. */
  1127. export var sequenceSeqArray =
  1128. /*#__PURE__*/ traverseSeqArray(identity);
  1129. // -------------------------------------------------------------------------------------
  1130. // deprecated
  1131. // -------------------------------------------------------------------------------------
  1132. /**
  1133. * This instance is deprecated, use small, specific instances instead.
  1134. * For example if a function needs a `Functor` instance, pass `TE.Functor` instead of `TE.taskEither`
  1135. * (where `TE` is from `import TE from 'fp-ts/TaskEither'`)
  1136. *
  1137. * @category zone of death
  1138. * @since 2.0.0
  1139. * @deprecated
  1140. */
  1141. export var taskEither = {
  1142. URI: URI,
  1143. bimap: _bimap,
  1144. mapLeft: _mapLeft,
  1145. map: _map,
  1146. of: of,
  1147. ap: _apPar,
  1148. chain: _chain,
  1149. alt: _alt,
  1150. fromIO: fromIO,
  1151. fromTask: fromTask,
  1152. throwError: throwError
  1153. };
  1154. /**
  1155. * This instance is deprecated, use small, specific instances instead.
  1156. * For example if a function needs a `Functor` instance, pass `TE.Functor` instead of `TE.taskEitherSeq`
  1157. * (where `TE` is from `import TE from 'fp-ts/TaskEither'`)
  1158. *
  1159. * @category zone of death
  1160. * @since 2.0.0
  1161. * @deprecated
  1162. */
  1163. export var taskEitherSeq = {
  1164. URI: URI,
  1165. bimap: _bimap,
  1166. mapLeft: _mapLeft,
  1167. map: _map,
  1168. of: of,
  1169. ap: _apSeq,
  1170. chain: _chain,
  1171. alt: _alt,
  1172. fromIO: fromIO,
  1173. fromTask: fromTask,
  1174. throwError: throwError
  1175. };
  1176. /**
  1177. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  1178. *
  1179. * @category zone of death
  1180. * @since 2.0.0
  1181. * @deprecated
  1182. */
  1183. export var getApplySemigroup =
  1184. /*#__PURE__*/ getApplySemigroup_(ApplySeq);
  1185. /**
  1186. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  1187. *
  1188. * @category zone of death
  1189. * @since 2.0.0
  1190. * @deprecated
  1191. */
  1192. export var getApplyMonoid =
  1193. /*#__PURE__*/ getApplicativeMonoid(ApplicativeSeq);
  1194. /**
  1195. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  1196. *
  1197. * @category zone of death
  1198. * @since 2.0.0
  1199. * @deprecated
  1200. */
  1201. export var getSemigroup = function (S) {
  1202. return getApplySemigroup_(T.ApplySeq)(E.getSemigroup(S));
  1203. };
  1204. /**
  1205. * Use [`getApplicativeTaskValidation`](#getapplicativetaskvalidation) and [`getAltTaskValidation`](#getalttaskvalidation) instead.
  1206. *
  1207. * @category zone of death
  1208. * @since 2.0.0
  1209. * @deprecated
  1210. */
  1211. export function getTaskValidation(SE) {
  1212. var applicativeTaskValidation = getApplicativeTaskValidation(T.ApplicativePar, SE);
  1213. var altTaskValidation = getAltTaskValidation(SE);
  1214. return {
  1215. URI: URI,
  1216. _E: undefined,
  1217. map: _map,
  1218. ap: applicativeTaskValidation.ap,
  1219. of: of,
  1220. chain: _chain,
  1221. bimap: _bimap,
  1222. mapLeft: _mapLeft,
  1223. alt: altTaskValidation.alt,
  1224. fromIO: fromIO,
  1225. fromTask: fromTask,
  1226. throwError: throwError
  1227. };
  1228. }