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

525 lines
14 KiB

  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.tree = exports.exists = exports.elem = exports.apS = exports.bind = exports.let = exports.bindTo = exports.Do = exports.Comonad = exports.Traversable = exports.Foldable = exports.chainFirst = exports.Monad = exports.Chain = exports.Applicative = exports.apSecond = exports.apFirst = exports.Apply = exports.Pointed = exports.flap = exports.Functor = exports.URI = exports.of = exports.sequence = exports.traverse = exports.extract = exports.reduceRight = exports.foldMap = exports.reduce = exports.map = exports.flatten = exports.duplicate = exports.extend = exports.chain = exports.ap = exports.fold = exports.unfoldForestM = exports.unfoldTreeM = exports.unfoldForest = exports.unfoldTree = exports.drawTree = exports.drawForest = exports.getEq = exports.getShow = exports.make = void 0;
  27. var Apply_1 = require("./Apply");
  28. var A = __importStar(require("./Array"));
  29. var Chain_1 = require("./Chain");
  30. var Eq_1 = require("./Eq");
  31. var function_1 = require("./function");
  32. var Functor_1 = require("./Functor");
  33. var _ = __importStar(require("./internal"));
  34. /**
  35. * @category constructors
  36. * @since 2.0.0
  37. */
  38. function make(value, forest) {
  39. if (forest === void 0) { forest = []; }
  40. return {
  41. value: value,
  42. forest: forest
  43. };
  44. }
  45. exports.make = make;
  46. /**
  47. * @category instances
  48. * @since 2.0.0
  49. */
  50. function getShow(S) {
  51. var show = function (t) {
  52. return A.isEmpty(t.forest)
  53. ? "make(".concat(S.show(t.value), ")")
  54. : "make(".concat(S.show(t.value), ", [").concat(t.forest.map(show).join(', '), "])");
  55. };
  56. return {
  57. show: show
  58. };
  59. }
  60. exports.getShow = getShow;
  61. /**
  62. * @category instances
  63. * @since 2.0.0
  64. */
  65. function getEq(E) {
  66. // eslint-disable-next-line prefer-const
  67. var SA;
  68. var R = (0, Eq_1.fromEquals)(function (x, y) { return E.equals(x.value, y.value) && SA.equals(x.forest, y.forest); });
  69. SA = A.getEq(R);
  70. return R;
  71. }
  72. exports.getEq = getEq;
  73. var draw = function (indentation, forest) {
  74. var r = '';
  75. var len = forest.length;
  76. var tree;
  77. for (var i = 0; i < len; i++) {
  78. tree = forest[i];
  79. var isLast = i === len - 1;
  80. r += indentation + (isLast ? '└' : '├') + '─ ' + tree.value;
  81. r += draw(indentation + (len > 1 && !isLast ? '│ ' : ' '), tree.forest);
  82. }
  83. return r;
  84. };
  85. /**
  86. * Neat 2-dimensional drawing of a forest
  87. *
  88. * @since 2.0.0
  89. */
  90. function drawForest(forest) {
  91. return draw('\n', forest);
  92. }
  93. exports.drawForest = drawForest;
  94. /**
  95. * Neat 2-dimensional drawing of a tree
  96. *
  97. * @example
  98. * import { make, drawTree } from 'fp-ts/Tree'
  99. *
  100. * const fa = make('a', [
  101. * make('b'),
  102. * make('c'),
  103. * make('d', [make('e'), make('f')])
  104. * ])
  105. *
  106. * assert.strictEqual(drawTree(fa), `a
  107. * ├─ b
  108. * ├─ c
  109. * └─ d
  110. * ├─ e
  111. * └─ f`)
  112. *
  113. *
  114. * @since 2.0.0
  115. */
  116. function drawTree(tree) {
  117. return tree.value + drawForest(tree.forest);
  118. }
  119. exports.drawTree = drawTree;
  120. /**
  121. * Build a (possibly infinite) tree from a seed value in breadth-first order.
  122. *
  123. * @category constructors
  124. * @since 2.0.0
  125. */
  126. function unfoldTree(b, f) {
  127. var _a = f(b), a = _a[0], bs = _a[1];
  128. return { value: a, forest: unfoldForest(bs, f) };
  129. }
  130. exports.unfoldTree = unfoldTree;
  131. /**
  132. * Build a (possibly infinite) forest from a list of seed values in breadth-first order.
  133. *
  134. * @category constructors
  135. * @since 2.0.0
  136. */
  137. function unfoldForest(bs, f) {
  138. return bs.map(function (b) { return unfoldTree(b, f); });
  139. }
  140. exports.unfoldForest = unfoldForest;
  141. function unfoldTreeM(M) {
  142. var unfoldForestMM = unfoldForestM(M);
  143. return function (b, f) { return M.chain(f(b), function (_a) {
  144. var a = _a[0], bs = _a[1];
  145. return M.map(unfoldForestMM(bs, f), function (ts) { return ({ value: a, forest: ts }); });
  146. }); };
  147. }
  148. exports.unfoldTreeM = unfoldTreeM;
  149. function unfoldForestM(M) {
  150. var traverseM = A.traverse(M);
  151. return function (bs, f) {
  152. return (0, function_1.pipe)(bs, traverseM(function (b) { return unfoldTreeM(M)(b, f); }));
  153. };
  154. }
  155. exports.unfoldForestM = unfoldForestM;
  156. /**
  157. * Fold a tree into a "summary" value in depth-first order.
  158. *
  159. * For each node in the tree, apply `f` to the `value` and the result of applying `f` to each `forest`.
  160. *
  161. * This is also known as the catamorphism on trees.
  162. *
  163. * @example
  164. * import { fold, make } from 'fp-ts/Tree'
  165. * import { concatAll } from 'fp-ts/Monoid'
  166. * import { MonoidSum } from 'fp-ts/number'
  167. *
  168. * const t = make(1, [make(2), make(3)])
  169. *
  170. * const sum = concatAll(MonoidSum)
  171. *
  172. * // Sum the values in a tree:
  173. * assert.deepStrictEqual(fold((a: number, bs: Array<number>) => a + sum(bs))(t), 6)
  174. *
  175. * // Find the maximum value in the tree:
  176. * assert.deepStrictEqual(fold((a: number, bs: Array<number>) => bs.reduce((b, acc) => Math.max(b, acc), a))(t), 3)
  177. *
  178. * // Count the number of leaves in the tree:
  179. * assert.deepStrictEqual(fold((_: number, bs: Array<number>) => (bs.length === 0 ? 1 : sum(bs)))(t), 2)
  180. *
  181. * @category folding
  182. * @since 2.6.0
  183. */
  184. function fold(f) {
  185. var go = function (tree) { return f(tree.value, tree.forest.map(go)); };
  186. return go;
  187. }
  188. exports.fold = fold;
  189. /* istanbul ignore next */
  190. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
  191. var _ap = function (fab, fa) {
  192. return (0, function_1.pipe)(fab, (0, exports.chain)(function (f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); }));
  193. };
  194. /* istanbul ignore next */
  195. var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
  196. /* istanbul ignore next */
  197. var _reduce = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduce)(b, f)); };
  198. /* istanbul ignore next */
  199. var _foldMap = function (M) {
  200. var foldMapM = (0, exports.foldMap)(M);
  201. return function (fa, f) { return (0, function_1.pipe)(fa, foldMapM(f)); };
  202. };
  203. /* istanbul ignore next */
  204. var _reduceRight = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduceRight)(b, f)); };
  205. /* istanbul ignore next */
  206. var _extend = function (wa, f) { return (0, function_1.pipe)(wa, (0, exports.extend)(f)); };
  207. /* istanbul ignore next */
  208. var _traverse = function (F) {
  209. var traverseF = (0, exports.traverse)(F);
  210. return function (ta, f) { return (0, function_1.pipe)(ta, traverseF(f)); };
  211. };
  212. /**
  213. * @since 2.0.0
  214. */
  215. var ap = function (fa) { return function (fab) { return _ap(fab, fa); }; };
  216. exports.ap = ap;
  217. /**
  218. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  219. *
  220. * @category Monad
  221. * @since 2.0.0
  222. */
  223. var chain = function (f) {
  224. return function (ma) {
  225. var _a = f(ma.value), value = _a.value, forest = _a.forest;
  226. var concat = A.getMonoid().concat;
  227. return {
  228. value: value,
  229. forest: concat(forest, ma.forest.map((0, exports.chain)(f)))
  230. };
  231. };
  232. };
  233. exports.chain = chain;
  234. /**
  235. * @since 2.0.0
  236. */
  237. var extend = function (f) { return function (wa) { return ({
  238. value: f(wa),
  239. forest: wa.forest.map((0, exports.extend)(f))
  240. }); }; };
  241. exports.extend = extend;
  242. /**
  243. * @since 2.0.0
  244. */
  245. exports.duplicate = (0, exports.extend)(function_1.identity);
  246. /**
  247. * @category sequencing
  248. * @since 2.0.0
  249. */
  250. exports.flatten = (0, exports.chain)(function_1.identity);
  251. /**
  252. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  253. * use the type constructor `F` to represent some computational context.
  254. *
  255. * @category mapping
  256. * @since 2.0.0
  257. */
  258. var map = function (f) { return function (fa) { return ({
  259. value: f(fa.value),
  260. forest: fa.forest.map((0, exports.map)(f))
  261. }); }; };
  262. exports.map = map;
  263. /**
  264. * @category folding
  265. * @since 2.0.0
  266. */
  267. var reduce = function (b, f) {
  268. return function (fa) {
  269. var r = f(b, fa.value);
  270. var len = fa.forest.length;
  271. for (var i = 0; i < len; i++) {
  272. r = (0, function_1.pipe)(fa.forest[i], (0, exports.reduce)(r, f));
  273. }
  274. return r;
  275. };
  276. };
  277. exports.reduce = reduce;
  278. /**
  279. * @category folding
  280. * @since 2.0.0
  281. */
  282. var foldMap = function (M) { return function (f) {
  283. return (0, exports.reduce)(M.empty, function (acc, a) { return M.concat(acc, f(a)); });
  284. }; };
  285. exports.foldMap = foldMap;
  286. /**
  287. * @category folding
  288. * @since 2.0.0
  289. */
  290. var reduceRight = function (b, f) {
  291. return function (fa) {
  292. var r = b;
  293. var len = fa.forest.length;
  294. for (var i = len - 1; i >= 0; i--) {
  295. r = (0, function_1.pipe)(fa.forest[i], (0, exports.reduceRight)(r, f));
  296. }
  297. return f(fa.value, r);
  298. };
  299. };
  300. exports.reduceRight = reduceRight;
  301. /**
  302. * @category Extract
  303. * @since 2.6.2
  304. */
  305. var extract = function (wa) { return wa.value; };
  306. exports.extract = extract;
  307. /**
  308. * @category traversing
  309. * @since 2.6.3
  310. */
  311. var traverse = function (F) {
  312. var traverseF = A.traverse(F);
  313. var out = function (f) {
  314. return function (ta) {
  315. return F.ap(F.map(f(ta.value), function (value) { return function (forest) { return ({
  316. value: value,
  317. forest: forest
  318. }); }; }), (0, function_1.pipe)(ta.forest, traverseF(out(f))));
  319. };
  320. };
  321. return out;
  322. };
  323. exports.traverse = traverse;
  324. /**
  325. * @category traversing
  326. * @since 2.6.3
  327. */
  328. var sequence = function (F) { return (0, exports.traverse)(F)(function_1.identity); };
  329. exports.sequence = sequence;
  330. /**
  331. * @category constructors
  332. * @since 2.7.0
  333. */
  334. var of = function (a) { return make(a); };
  335. exports.of = of;
  336. /**
  337. * @category type lambdas
  338. * @since 2.0.0
  339. */
  340. exports.URI = 'Tree';
  341. /**
  342. * @category instances
  343. * @since 2.7.0
  344. */
  345. exports.Functor = {
  346. URI: exports.URI,
  347. map: _map
  348. };
  349. /**
  350. * @category mapping
  351. * @since 2.10.0
  352. */
  353. exports.flap = (0, Functor_1.flap)(exports.Functor);
  354. /**
  355. * @category instances
  356. * @since 2.10.0
  357. */
  358. exports.Pointed = {
  359. URI: exports.URI,
  360. of: exports.of
  361. };
  362. /**
  363. * @category instances
  364. * @since 2.10.0
  365. */
  366. exports.Apply = {
  367. URI: exports.URI,
  368. map: _map,
  369. ap: _ap
  370. };
  371. /**
  372. * Combine two effectful actions, keeping only the result of the first.
  373. *
  374. * @since 2.0.0
  375. */
  376. exports.apFirst = (0, Apply_1.apFirst)(exports.Apply);
  377. /**
  378. * Combine two effectful actions, keeping only the result of the second.
  379. *
  380. * @since 2.0.0
  381. */
  382. exports.apSecond = (0, Apply_1.apSecond)(exports.Apply);
  383. /**
  384. * @category instances
  385. * @since 2.7.0
  386. */
  387. exports.Applicative = {
  388. URI: exports.URI,
  389. map: _map,
  390. ap: _ap,
  391. of: exports.of
  392. };
  393. /**
  394. * @category instances
  395. * @since 2.10.0
  396. */
  397. exports.Chain = {
  398. URI: exports.URI,
  399. map: _map,
  400. ap: _ap,
  401. chain: _chain
  402. };
  403. /**
  404. * @category instances
  405. * @since 2.7.0
  406. */
  407. exports.Monad = {
  408. URI: exports.URI,
  409. map: _map,
  410. ap: _ap,
  411. of: exports.of,
  412. chain: _chain
  413. };
  414. /**
  415. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  416. * keeping only the result of the first.
  417. *
  418. * @since 2.0.0
  419. */
  420. exports.chainFirst = (0, Chain_1.chainFirst)(exports.Chain);
  421. /**
  422. * @category instances
  423. * @since 2.7.0
  424. */
  425. exports.Foldable = {
  426. URI: exports.URI,
  427. reduce: _reduce,
  428. foldMap: _foldMap,
  429. reduceRight: _reduceRight
  430. };
  431. /**
  432. * @category instances
  433. * @since 2.7.0
  434. */
  435. exports.Traversable = {
  436. URI: exports.URI,
  437. map: _map,
  438. reduce: _reduce,
  439. foldMap: _foldMap,
  440. reduceRight: _reduceRight,
  441. traverse: _traverse,
  442. sequence: exports.sequence
  443. };
  444. /**
  445. * @category instances
  446. * @since 2.7.0
  447. */
  448. exports.Comonad = {
  449. URI: exports.URI,
  450. map: _map,
  451. extend: _extend,
  452. extract: exports.extract
  453. };
  454. // -------------------------------------------------------------------------------------
  455. // do notation
  456. // -------------------------------------------------------------------------------------
  457. /**
  458. * @category do notation
  459. * @since 2.9.0
  460. */
  461. exports.Do = (0, exports.of)(_.emptyRecord);
  462. /**
  463. * @category do notation
  464. * @since 2.8.0
  465. */
  466. exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
  467. var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
  468. exports.let = let_;
  469. /**
  470. * @category do notation
  471. * @since 2.8.0
  472. */
  473. exports.bind = (0, Chain_1.bind)(exports.Chain);
  474. /**
  475. * @category do notation
  476. * @since 2.8.0
  477. */
  478. exports.apS = (0, Apply_1.apS)(exports.Apply);
  479. // -------------------------------------------------------------------------------------
  480. // utils
  481. // -------------------------------------------------------------------------------------
  482. /**
  483. * @since 2.0.0
  484. */
  485. function elem(E) {
  486. var go = function (a, fa) { return E.equals(a, fa.value) || fa.forest.some(function (tree) { return go(a, tree); }); };
  487. return go;
  488. }
  489. exports.elem = elem;
  490. /**
  491. * @since 2.11.0
  492. */
  493. var exists = function (predicate) {
  494. return function (ma) {
  495. return predicate(ma.value) || ma.forest.some((0, exports.exists)(predicate));
  496. };
  497. };
  498. exports.exists = exists;
  499. // -------------------------------------------------------------------------------------
  500. // deprecated
  501. // -------------------------------------------------------------------------------------
  502. /**
  503. * This instance is deprecated, use small, specific instances instead.
  504. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.tree`
  505. * (where `T` is from `import T from 'fp-ts/Tree'`)
  506. *
  507. * @category zone of death
  508. * @since 2.0.0
  509. * @deprecated
  510. */
  511. exports.tree = {
  512. URI: exports.URI,
  513. map: _map,
  514. of: exports.of,
  515. ap: _ap,
  516. chain: _chain,
  517. reduce: _reduce,
  518. foldMap: _foldMap,
  519. reduceRight: _reduceRight,
  520. traverse: _traverse,
  521. sequence: exports.sequence,
  522. extract: exports.extract,
  523. extend: _extend
  524. };