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

309 строки
7.8 KiB

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