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

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