版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

317 righe
6.9 KiB

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