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

199 righe
7.3 KiB

  1. import { apFirst as apFirst_, apSecond as apSecond_ } from './Apply';
  2. import { chainFirst as chainFirst_ } from './Chain';
  3. import { identity, pipe as pipeFromFunctionModule } from './function';
  4. export function map(F) {
  5. return function (f) { return function (fa) { return F.map(fa, f); }; };
  6. }
  7. export function contramap(F) {
  8. return function (f) { return function (fa) { return F.contramap(fa, f); }; };
  9. }
  10. export function mapWithIndex(F) {
  11. return function (f) { return function (fa) { return F.mapWithIndex(fa, f); }; };
  12. }
  13. export function ap(F) {
  14. return function (fa) { return function (fab) { return F.ap(fab, fa); }; };
  15. }
  16. export function chain(F) {
  17. return function (f) { return function (fa) { return F.chain(fa, f); }; };
  18. }
  19. export function bimap(F) {
  20. return function (f, g) { return function (fea) { return F.bimap(fea, f, g); }; };
  21. }
  22. export function mapLeft(F) {
  23. return function (f) { return function (fea) { return F.mapLeft(fea, f); }; };
  24. }
  25. export function extend(F) {
  26. return function (f) { return function (wa) { return F.extend(wa, f); }; };
  27. }
  28. export function reduce(F) {
  29. return function (b, f) { return function (fa) { return F.reduce(fa, b, f); }; };
  30. }
  31. export function foldMap(F) {
  32. return function (M) {
  33. var foldMapM = F.foldMap(M);
  34. return function (f) { return function (fa) { return foldMapM(fa, f); }; };
  35. };
  36. }
  37. export function reduceRight(F) {
  38. return function (b, f) { return function (fa) { return F.reduceRight(fa, b, f); }; };
  39. }
  40. export function reduceWithIndex(F) {
  41. return function (b, f) { return function (fa) { return F.reduceWithIndex(fa, b, f); }; };
  42. }
  43. export function foldMapWithIndex(F) {
  44. return function (M) {
  45. var foldMapWithIndexM = F.foldMapWithIndex(M);
  46. return function (f) { return function (fa) { return foldMapWithIndexM(fa, f); }; };
  47. };
  48. }
  49. export function reduceRightWithIndex(F) {
  50. return function (b, f) { return function (fa) { return F.reduceRightWithIndex(fa, b, f); }; };
  51. }
  52. export function alt(F) {
  53. return function (that) { return function (fa) { return F.alt(fa, that); }; };
  54. }
  55. export function filter(F) {
  56. return function (predicate) { return function (fa) { return F.filter(fa, predicate); }; };
  57. }
  58. export function filterMap(F) {
  59. return function (f) { return function (fa) { return F.filterMap(fa, f); }; };
  60. }
  61. export function partition(F) {
  62. return function (f) { return function (fa) { return F.partition(fa, f); }; };
  63. }
  64. export function partitionMap(F) {
  65. return function (f) { return function (fa) { return F.partitionMap(fa, f); }; };
  66. }
  67. export function filterWithIndex(F) {
  68. return function (predicate) { return function (fa) { return F.filterWithIndex(fa, predicate); }; };
  69. }
  70. export function filterMapWithIndex(F) {
  71. return function (f) { return function (fa) { return F.filterMapWithIndex(fa, f); }; };
  72. }
  73. export function partitionWithIndex(F) {
  74. return function (f) { return function (fa) { return F.partitionWithIndex(fa, f); }; };
  75. }
  76. export function partitionMapWithIndex(F) {
  77. return function (f) { return function (fa) { return F.partitionMapWithIndex(fa, f); }; };
  78. }
  79. export function promap(F) {
  80. return function (f, g) { return function (fbc) { return F.promap(fbc, f, g); }; };
  81. }
  82. export function compose(F) {
  83. return function (ea) { return function (ab) { return F.compose(ab, ea); }; };
  84. }
  85. var isFunctor = function (I) { return typeof I.map === 'function'; };
  86. var isContravariant = function (I) { return typeof I.contramap === 'function'; };
  87. var isFunctorWithIndex = function (I) { return typeof I.mapWithIndex === 'function'; };
  88. var isApply = function (I) { return typeof I.ap === 'function'; };
  89. var isChain = function (I) { return typeof I.chain === 'function'; };
  90. var isBifunctor = function (I) { return typeof I.bimap === 'function'; };
  91. var isExtend = function (I) { return typeof I.extend === 'function'; };
  92. var isFoldable = function (I) { return typeof I.reduce === 'function'; };
  93. var isFoldableWithIndex = function (I) { return typeof I.reduceWithIndex === 'function'; };
  94. var isAlt = function (I) { return typeof I.alt === 'function'; };
  95. var isCompactable = function (I) { return typeof I.compact === 'function'; };
  96. var isFilterable = function (I) { return typeof I.filter === 'function'; };
  97. var isFilterableWithIndex = function (I) {
  98. return typeof I.filterWithIndex === 'function';
  99. };
  100. var isProfunctor = function (I) { return typeof I.promap === 'function'; };
  101. var isSemigroupoid = function (I) { return typeof I.compose === 'function'; };
  102. var isMonadThrow = function (I) { return typeof I.throwError === 'function'; };
  103. /** @deprecated */
  104. export function pipeable(I) {
  105. var r = {};
  106. if (isFunctor(I)) {
  107. r.map = map(I);
  108. }
  109. if (isContravariant(I)) {
  110. r.contramap = contramap(I);
  111. }
  112. if (isFunctorWithIndex(I)) {
  113. r.mapWithIndex = mapWithIndex(I);
  114. }
  115. if (isApply(I)) {
  116. r.ap = ap(I);
  117. r.apFirst = apFirst_(I);
  118. r.apSecond = apSecond_(I);
  119. }
  120. if (isChain(I)) {
  121. r.chain = chain(I);
  122. r.chainFirst = chainFirst_(I);
  123. r.flatten = r.chain(identity);
  124. }
  125. if (isBifunctor(I)) {
  126. r.bimap = bimap(I);
  127. r.mapLeft = mapLeft(I);
  128. }
  129. if (isExtend(I)) {
  130. r.extend = extend(I);
  131. r.duplicate = r.extend(identity);
  132. }
  133. if (isFoldable(I)) {
  134. r.reduce = reduce(I);
  135. r.foldMap = foldMap(I);
  136. r.reduceRight = reduceRight(I);
  137. }
  138. if (isFoldableWithIndex(I)) {
  139. r.reduceWithIndex = reduceWithIndex(I);
  140. r.foldMapWithIndex = foldMapWithIndex(I);
  141. r.reduceRightWithIndex = reduceRightWithIndex(I);
  142. }
  143. if (isAlt(I)) {
  144. r.alt = alt(I);
  145. }
  146. if (isCompactable(I)) {
  147. r.compact = I.compact;
  148. r.separate = I.separate;
  149. }
  150. if (isFilterable(I)) {
  151. r.filter = filter(I);
  152. r.filterMap = filterMap(I);
  153. r.partition = partition(I);
  154. r.partitionMap = partitionMap(I);
  155. }
  156. if (isFilterableWithIndex(I)) {
  157. r.filterWithIndex = filterWithIndex(I);
  158. r.filterMapWithIndex = filterMapWithIndex(I);
  159. r.partitionWithIndex = partitionWithIndex(I);
  160. r.partitionMapWithIndex = partitionMapWithIndex(I);
  161. }
  162. if (isProfunctor(I)) {
  163. r.promap = promap(I);
  164. }
  165. if (isSemigroupoid(I)) {
  166. r.compose = compose(I);
  167. }
  168. if (isMonadThrow(I)) {
  169. var fromOption = function (onNone) { return function (ma) {
  170. return ma._tag === 'None' ? I.throwError(onNone()) : I.of(ma.value);
  171. }; };
  172. var fromEither = function (ma) {
  173. return ma._tag === 'Left' ? I.throwError(ma.left) : I.of(ma.right);
  174. };
  175. var fromPredicate = function (predicate, onFalse) {
  176. return function (a) {
  177. return predicate(a) ? I.of(a) : I.throwError(onFalse(a));
  178. };
  179. };
  180. var filterOrElse = function (predicate, onFalse) {
  181. return function (ma) {
  182. return I.chain(ma, function (a) { return (predicate(a) ? I.of(a) : I.throwError(onFalse(a))); });
  183. };
  184. };
  185. r.fromOption = fromOption;
  186. r.fromEither = fromEither;
  187. r.fromPredicate = fromPredicate;
  188. r.filterOrElse = filterOrElse;
  189. }
  190. return r;
  191. }
  192. /**
  193. * Use [`pipe`](https://gcanti.github.io/fp-ts/modules/function.ts.html#pipe) from `function` module instead.
  194. *
  195. * @since 2.0.0
  196. * @deprecated
  197. */
  198. export var pipe = pipeFromFunctionModule;