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

TaskThese.js 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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.bifunctorTaskThese = exports.functorTaskThese = exports.traverseReadonlyArrayWithIndexSeq = exports.traverseReadonlyNonEmptyArrayWithIndexSeq = exports.traverseReadonlyArrayWithIndex = exports.traverseReadonlyNonEmptyArrayWithIndex = exports.ApT = exports.toTuple2 = exports.fromTaskK = exports.FromTask = exports.fromIOK = exports.FromIO = exports.fromTheseK = exports.FromThese = exports.fromPredicate = exports.fromOptionK = exports.fromOption = exports.FromEither = exports.Bifunctor = exports.Pointed = exports.flap = exports.Functor = exports.getMonad = exports.getChain = exports.getApplicative = exports.getApply = exports.URI = exports.of = exports.mapLeft = exports.bimap = exports.map = exports.swap = exports.foldW = exports.matchEW = exports.fold = exports.matchE = exports.matchW = exports.match = exports.fromTask = exports.fromIOEither = exports.fromIO = exports.fromThese = exports.fromEither = exports.leftIO = exports.rightIO = exports.leftTask = exports.rightTask = exports.both = exports.right = exports.left = void 0;
  27. exports.getSemigroup = exports.taskThese = exports.toTuple = void 0;
  28. var Apply_1 = require("./Apply");
  29. var FromEither_1 = require("./FromEither");
  30. var FromIO_1 = require("./FromIO");
  31. var FromTask_1 = require("./FromTask");
  32. var FromThese_1 = require("./FromThese");
  33. var function_1 = require("./function");
  34. var Functor_1 = require("./Functor");
  35. var _ = __importStar(require("./internal"));
  36. var T = __importStar(require("./Task"));
  37. var TH = __importStar(require("./These"));
  38. var TT = __importStar(require("./TheseT"));
  39. /**
  40. * @category constructors
  41. * @since 2.4.0
  42. */
  43. exports.left = TT.left(T.Pointed);
  44. /**
  45. * @category constructors
  46. * @since 2.4.0
  47. */
  48. exports.right = TT.right(T.Pointed);
  49. /**
  50. * @category constructors
  51. * @since 2.4.0
  52. */
  53. exports.both = TT.both(T.Pointed);
  54. /**
  55. * @category constructors
  56. * @since 2.4.0
  57. */
  58. exports.rightTask = TT.rightF(T.Functor);
  59. /**
  60. * @category constructors
  61. * @since 2.4.0
  62. */
  63. exports.leftTask = TT.leftF(T.Functor);
  64. /**
  65. * @category constructors
  66. * @since 2.4.0
  67. */
  68. exports.rightIO = (0, function_1.flow)(T.fromIO, exports.rightTask);
  69. /**
  70. * @category constructors
  71. * @since 2.4.0
  72. */
  73. exports.leftIO = (0, function_1.flow)(T.fromIO, exports.leftTask);
  74. // -------------------------------------------------------------------------------------
  75. // conversions
  76. // -------------------------------------------------------------------------------------
  77. /**
  78. * @category conversions
  79. * @since 2.10.0
  80. */
  81. exports.fromEither = T.of;
  82. /**
  83. * @category conversions
  84. * @since 2.11.0
  85. */
  86. exports.fromThese = T.of;
  87. /**
  88. * @category conversions
  89. * @since 2.7.0
  90. */
  91. exports.fromIO = exports.rightIO;
  92. /**
  93. * @category conversions
  94. * @since 2.4.0
  95. */
  96. exports.fromIOEither = T.fromIO;
  97. /**
  98. * @category conversions
  99. * @since 2.7.0
  100. */
  101. exports.fromTask = exports.rightTask;
  102. /**
  103. * @category pattern matching
  104. * @since 2.10.0
  105. */
  106. exports.match = TT.match(T.Functor);
  107. /**
  108. * Less strict version of [`match`](#match).
  109. *
  110. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  111. *
  112. * @category pattern matching
  113. * @since 2.10.0
  114. */
  115. exports.matchW = exports.match;
  116. /**
  117. * The `E` suffix (short for **E**ffect) means that the handlers return an effect (`Task`).
  118. *
  119. * @category pattern matching
  120. * @since 2.10.0
  121. */
  122. exports.matchE = TT.matchE(T.Monad);
  123. /**
  124. * Alias of [`matchE`](#matche).
  125. *
  126. * @category pattern matching
  127. * @since 2.4.0
  128. */
  129. exports.fold = exports.matchE;
  130. /**
  131. * Less strict version of [`matchE`](#matche).
  132. *
  133. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  134. *
  135. * @category pattern matching
  136. * @since 2.10.0
  137. */
  138. exports.matchEW = exports.fold;
  139. /**
  140. * Alias of [`matchEW`](#matchew).
  141. *
  142. * @category pattern matching
  143. * @since 2.10.0
  144. */
  145. exports.foldW = exports.matchEW;
  146. /**
  147. * @since 2.4.0
  148. */
  149. exports.swap = TT.swap(T.Functor);
  150. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
  151. /* istanbul ignore next */
  152. var _bimap = function (fa, f, g) { return (0, function_1.pipe)(fa, (0, exports.bimap)(f, g)); };
  153. /* istanbul ignore next */
  154. var _mapLeft = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.mapLeft)(f)); };
  155. /**
  156. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  157. * use the type constructor `F` to represent some computational context.
  158. *
  159. * @category mapping
  160. * @since 2.4.0
  161. */
  162. exports.map = TT.map(T.Functor);
  163. /**
  164. * Map a pair of functions over the two type arguments of the bifunctor.
  165. *
  166. * @category mapping
  167. * @since 2.4.0
  168. */
  169. exports.bimap =
  170. /*#__PURE__*/ TT.bimap(T.Functor);
  171. /**
  172. * Map a function over the first type argument of a bifunctor.
  173. *
  174. * @category error handling
  175. * @since 2.4.0
  176. */
  177. exports.mapLeft = TT.mapLeft(T.Functor);
  178. /**
  179. * @category constructors
  180. * @since 2.7.0
  181. */
  182. exports.of = exports.right;
  183. /**
  184. * @category type lambdas
  185. * @since 2.4.0
  186. */
  187. exports.URI = 'TaskThese';
  188. /**
  189. * @category instances
  190. * @since 2.10.0
  191. */
  192. var getApply = function (A, S) {
  193. var ap = TT.ap(A, S);
  194. return {
  195. URI: exports.URI,
  196. _E: undefined,
  197. map: _map,
  198. ap: function (fab, fa) { return (0, function_1.pipe)(fab, ap(fa)); }
  199. };
  200. };
  201. exports.getApply = getApply;
  202. /**
  203. * @category instances
  204. * @since 2.7.0
  205. */
  206. function getApplicative(A, S) {
  207. var ap = (0, exports.getApply)(A, S).ap;
  208. return {
  209. URI: exports.URI,
  210. _E: undefined,
  211. map: _map,
  212. ap: ap,
  213. of: exports.of
  214. };
  215. }
  216. exports.getApplicative = getApplicative;
  217. /**
  218. * @category instances
  219. * @since 2.10.0
  220. */
  221. function getChain(S) {
  222. var A = (0, exports.getApply)(T.ApplicativePar, S);
  223. var chain = TT.chain(T.Monad, S);
  224. return {
  225. URI: exports.URI,
  226. _E: undefined,
  227. map: _map,
  228. ap: A.ap,
  229. chain: function (ma, f) { return (0, function_1.pipe)(ma, chain(f)); }
  230. };
  231. }
  232. exports.getChain = getChain;
  233. /**
  234. * @category instances
  235. * @since 2.4.0
  236. */
  237. function getMonad(S) {
  238. var A = getApplicative(T.ApplicativePar, S);
  239. var C = getChain(S);
  240. return {
  241. URI: exports.URI,
  242. _E: undefined,
  243. map: _map,
  244. ap: A.ap,
  245. of: exports.of,
  246. chain: C.chain,
  247. fromIO: exports.fromIO,
  248. fromTask: exports.fromTask
  249. };
  250. }
  251. exports.getMonad = getMonad;
  252. /**
  253. * @category instances
  254. * @since 2.10.0
  255. */
  256. exports.Functor = {
  257. URI: exports.URI,
  258. map: _map
  259. };
  260. /**
  261. * @category mapping
  262. * @since 2.10.0
  263. */
  264. exports.flap = (0, Functor_1.flap)(exports.Functor);
  265. /**
  266. * @category instances
  267. * @since 2.10.0
  268. */
  269. exports.Pointed = {
  270. URI: exports.URI,
  271. of: exports.of
  272. };
  273. /**
  274. * @category instances
  275. * @since 2.10.0
  276. */
  277. exports.Bifunctor = {
  278. URI: exports.URI,
  279. bimap: _bimap,
  280. mapLeft: _mapLeft
  281. };
  282. /**
  283. * @category instances
  284. * @since 2.10.0
  285. */
  286. exports.FromEither = {
  287. URI: exports.URI,
  288. fromEither: exports.fromEither
  289. };
  290. /**
  291. * @category conversions
  292. * @since 2.10.0
  293. */
  294. exports.fromOption =
  295. /*#__PURE__*/ (0, FromEither_1.fromOption)(exports.FromEither);
  296. /**
  297. * @category lifting
  298. * @since 2.10.0
  299. */
  300. exports.fromOptionK =
  301. /*#__PURE__*/ (0, FromEither_1.fromOptionK)(exports.FromEither);
  302. /**
  303. * @category lifting
  304. * @since 2.10.0
  305. */
  306. exports.fromPredicate = (0, FromEither_1.fromPredicate)(exports.FromEither);
  307. /**
  308. * @category instances
  309. * @since 2.11.0
  310. */
  311. exports.FromThese = {
  312. URI: exports.URI,
  313. fromThese: exports.fromThese
  314. };
  315. /**
  316. * @category lifting
  317. * @since 2.11.0
  318. */
  319. exports.fromTheseK = (0, FromThese_1.fromTheseK)(exports.FromThese);
  320. /**
  321. * @category instances
  322. * @since 2.10.0
  323. */
  324. exports.FromIO = {
  325. URI: exports.URI,
  326. fromIO: exports.fromIO
  327. };
  328. /**
  329. * @category lifting
  330. * @since 2.10.0
  331. */
  332. exports.fromIOK = (0, FromIO_1.fromIOK)(exports.FromIO);
  333. /**
  334. * @category instances
  335. * @since 2.10.0
  336. */
  337. exports.FromTask = {
  338. URI: exports.URI,
  339. fromIO: exports.fromIO,
  340. fromTask: exports.fromTask
  341. };
  342. /**
  343. * @category lifting
  344. * @since 2.10.0
  345. */
  346. exports.fromTaskK = (0, FromTask_1.fromTaskK)(exports.FromTask);
  347. // -------------------------------------------------------------------------------------
  348. // utils
  349. // -------------------------------------------------------------------------------------
  350. /**
  351. * @since 2.10.0
  352. */
  353. exports.toTuple2 =
  354. /*#__PURE__*/ TT.toTuple2(T.Functor);
  355. /**
  356. * @since 2.11.0
  357. */
  358. exports.ApT = (0, exports.of)(_.emptyReadonlyArray);
  359. // -------------------------------------------------------------------------------------
  360. // array utils
  361. // -------------------------------------------------------------------------------------
  362. /**
  363. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(T.ApplicativePar, S))`.
  364. *
  365. * @category traversing
  366. * @since 2.11.0
  367. */
  368. var traverseReadonlyNonEmptyArrayWithIndex = function (S) {
  369. var g = TH.traverseReadonlyNonEmptyArrayWithIndex(S);
  370. return function (f) { return (0, function_1.flow)(T.traverseReadonlyNonEmptyArrayWithIndex(f), T.map(g(function_1.SK))); };
  371. };
  372. exports.traverseReadonlyNonEmptyArrayWithIndex = traverseReadonlyNonEmptyArrayWithIndex;
  373. /**
  374. * Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(T.ApplicativePar, S))`.
  375. *
  376. * @category traversing
  377. * @since 2.11.0
  378. */
  379. var traverseReadonlyArrayWithIndex = function (S) {
  380. return function (f) {
  381. var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndex)(S)(f);
  382. return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
  383. };
  384. };
  385. exports.traverseReadonlyArrayWithIndex = traverseReadonlyArrayWithIndex;
  386. /**
  387. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(T.ApplicativeSeq, S))`.
  388. *
  389. * @category traversing
  390. * @since 2.11.0
  391. */
  392. var traverseReadonlyNonEmptyArrayWithIndexSeq = function (S) {
  393. return function (f) {
  394. return function (as) {
  395. return function () {
  396. return _.tail(as).reduce(function (acc, a, i) {
  397. return acc.then(function (ebs) {
  398. return TH.isLeft(ebs)
  399. ? acc
  400. : f(i + 1, a)().then(function (eb) {
  401. if (TH.isLeft(eb)) {
  402. return eb;
  403. }
  404. if (TH.isBoth(eb)) {
  405. var right_1 = ebs.right;
  406. right_1.push(eb.right);
  407. return TH.isBoth(ebs) ? TH.both(S.concat(ebs.left, eb.left), right_1) : TH.both(eb.left, right_1);
  408. }
  409. ebs.right.push(eb.right);
  410. return ebs;
  411. });
  412. });
  413. }, f(0, _.head(as))().then(TH.map(_.singleton)));
  414. };
  415. };
  416. };
  417. };
  418. exports.traverseReadonlyNonEmptyArrayWithIndexSeq = traverseReadonlyNonEmptyArrayWithIndexSeq;
  419. /**
  420. * Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(T.ApplicativeSeq, S))`.
  421. *
  422. * @category traversing
  423. * @since 2.11.0
  424. */
  425. var traverseReadonlyArrayWithIndexSeq = function (S) {
  426. return function (f) {
  427. var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndexSeq)(S)(f);
  428. return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
  429. };
  430. };
  431. exports.traverseReadonlyArrayWithIndexSeq = traverseReadonlyArrayWithIndexSeq;
  432. // -------------------------------------------------------------------------------------
  433. // deprecated
  434. // -------------------------------------------------------------------------------------
  435. /**
  436. * Use [`Functor`](#functor) instead.
  437. *
  438. * @category zone of death
  439. * @since 2.7.0
  440. * @deprecated
  441. */
  442. exports.functorTaskThese = {
  443. URI: exports.URI,
  444. map: _map
  445. };
  446. /**
  447. * Use [`Bifunctor`](#bifunctor) instead.
  448. *
  449. * @category zone of death
  450. * @since 2.7.0
  451. * @deprecated
  452. */
  453. exports.bifunctorTaskThese = {
  454. URI: exports.URI,
  455. bimap: _bimap,
  456. mapLeft: _mapLeft
  457. };
  458. /**
  459. * Use [`toTuple2`](#totuple2) instead.
  460. *
  461. * @category zone of death
  462. * @since 2.4.0
  463. * @deprecated
  464. */
  465. var toTuple = function (e, a) {
  466. return (0, exports.toTuple2)(function () { return e; }, function () { return a; });
  467. };
  468. exports.toTuple = toTuple;
  469. /**
  470. * This instance is deprecated, use small, specific instances instead.
  471. * For example if a function needs a `Functor` instance, pass `TT.Functor` instead of `TT.taskThese`
  472. * (where `TT` is from `import TT from 'fp-ts/TaskThese'`)
  473. *
  474. * @category zone of death
  475. * @since 2.4.0
  476. * @deprecated
  477. */
  478. exports.taskThese = {
  479. URI: exports.URI,
  480. map: _map,
  481. bimap: _bimap,
  482. mapLeft: _mapLeft
  483. };
  484. /**
  485. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  486. *
  487. * @category zone of death
  488. * @since 2.4.0
  489. * @deprecated
  490. */
  491. var getSemigroup = function (SE, SA) {
  492. return (0, Apply_1.getApplySemigroup)(T.ApplySeq)(TH.getSemigroup(SE, SA));
  493. };
  494. exports.getSemigroup = getSemigroup;