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

211 lines
5.6 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.writer = exports.execWriter = exports.evalWriter = exports.execute = exports.evaluate = exports.flap = exports.Functor = exports.getMonad = exports.getChain = exports.getApplicative = exports.getApply = exports.getPointed = exports.URI = exports.map = exports.censor = exports.listens = exports.pass = exports.listen = exports.tell = void 0;
  4. var function_1 = require("./function");
  5. var Functor_1 = require("./Functor");
  6. // -------------------------------------------------------------------------------------
  7. // constructors
  8. // -------------------------------------------------------------------------------------
  9. /**
  10. * Appends a value to the accumulator
  11. *
  12. * @category constructors
  13. * @since 2.0.0
  14. */
  15. var tell = function (w) { return function () { return [undefined, w]; }; };
  16. exports.tell = tell;
  17. // -------------------------------------------------------------------------------------
  18. // combinators
  19. // -------------------------------------------------------------------------------------
  20. /**
  21. * Modifies the result to include the changes to the accumulator
  22. *
  23. * @since 2.0.0
  24. */
  25. var listen = function (fa) { return function () {
  26. var _a = fa(), a = _a[0], w = _a[1];
  27. return [[a, w], w];
  28. }; };
  29. exports.listen = listen;
  30. /**
  31. * Applies the returned function to the accumulator
  32. *
  33. * @since 2.0.0
  34. */
  35. var pass = function (fa) { return function () {
  36. var _a = fa(), _b = _a[0], a = _b[0], f = _b[1], w = _a[1];
  37. return [a, f(w)];
  38. }; };
  39. exports.pass = pass;
  40. /**
  41. * Projects a value from modifications made to the accumulator during an action
  42. *
  43. * @since 2.0.0
  44. */
  45. var listens = function (f) { return function (fa) { return function () {
  46. var _a = fa(), a = _a[0], w = _a[1];
  47. return [[a, f(w)], w];
  48. }; }; };
  49. exports.listens = listens;
  50. /**
  51. * Modify the final accumulator value by applying a function
  52. *
  53. * @since 2.0.0
  54. */
  55. var censor = function (f) { return function (fa) { return function () {
  56. var _a = fa(), a = _a[0], w = _a[1];
  57. return [a, f(w)];
  58. }; }; };
  59. exports.censor = censor;
  60. /* istanbul ignore next */
  61. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
  62. /**
  63. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  64. * use the type constructor `F` to represent some computational context.
  65. *
  66. * @category mapping
  67. * @since 2.0.0
  68. */
  69. var map = function (f) { return function (fa) { return function () {
  70. var _a = fa(), a = _a[0], w = _a[1];
  71. return [f(a), w];
  72. }; }; };
  73. exports.map = map;
  74. /**
  75. * @category type lambdas
  76. * @since 2.0.0
  77. */
  78. exports.URI = 'Writer';
  79. /**
  80. * @category instances
  81. * @since 2.10.0
  82. */
  83. var getPointed = function (M) { return ({
  84. URI: exports.URI,
  85. _E: undefined,
  86. of: function (a) { return function () { return [a, M.empty]; }; }
  87. }); };
  88. exports.getPointed = getPointed;
  89. /**
  90. * @category instances
  91. * @since 2.10.0
  92. */
  93. var getApply = function (S) { return ({
  94. URI: exports.URI,
  95. _E: undefined,
  96. map: _map,
  97. ap: function (fab, fa) { return function () {
  98. var _a = fab(), f = _a[0], w1 = _a[1];
  99. var _b = fa(), a = _b[0], w2 = _b[1];
  100. return [f(a), S.concat(w1, w2)];
  101. }; }
  102. }); };
  103. exports.getApply = getApply;
  104. /**
  105. * @category instances
  106. * @since 2.10.0
  107. */
  108. var getApplicative = function (M) {
  109. var A = (0, exports.getApply)(M);
  110. var P = (0, exports.getPointed)(M);
  111. return {
  112. URI: exports.URI,
  113. _E: undefined,
  114. map: _map,
  115. ap: A.ap,
  116. of: P.of
  117. };
  118. };
  119. exports.getApplicative = getApplicative;
  120. /**
  121. * @category instances
  122. * @since 2.10.0
  123. */
  124. function getChain(S) {
  125. var A = (0, exports.getApply)(S);
  126. return {
  127. URI: exports.URI,
  128. _E: undefined,
  129. map: _map,
  130. ap: A.ap,
  131. chain: function (fa, f) { return function () {
  132. var _a = fa(), a = _a[0], w1 = _a[1];
  133. var _b = f(a)(), b = _b[0], w2 = _b[1];
  134. return [b, S.concat(w1, w2)];
  135. }; }
  136. };
  137. }
  138. exports.getChain = getChain;
  139. /**
  140. * @category instances
  141. * @since 2.0.0
  142. */
  143. function getMonad(M) {
  144. var A = (0, exports.getApplicative)(M);
  145. var C = getChain(M);
  146. return {
  147. URI: exports.URI,
  148. _E: undefined,
  149. map: _map,
  150. ap: A.ap,
  151. of: A.of,
  152. chain: C.chain
  153. };
  154. }
  155. exports.getMonad = getMonad;
  156. /**
  157. * @category instances
  158. * @since 2.7.0
  159. */
  160. exports.Functor = {
  161. URI: exports.URI,
  162. map: _map
  163. };
  164. /**
  165. * @category mapping
  166. * @since 2.10.0
  167. */
  168. exports.flap = (0, Functor_1.flap)(exports.Functor);
  169. // -------------------------------------------------------------------------------------
  170. // utils
  171. // -------------------------------------------------------------------------------------
  172. /**
  173. * @since 2.8.0
  174. */
  175. var evaluate = function (fa) { return fa()[0]; };
  176. exports.evaluate = evaluate;
  177. /**
  178. * @since 2.8.0
  179. */
  180. var execute = function (fa) { return fa()[1]; };
  181. exports.execute = execute;
  182. // -------------------------------------------------------------------------------------
  183. // deprecated
  184. // -------------------------------------------------------------------------------------
  185. /**
  186. * Use [`evaluate`](#evaluate) instead
  187. *
  188. * @category zone of death
  189. * @since 2.0.0
  190. * @deprecated
  191. */
  192. var evalWriter = function (fa) { return fa()[0]; };
  193. exports.evalWriter = evalWriter;
  194. /**
  195. * Use [`execute`](#execute) instead
  196. *
  197. * @category zone of death
  198. * @since 2.0.0
  199. * @deprecated
  200. */
  201. var execWriter = function (fa) { return fa()[1]; };
  202. exports.execWriter = execWriter;
  203. /**
  204. * Use [`Functor`](#functor) instead.
  205. *
  206. * @category zone of death
  207. * @since 2.0.0
  208. * @deprecated
  209. */
  210. exports.writer = exports.Functor;