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

309 строки
7.2 KiB

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