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

351 строка
8.5 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.readonlyTuple = exports.Traversable = exports.Foldable = exports.Comonad = exports.Semigroupoid = exports.Bifunctor = exports.mapLeft = exports.map = exports.flap = exports.Functor = exports.URI = exports.sequence = exports.traverse = exports.reduceRight = exports.foldMap = exports.reduce = exports.duplicate = exports.extract = exports.extend = exports.compose = exports.mapSnd = exports.mapFst = exports.bimap = exports.getChainRec = exports.getMonad = exports.getChain = exports.getApplicative = exports.getApply = exports.swap = exports.snd = exports.fst = void 0;
  4. var function_1 = require("./function");
  5. var Functor_1 = require("./Functor");
  6. // -------------------------------------------------------------------------------------
  7. // model
  8. // -------------------------------------------------------------------------------------
  9. /**
  10. * @since 2.5.0
  11. */
  12. function fst(ea) {
  13. return ea[0];
  14. }
  15. exports.fst = fst;
  16. /**
  17. * @since 2.5.0
  18. */
  19. function snd(ea) {
  20. return ea[1];
  21. }
  22. exports.snd = snd;
  23. /**
  24. * @since 2.5.0
  25. */
  26. var swap = function (ea) { return [snd(ea), fst(ea)]; };
  27. exports.swap = swap;
  28. /**
  29. * @category instances
  30. * @since 2.5.0
  31. */
  32. function getApply(S) {
  33. return {
  34. URI: exports.URI,
  35. _E: undefined,
  36. map: _map,
  37. ap: function (fab, fa) { return [fst(fab)(fst(fa)), S.concat(snd(fab), snd(fa))]; }
  38. };
  39. }
  40. exports.getApply = getApply;
  41. var of = function (M) {
  42. return function (a) {
  43. return [a, M.empty];
  44. };
  45. };
  46. /**
  47. * @category instances
  48. * @since 2.5.0
  49. */
  50. function getApplicative(M) {
  51. var A = getApply(M);
  52. return {
  53. URI: exports.URI,
  54. _E: undefined,
  55. map: _map,
  56. ap: A.ap,
  57. of: of(M)
  58. };
  59. }
  60. exports.getApplicative = getApplicative;
  61. /**
  62. * @category instances
  63. * @since 2.5.0
  64. */
  65. function getChain(S) {
  66. var A = getApply(S);
  67. return {
  68. URI: exports.URI,
  69. _E: undefined,
  70. map: _map,
  71. ap: A.ap,
  72. chain: function (ma, f) {
  73. var _a = f(fst(ma)), b = _a[0], s = _a[1];
  74. return [b, S.concat(snd(ma), s)];
  75. }
  76. };
  77. }
  78. exports.getChain = getChain;
  79. /**
  80. * @category instances
  81. * @since 2.5.0
  82. */
  83. function getMonad(M) {
  84. var C = getChain(M);
  85. return {
  86. URI: exports.URI,
  87. _E: undefined,
  88. map: _map,
  89. ap: C.ap,
  90. chain: C.chain,
  91. of: of(M)
  92. };
  93. }
  94. exports.getMonad = getMonad;
  95. /**
  96. * @category instances
  97. * @since 2.5.0
  98. */
  99. function getChainRec(M) {
  100. var chainRec = function (a, f) {
  101. var result = f(a);
  102. var acc = M.empty;
  103. var s = fst(result);
  104. while (s._tag === 'Left') {
  105. acc = M.concat(acc, snd(result));
  106. result = f(s.left);
  107. s = fst(result);
  108. }
  109. return [s.right, M.concat(acc, snd(result))];
  110. };
  111. var C = getChain(M);
  112. return {
  113. URI: exports.URI,
  114. _E: undefined,
  115. map: _map,
  116. ap: C.ap,
  117. chain: C.chain,
  118. chainRec: chainRec
  119. };
  120. }
  121. exports.getChainRec = getChainRec;
  122. /* istanbul ignore next */
  123. var _compose = function (bc, ab) { return (0, function_1.pipe)(bc, (0, exports.compose)(ab)); };
  124. /* istanbul ignore next */
  125. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.mapFst)(f)); };
  126. /* istanbul ignore next */
  127. var _bimap = function (fa, f, g) { return (0, function_1.pipe)(fa, (0, exports.bimap)(f, g)); };
  128. /* istanbul ignore next */
  129. var _mapLeft = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.mapSnd)(f)); };
  130. /* istanbul ignore next */
  131. var _extend = function (wa, f) { return (0, function_1.pipe)(wa, (0, exports.extend)(f)); };
  132. /* istanbul ignore next */
  133. var _reduce = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduce)(b, f)); };
  134. /* istanbul ignore next */
  135. var _foldMap = function (M) {
  136. var foldMapM = (0, exports.foldMap)(M);
  137. return function (fa, f) { return (0, function_1.pipe)(fa, foldMapM(f)); };
  138. };
  139. /* istanbul ignore next */
  140. var _reduceRight = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduceRight)(b, f)); };
  141. /* istanbul ignore next */
  142. var _traverse = function (F) {
  143. var traverseF = (0, exports.traverse)(F);
  144. return function (ta, f) { return (0, function_1.pipe)(ta, traverseF(f)); };
  145. };
  146. /**
  147. * Map a pair of functions over the two type arguments of the bifunctor.
  148. *
  149. * @category mapping
  150. * @since 2.5.0
  151. */
  152. var bimap = function (f, g) { return function (fa) { return [g(fst(fa)), f(snd(fa))]; }; };
  153. exports.bimap = bimap;
  154. /**
  155. * Map a function over the first component of a `ReadonlyTuple`.
  156. *
  157. * This is the `map` operation of the `Functor` instance.
  158. *
  159. * @category mapping
  160. * @since 2.10.0
  161. */
  162. var mapFst = function (f) { return function (fa) {
  163. return [f(fst(fa)), snd(fa)];
  164. }; };
  165. exports.mapFst = mapFst;
  166. /**
  167. * Map a function over the second component of a `ReadonlyTuple`.
  168. *
  169. * This is the `mapLeft` operation of the `Bifunctor` instance.
  170. *
  171. * @category mapping
  172. * @since 2.10.0
  173. */
  174. var mapSnd = function (f) { return function (fa) {
  175. return [fst(fa), f(snd(fa))];
  176. }; };
  177. exports.mapSnd = mapSnd;
  178. /**
  179. * @since 2.5.0
  180. */
  181. var compose = function (ab) { return function (bc) {
  182. return [fst(bc), snd(ab)];
  183. }; };
  184. exports.compose = compose;
  185. /**
  186. * @since 2.5.0
  187. */
  188. var extend = function (f) { return function (wa) {
  189. return [f(wa), snd(wa)];
  190. }; };
  191. exports.extend = extend;
  192. /**
  193. * @category Extract
  194. * @since 2.6.2
  195. */
  196. exports.extract = fst;
  197. /**
  198. * @since 2.5.0
  199. */
  200. exports.duplicate = (0, exports.extend)(function_1.identity);
  201. /**
  202. * @category folding
  203. * @since 2.5.0
  204. */
  205. var reduce = function (b, f) { return function (fa) {
  206. return f(b, fst(fa));
  207. }; };
  208. exports.reduce = reduce;
  209. /**
  210. * @category folding
  211. * @since 2.5.0
  212. */
  213. var foldMap = function () {
  214. return function (f) { return function (fa) { return f(fst(fa)); }; };
  215. };
  216. exports.foldMap = foldMap;
  217. /**
  218. * @category folding
  219. * @since 2.5.0
  220. */
  221. var reduceRight = function (b, f) { return function (fa) {
  222. return f(fst(fa), b);
  223. }; };
  224. exports.reduceRight = reduceRight;
  225. /**
  226. * @category traversing
  227. * @since 2.6.3
  228. */
  229. var traverse = function (F) {
  230. return function (f) { return function (ta) { return F.map(f(fst(ta)), function (b) { return [b, snd(ta)]; }); }; };
  231. };
  232. exports.traverse = traverse;
  233. /**
  234. * @category traversing
  235. * @since 2.6.3
  236. */
  237. var sequence = function (F) {
  238. return function (fas) {
  239. return F.map(fst(fas), function (a) { return [a, snd(fas)]; });
  240. };
  241. };
  242. exports.sequence = sequence;
  243. /**
  244. * @category type lambdas
  245. * @since 2.5.0
  246. */
  247. exports.URI = 'ReadonlyTuple';
  248. /**
  249. * @category instances
  250. * @since 2.7.0
  251. */
  252. exports.Functor = {
  253. URI: exports.URI,
  254. map: _map
  255. };
  256. /**
  257. * @category mapping
  258. * @since 2.10.0
  259. */
  260. exports.flap = (0, Functor_1.flap)(exports.Functor);
  261. /**
  262. * Alias of [`mapFst`](#mapfst).
  263. *
  264. * @category mapping
  265. * @since 2.5.0
  266. */
  267. exports.map = exports.mapFst;
  268. /**
  269. * Alias of [`mapSnd`](#mapsnd).
  270. *
  271. * @category error handling
  272. * @since 2.5.0
  273. */
  274. exports.mapLeft = exports.mapSnd;
  275. /**
  276. * @category instances
  277. * @since 2.7.0
  278. */
  279. exports.Bifunctor = {
  280. URI: exports.URI,
  281. bimap: _bimap,
  282. mapLeft: _mapLeft
  283. };
  284. /**
  285. * @category instances
  286. * @since 2.7.0
  287. */
  288. exports.Semigroupoid = {
  289. URI: exports.URI,
  290. compose: _compose
  291. };
  292. /**
  293. * @category instances
  294. * @since 2.7.0
  295. */
  296. exports.Comonad = {
  297. URI: exports.URI,
  298. map: _map,
  299. extend: _extend,
  300. extract: exports.extract
  301. };
  302. /**
  303. * @category instances
  304. * @since 2.7.0
  305. */
  306. exports.Foldable = {
  307. URI: exports.URI,
  308. reduce: _reduce,
  309. foldMap: _foldMap,
  310. reduceRight: _reduceRight
  311. };
  312. /**
  313. * @category instances
  314. * @since 2.7.0
  315. */
  316. exports.Traversable = {
  317. URI: exports.URI,
  318. map: _map,
  319. reduce: _reduce,
  320. foldMap: _foldMap,
  321. reduceRight: _reduceRight,
  322. traverse: _traverse,
  323. sequence: exports.sequence
  324. };
  325. // -------------------------------------------------------------------------------------
  326. // deprecated
  327. // -------------------------------------------------------------------------------------
  328. /**
  329. * This instance is deprecated, use small, specific instances instead.
  330. * For example if a function needs a `Functor` instance, pass `RT.Functor` instead of `RT.readonlyTuple`
  331. * (where `RT` is from `import RT from 'fp-ts/ReadonlyTuple'`)
  332. *
  333. * @category zone of death
  334. * @since 2.5.0
  335. * @deprecated
  336. */
  337. exports.readonlyTuple = {
  338. URI: exports.URI,
  339. compose: _compose,
  340. map: _map,
  341. bimap: _bimap,
  342. mapLeft: _mapLeft,
  343. extract: exports.extract,
  344. extend: _extend,
  345. reduce: _reduce,
  346. foldMap: _foldMap,
  347. reduceRight: _reduceRight,
  348. traverse: _traverse,
  349. sequence: exports.sequence
  350. };