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

336 line
9.2 KiB

  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.getMonoid = exports.getSemigroup = exports.io = exports.sequenceArray = exports.traverseArray = exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex = exports.traverseReadonlyNonEmptyArrayWithIndex = exports.ApT = exports.apS = exports.bind = exports.let = exports.bindTo = exports.Do = exports.FromIO = exports.ChainRec = exports.MonadIO = exports.fromIO = exports.chainFirst = exports.Monad = exports.Chain = exports.Applicative = exports.apSecond = exports.apFirst = exports.Apply = exports.Pointed = exports.flap = exports.Functor = exports.URI = exports.flatten = exports.chain = exports.of = exports.ap = exports.map = void 0;
  27. /**
  28. * ```ts
  29. * interface IO<A> {
  30. * (): A
  31. * }
  32. * ```
  33. *
  34. * `IO<A>` represents a non-deterministic synchronous computation that can cause side effects, yields a value of
  35. * type `A` and **never fails**.
  36. *
  37. * If you want to represent a synchronous computation that may fail, please see `IOEither`.
  38. * If you want to represent a synchronous computation that may yield nothing, please see `IOOption`.
  39. *
  40. * @since 2.0.0
  41. */
  42. var Applicative_1 = require("./Applicative");
  43. var Apply_1 = require("./Apply");
  44. var Chain_1 = require("./Chain");
  45. var function_1 = require("./function");
  46. var Functor_1 = require("./Functor");
  47. var _ = __importStar(require("./internal"));
  48. var _map = function (ma, f) { return function () { return f(ma()); }; };
  49. var _ap = function (mab, ma) { return function () { return mab()(ma()); }; };
  50. var _chain = function (ma, f) { return function () { return f(ma())(); }; };
  51. var _chainRec = function (a, f) { return function () {
  52. var e = f(a)();
  53. while (e._tag === 'Left') {
  54. e = f(e.left)();
  55. }
  56. return e.right;
  57. }; };
  58. /**
  59. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  60. * use the type constructor `F` to represent some computational context.
  61. *
  62. * @category mapping
  63. * @since 2.0.0
  64. */
  65. var map = function (f) { return function (fa) { return _map(fa, f); }; };
  66. exports.map = map;
  67. /**
  68. * @since 2.0.0
  69. */
  70. var ap = function (fa) { return function (fab) { return _ap(fab, fa); }; };
  71. exports.ap = ap;
  72. /**
  73. * @category constructors
  74. * @since 2.0.0
  75. */
  76. exports.of = function_1.constant;
  77. /**
  78. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  79. *
  80. * @category sequencing
  81. * @since 2.0.0
  82. */
  83. var chain = function (f) { return function (ma) { return _chain(ma, f); }; };
  84. exports.chain = chain;
  85. /**
  86. * @category sequencing
  87. * @since 2.0.0
  88. */
  89. exports.flatten = (0, exports.chain)(function_1.identity);
  90. /**
  91. * @category type lambdas
  92. * @since 2.0.0
  93. */
  94. exports.URI = 'IO';
  95. /**
  96. * @category instances
  97. * @since 2.7.0
  98. */
  99. exports.Functor = {
  100. URI: exports.URI,
  101. map: _map
  102. };
  103. /**
  104. * @category mapping
  105. * @since 2.10.0
  106. */
  107. exports.flap = (0, Functor_1.flap)(exports.Functor);
  108. /**
  109. * @category instances
  110. * @since 2.10.0
  111. */
  112. exports.Pointed = {
  113. URI: exports.URI,
  114. of: exports.of
  115. };
  116. /**
  117. * @category instances
  118. * @since 2.10.0
  119. */
  120. exports.Apply = {
  121. URI: exports.URI,
  122. map: _map,
  123. ap: _ap
  124. };
  125. /**
  126. * Combine two effectful actions, keeping only the result of the first.
  127. *
  128. * @since 2.0.0
  129. */
  130. exports.apFirst = (0, Apply_1.apFirst)(exports.Apply);
  131. /**
  132. * Combine two effectful actions, keeping only the result of the second.
  133. *
  134. * @since 2.0.0
  135. */
  136. exports.apSecond = (0, Apply_1.apSecond)(exports.Apply);
  137. /**
  138. * @category instances
  139. * @since 2.7.0
  140. */
  141. exports.Applicative = {
  142. URI: exports.URI,
  143. map: _map,
  144. ap: _ap,
  145. of: exports.of
  146. };
  147. /**
  148. * @category instances
  149. * @since 2.10.0
  150. */
  151. exports.Chain = {
  152. URI: exports.URI,
  153. map: _map,
  154. ap: _ap,
  155. chain: _chain
  156. };
  157. /**
  158. * @category instances
  159. * @since 2.7.0
  160. */
  161. exports.Monad = {
  162. URI: exports.URI,
  163. map: _map,
  164. ap: _ap,
  165. of: exports.of,
  166. chain: _chain
  167. };
  168. /**
  169. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  170. * keeping only the result of the first.
  171. *
  172. * @category sequencing
  173. * @since 2.0.0
  174. */
  175. exports.chainFirst = (0, Chain_1.chainFirst)(exports.Chain);
  176. /**
  177. * @category zone of death
  178. * @since 2.7.0
  179. * @deprecated
  180. */
  181. exports.fromIO = function_1.identity;
  182. /**
  183. * @category instances
  184. * @since 2.7.0
  185. */
  186. exports.MonadIO = {
  187. URI: exports.URI,
  188. map: _map,
  189. ap: _ap,
  190. of: exports.of,
  191. chain: _chain,
  192. fromIO: exports.fromIO
  193. };
  194. /**
  195. * @category instances
  196. * @since 2.7.0
  197. */
  198. exports.ChainRec = {
  199. URI: exports.URI,
  200. map: _map,
  201. ap: _ap,
  202. chain: _chain,
  203. chainRec: _chainRec
  204. };
  205. /**
  206. * @category instances
  207. * @since 2.10.0
  208. */
  209. exports.FromIO = {
  210. URI: exports.URI,
  211. fromIO: function_1.identity
  212. };
  213. // -------------------------------------------------------------------------------------
  214. // do notation
  215. // -------------------------------------------------------------------------------------
  216. /**
  217. * @category do notation
  218. * @since 2.9.0
  219. */
  220. exports.Do = (0, exports.of)(_.emptyRecord);
  221. /**
  222. * @category do notation
  223. * @since 2.8.0
  224. */
  225. exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
  226. var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
  227. exports.let = let_;
  228. /**
  229. * @category do notation
  230. * @since 2.8.0
  231. */
  232. exports.bind = (0, Chain_1.bind)(exports.Chain);
  233. /**
  234. * @category do notation
  235. * @since 2.8.0
  236. */
  237. exports.apS = (0, Apply_1.apS)(exports.Apply);
  238. /**
  239. * @since 2.11.0
  240. */
  241. exports.ApT = (0, exports.of)(_.emptyReadonlyArray);
  242. // -------------------------------------------------------------------------------------
  243. // array utils
  244. // -------------------------------------------------------------------------------------
  245. /**
  246. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
  247. *
  248. * @category traversing
  249. * @since 2.11.0
  250. */
  251. var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
  252. return function (as) {
  253. return function () {
  254. var out = [f(0, _.head(as))()];
  255. for (var i = 1; i < as.length; i++) {
  256. out.push(f(i, as[i])());
  257. }
  258. return out;
  259. };
  260. };
  261. };
  262. exports.traverseReadonlyNonEmptyArrayWithIndex = traverseReadonlyNonEmptyArrayWithIndex;
  263. /**
  264. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  265. *
  266. * @category traversing
  267. * @since 2.11.0
  268. */
  269. var traverseReadonlyArrayWithIndex = function (f) {
  270. var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndex)(f);
  271. return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
  272. };
  273. exports.traverseReadonlyArrayWithIndex = traverseReadonlyArrayWithIndex;
  274. /**
  275. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  276. *
  277. * @category traversing
  278. * @since 2.9.0
  279. */
  280. exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex;
  281. /**
  282. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  283. *
  284. * @category traversing
  285. * @since 2.9.0
  286. */
  287. var traverseArray = function (f) {
  288. return (0, exports.traverseReadonlyArrayWithIndex)(function (_, a) { return f(a); });
  289. };
  290. exports.traverseArray = traverseArray;
  291. /**
  292. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  293. *
  294. * @category traversing
  295. * @since 2.9.0
  296. */
  297. exports.sequenceArray =
  298. /*#__PURE__*/ (0, exports.traverseArray)(function_1.identity);
  299. // -------------------------------------------------------------------------------------
  300. // deprecated
  301. // -------------------------------------------------------------------------------------
  302. /**
  303. * This instance is deprecated, use small, specific instances instead.
  304. * For example if a function needs a `Functor` instance, pass `IO.Functor` instead of `IO.io`
  305. * (where `IO` is from `import IO from 'fp-ts/IO'`)
  306. *
  307. * @category zone of death
  308. * @since 2.0.0
  309. * @deprecated
  310. */
  311. exports.io = {
  312. URI: exports.URI,
  313. map: _map,
  314. of: exports.of,
  315. ap: _ap,
  316. chain: _chain,
  317. fromIO: exports.fromIO,
  318. chainRec: _chainRec
  319. };
  320. /**
  321. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  322. *
  323. * @category zone of death
  324. * @since 2.0.0
  325. * @deprecated
  326. */
  327. exports.getSemigroup = (0, Apply_1.getApplySemigroup)(exports.Apply);
  328. /**
  329. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  330. *
  331. * @category zone of death
  332. * @since 2.0.0
  333. * @deprecated
  334. */
  335. exports.getMonoid = (0, Applicative_1.getApplicativeMonoid)(exports.Applicative);