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

326 строки
8.1 KiB

  1. // -------------------------------------------------------------------------------------
  2. // instances
  3. // -------------------------------------------------------------------------------------
  4. /**
  5. * @category instances
  6. * @since 2.10.0
  7. */
  8. export var getBooleanAlgebra = function (B) {
  9. return function () { return ({
  10. meet: function (x, y) { return function (a) { return B.meet(x(a), y(a)); }; },
  11. join: function (x, y) { return function (a) { return B.join(x(a), y(a)); }; },
  12. zero: function () { return B.zero; },
  13. one: function () { return B.one; },
  14. implies: function (x, y) { return function (a) { return B.implies(x(a), y(a)); }; },
  15. not: function (x) { return function (a) { return B.not(x(a)); }; }
  16. }); };
  17. };
  18. /**
  19. * Unary functions form a semigroup as long as you can provide a semigroup for the codomain.
  20. *
  21. * @example
  22. * import { Predicate, getSemigroup } from 'fp-ts/function'
  23. * import * as B from 'fp-ts/boolean'
  24. *
  25. * const f: Predicate<number> = (n) => n <= 2
  26. * const g: Predicate<number> = (n) => n >= 0
  27. *
  28. * const S1 = getSemigroup(B.SemigroupAll)<number>()
  29. *
  30. * assert.deepStrictEqual(S1.concat(f, g)(1), true)
  31. * assert.deepStrictEqual(S1.concat(f, g)(3), false)
  32. *
  33. * const S2 = getSemigroup(B.SemigroupAny)<number>()
  34. *
  35. * assert.deepStrictEqual(S2.concat(f, g)(1), true)
  36. * assert.deepStrictEqual(S2.concat(f, g)(3), true)
  37. *
  38. * @category instances
  39. * @since 2.10.0
  40. */
  41. export var getSemigroup = function (S) {
  42. return function () { return ({
  43. concat: function (f, g) { return function (a) { return S.concat(f(a), g(a)); }; }
  44. }); };
  45. };
  46. /**
  47. * Unary functions form a monoid as long as you can provide a monoid for the codomain.
  48. *
  49. * @example
  50. * import { Predicate } from 'fp-ts/Predicate'
  51. * import { getMonoid } from 'fp-ts/function'
  52. * import * as B from 'fp-ts/boolean'
  53. *
  54. * const f: Predicate<number> = (n) => n <= 2
  55. * const g: Predicate<number> = (n) => n >= 0
  56. *
  57. * const M1 = getMonoid(B.MonoidAll)<number>()
  58. *
  59. * assert.deepStrictEqual(M1.concat(f, g)(1), true)
  60. * assert.deepStrictEqual(M1.concat(f, g)(3), false)
  61. *
  62. * const M2 = getMonoid(B.MonoidAny)<number>()
  63. *
  64. * assert.deepStrictEqual(M2.concat(f, g)(1), true)
  65. * assert.deepStrictEqual(M2.concat(f, g)(3), true)
  66. *
  67. * @category instances
  68. * @since 2.10.0
  69. */
  70. export var getMonoid = function (M) {
  71. var getSemigroupM = getSemigroup(M);
  72. return function () { return ({
  73. concat: getSemigroupM().concat,
  74. empty: function () { return M.empty; }
  75. }); };
  76. };
  77. /**
  78. * @category instances
  79. * @since 2.10.0
  80. */
  81. export var getSemiring = function (S) { return ({
  82. add: function (f, g) { return function (x) { return S.add(f(x), g(x)); }; },
  83. zero: function () { return S.zero; },
  84. mul: function (f, g) { return function (x) { return S.mul(f(x), g(x)); }; },
  85. one: function () { return S.one; }
  86. }); };
  87. /**
  88. * @category instances
  89. * @since 2.10.0
  90. */
  91. export var getRing = function (R) {
  92. var S = getSemiring(R);
  93. return {
  94. add: S.add,
  95. mul: S.mul,
  96. one: S.one,
  97. zero: S.zero,
  98. sub: function (f, g) { return function (x) { return R.sub(f(x), g(x)); }; }
  99. };
  100. };
  101. // -------------------------------------------------------------------------------------
  102. // utils
  103. // -------------------------------------------------------------------------------------
  104. /**
  105. * @since 2.11.0
  106. */
  107. export var apply = function (a) {
  108. return function (f) {
  109. return f(a);
  110. };
  111. };
  112. /**
  113. * @since 2.0.0
  114. */
  115. export function identity(a) {
  116. return a;
  117. }
  118. /**
  119. * @since 2.0.0
  120. */
  121. export var unsafeCoerce = identity;
  122. /**
  123. * @since 2.0.0
  124. */
  125. export function constant(a) {
  126. return function () { return a; };
  127. }
  128. /**
  129. * A thunk that returns always `true`.
  130. *
  131. * @since 2.0.0
  132. */
  133. export var constTrue = /*#__PURE__*/ constant(true);
  134. /**
  135. * A thunk that returns always `false`.
  136. *
  137. * @since 2.0.0
  138. */
  139. export var constFalse = /*#__PURE__*/ constant(false);
  140. /**
  141. * A thunk that returns always `null`.
  142. *
  143. * @since 2.0.0
  144. */
  145. export var constNull = /*#__PURE__*/ constant(null);
  146. /**
  147. * A thunk that returns always `undefined`.
  148. *
  149. * @since 2.0.0
  150. */
  151. export var constUndefined = /*#__PURE__*/ constant(undefined);
  152. /**
  153. * A thunk that returns always `void`.
  154. *
  155. * @since 2.0.0
  156. */
  157. export var constVoid = constUndefined;
  158. export function flip(f) {
  159. return function () {
  160. var args = [];
  161. for (var _i = 0; _i < arguments.length; _i++) {
  162. args[_i] = arguments[_i];
  163. }
  164. if (args.length > 1) {
  165. return f(args[1], args[0]);
  166. }
  167. return function (a) { return f(a)(args[0]); };
  168. };
  169. }
  170. export function flow(ab, bc, cd, de, ef, fg, gh, hi, ij) {
  171. switch (arguments.length) {
  172. case 1:
  173. return ab;
  174. case 2:
  175. return function () {
  176. return bc(ab.apply(this, arguments));
  177. };
  178. case 3:
  179. return function () {
  180. return cd(bc(ab.apply(this, arguments)));
  181. };
  182. case 4:
  183. return function () {
  184. return de(cd(bc(ab.apply(this, arguments))));
  185. };
  186. case 5:
  187. return function () {
  188. return ef(de(cd(bc(ab.apply(this, arguments)))));
  189. };
  190. case 6:
  191. return function () {
  192. return fg(ef(de(cd(bc(ab.apply(this, arguments))))));
  193. };
  194. case 7:
  195. return function () {
  196. return gh(fg(ef(de(cd(bc(ab.apply(this, arguments)))))));
  197. };
  198. case 8:
  199. return function () {
  200. return hi(gh(fg(ef(de(cd(bc(ab.apply(this, arguments))))))));
  201. };
  202. case 9:
  203. return function () {
  204. return ij(hi(gh(fg(ef(de(cd(bc(ab.apply(this, arguments)))))))));
  205. };
  206. }
  207. return;
  208. }
  209. /**
  210. * @since 2.0.0
  211. */
  212. export function tuple() {
  213. var t = [];
  214. for (var _i = 0; _i < arguments.length; _i++) {
  215. t[_i] = arguments[_i];
  216. }
  217. return t;
  218. }
  219. /**
  220. * @since 2.0.0
  221. */
  222. export function increment(n) {
  223. return n + 1;
  224. }
  225. /**
  226. * @since 2.0.0
  227. */
  228. export function decrement(n) {
  229. return n - 1;
  230. }
  231. /**
  232. * @since 2.0.0
  233. */
  234. export function absurd(_) {
  235. throw new Error('Called `absurd` function which should be uncallable');
  236. }
  237. /**
  238. * Creates a tupled version of this function: instead of `n` arguments, it accepts a single tuple argument.
  239. *
  240. * @example
  241. * import { tupled } from 'fp-ts/function'
  242. *
  243. * const add = tupled((x: number, y: number): number => x + y)
  244. *
  245. * assert.strictEqual(add([1, 2]), 3)
  246. *
  247. * @since 2.4.0
  248. */
  249. export function tupled(f) {
  250. return function (a) { return f.apply(void 0, a); };
  251. }
  252. /**
  253. * Inverse function of `tupled`
  254. *
  255. * @since 2.4.0
  256. */
  257. export function untupled(f) {
  258. return function () {
  259. var a = [];
  260. for (var _i = 0; _i < arguments.length; _i++) {
  261. a[_i] = arguments[_i];
  262. }
  263. return f(a);
  264. };
  265. }
  266. export function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
  267. switch (arguments.length) {
  268. case 1:
  269. return a;
  270. case 2:
  271. return ab(a);
  272. case 3:
  273. return bc(ab(a));
  274. case 4:
  275. return cd(bc(ab(a)));
  276. case 5:
  277. return de(cd(bc(ab(a))));
  278. case 6:
  279. return ef(de(cd(bc(ab(a)))));
  280. case 7:
  281. return fg(ef(de(cd(bc(ab(a))))));
  282. case 8:
  283. return gh(fg(ef(de(cd(bc(ab(a)))))));
  284. case 9:
  285. return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
  286. default: {
  287. var ret = arguments[0];
  288. for (var i = 1; i < arguments.length; i++) {
  289. ret = arguments[i](ret);
  290. }
  291. return ret;
  292. }
  293. }
  294. }
  295. /**
  296. * Type hole simulation
  297. *
  298. * @since 2.7.0
  299. */
  300. export var hole = absurd;
  301. /**
  302. * @since 2.11.0
  303. */
  304. export var SK = function (_, b) { return b; };
  305. /**
  306. * Use `Predicate` module instead.
  307. *
  308. * @category zone of death
  309. * @since 2.0.0
  310. * @deprecated
  311. */
  312. export function not(predicate) {
  313. return function (a) { return !predicate(a); };
  314. }
  315. /**
  316. * Use `Endomorphism` module instead.
  317. *
  318. * @category zone of death
  319. * @since 2.10.0
  320. * @deprecated
  321. */
  322. export var getEndomorphismMonoid = function () { return ({
  323. concat: function (first, second) { return flow(first, second); },
  324. empty: identity
  325. }); };