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

пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. import { fromEquals } from './Eq';
  2. import { pipe, SK } from './function';
  3. import { flap as flap_ } from './Functor';
  4. import * as _ from './internal';
  5. import * as O from './Option';
  6. import { separated } from './Separated';
  7. import { wiltDefault, witherDefault } from './Witherable';
  8. /**
  9. * @category conversions
  10. * @since 2.5.0
  11. */
  12. export var fromMap = function (m) { return new Map(m); };
  13. /**
  14. * @category conversions
  15. * @since 2.5.0
  16. */
  17. export function toMap(m) {
  18. return new Map(m);
  19. }
  20. /**
  21. * @category instances
  22. * @since 2.5.0
  23. */
  24. export function getShow(SK, SA) {
  25. return {
  26. show: function (m) {
  27. var entries = [];
  28. m.forEach(function (a, k) {
  29. entries.push("[".concat(SK.show(k), ", ").concat(SA.show(a), "]"));
  30. });
  31. return "new Map([".concat(entries.sort().join(', '), "])");
  32. }
  33. };
  34. }
  35. /**
  36. * Calculate the number of key/value pairs in a map
  37. *
  38. * @since 2.5.0
  39. */
  40. export var size = function (m) { return m.size; };
  41. /**
  42. * Test whether or not a map is empty
  43. *
  44. * @since 2.5.0
  45. */
  46. export var isEmpty = function (m) { return m.size === 0; };
  47. export function member(E) {
  48. var lookupE = lookup(E);
  49. return function (k, m) {
  50. if (m === undefined) {
  51. var memberE_1 = member(E);
  52. return function (m) { return memberE_1(k, m); };
  53. }
  54. return _.isSome(lookupE(k, m));
  55. };
  56. }
  57. export function elem(E) {
  58. return function (a, m) {
  59. if (m === undefined) {
  60. var elemE_1 = elem(E);
  61. return function (m) { return elemE_1(a, m); };
  62. }
  63. var values = m.values();
  64. var e;
  65. while (!(e = values.next()).done) {
  66. var v = e.value;
  67. if (E.equals(a, v)) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. };
  73. }
  74. /**
  75. * Get a sorted `ReadonlyArray` of the keys contained in a `ReadonlyMap`.
  76. *
  77. * @since 2.5.0
  78. */
  79. export var keys = function (O) {
  80. return function (m) {
  81. return Array.from(m.keys()).sort(O.compare);
  82. };
  83. };
  84. /**
  85. * Get a sorted `ReadonlyArray` of the values contained in a `ReadonlyMap`.
  86. *
  87. * @since 2.5.0
  88. */
  89. export var values = function (O) {
  90. return function (m) {
  91. return Array.from(m.values()).sort(O.compare);
  92. };
  93. };
  94. /**
  95. * @since 2.5.0
  96. */
  97. export function collect(O) {
  98. var keysO = keys(O);
  99. return function (f) {
  100. return function (m) {
  101. var out = [];
  102. var ks = keysO(m);
  103. for (var _i = 0, ks_1 = ks; _i < ks_1.length; _i++) {
  104. var key = ks_1[_i];
  105. out.push(f(key, m.get(key)));
  106. }
  107. return out;
  108. };
  109. };
  110. }
  111. /**
  112. * Get a sorted `ReadonlyArray` of the key/value pairs contained in a `ReadonlyMap`.
  113. *
  114. * @category conversions
  115. * @since 2.5.0
  116. */
  117. export var toReadonlyArray = function (O) {
  118. return collect(O)(function (k, a) { return [k, a]; });
  119. };
  120. export function toUnfoldable(ord, U) {
  121. var toReadonlyArrayO = toReadonlyArray(ord);
  122. return function (d) {
  123. var kas = toReadonlyArrayO(d);
  124. var len = kas.length;
  125. return U.unfold(0, function (b) { return (b < len ? _.some([kas[b], b + 1]) : _.none); });
  126. };
  127. }
  128. /**
  129. * Insert or replace a key/value pair in a `ReadonlyMap`.
  130. *
  131. * @since 2.10.0
  132. */
  133. export var upsertAt = function (E) {
  134. var lookupWithKeyE = lookupWithKey(E);
  135. return function (k, a) {
  136. var lookupWithKeyEk = lookupWithKeyE(k);
  137. return function (m) {
  138. var found = lookupWithKeyEk(m);
  139. if (_.isNone(found)) {
  140. var out = new Map(m);
  141. out.set(k, a);
  142. return out;
  143. }
  144. else if (found.value[1] !== a) {
  145. var out = new Map(m);
  146. out.set(found.value[0], a);
  147. return out;
  148. }
  149. return m;
  150. };
  151. };
  152. };
  153. /**
  154. * Delete a key and value from a map
  155. *
  156. * @since 2.5.0
  157. */
  158. export var deleteAt = function (E) {
  159. var lookupWithKeyE = lookupWithKey(E);
  160. return function (k) { return function (m) {
  161. var found = lookupWithKeyE(k, m);
  162. if (_.isSome(found)) {
  163. var r = new Map(m);
  164. r.delete(found.value[0]);
  165. return r;
  166. }
  167. return m;
  168. }; };
  169. };
  170. /**
  171. * @since 2.5.0
  172. */
  173. export var updateAt = function (E) {
  174. var modifyAtE = modifyAt(E);
  175. return function (k, a) { return modifyAtE(k, function () { return a; }); };
  176. };
  177. /**
  178. * @since 2.5.0
  179. */
  180. export var modifyAt = function (E) {
  181. var lookupWithKeyE = lookupWithKey(E);
  182. return function (k, f) { return function (m) {
  183. var found = lookupWithKeyE(k, m);
  184. if (_.isNone(found)) {
  185. return _.none;
  186. }
  187. var _a = found.value, fk = _a[0], fv = _a[1];
  188. var next = f(fv);
  189. if (next === fv) {
  190. return _.some(m);
  191. }
  192. var r = new Map(m);
  193. r.set(fk, next);
  194. return _.some(r);
  195. }; };
  196. };
  197. /**
  198. * Delete a key and value from a map, returning the value as well as the subsequent map
  199. *
  200. * @since 2.5.0
  201. */
  202. export function pop(E) {
  203. var lookupE = lookup(E);
  204. var deleteAtE = deleteAt(E);
  205. return function (k) {
  206. var deleteAtEk = deleteAtE(k);
  207. return function (m) {
  208. return pipe(lookupE(k, m), O.map(function (a) { return [a, deleteAtEk(m)]; }));
  209. };
  210. };
  211. }
  212. export function lookupWithKey(E) {
  213. return function (k, m) {
  214. if (m === undefined) {
  215. var lookupWithKeyE_1 = lookupWithKey(E);
  216. return function (m) { return lookupWithKeyE_1(k, m); };
  217. }
  218. var entries = m.entries();
  219. var e;
  220. while (!(e = entries.next()).done) {
  221. var _a = e.value, ka = _a[0], a = _a[1];
  222. if (E.equals(ka, k)) {
  223. return _.some([ka, a]);
  224. }
  225. }
  226. return _.none;
  227. };
  228. }
  229. export function lookup(E) {
  230. var lookupWithKeyE = lookupWithKey(E);
  231. return function (k, m) {
  232. if (m === undefined) {
  233. var lookupE_1 = lookup(E);
  234. return function (m) { return lookupE_1(k, m); };
  235. }
  236. return pipe(lookupWithKeyE(k, m), O.map(function (_a) {
  237. var _ = _a[0], a = _a[1];
  238. return a;
  239. }));
  240. };
  241. }
  242. export function isSubmap(SK, SA) {
  243. var lookupWithKeyS = lookupWithKey(SK);
  244. return function (me, that) {
  245. if (that === undefined) {
  246. var isSubmapSKSA_1 = isSubmap(SK, SA);
  247. return function (that) { return isSubmapSKSA_1(that, me); };
  248. }
  249. var entries = me.entries();
  250. var e;
  251. while (!(e = entries.next()).done) {
  252. var _a = e.value, k = _a[0], a = _a[1];
  253. var d2OptA = lookupWithKeyS(k, that);
  254. if (_.isNone(d2OptA) || !SK.equals(k, d2OptA.value[0]) || !SA.equals(a, d2OptA.value[1])) {
  255. return false;
  256. }
  257. }
  258. return true;
  259. };
  260. }
  261. /**
  262. * @since 2.5.0
  263. */
  264. export var empty =
  265. // the type annotation here is intended (otherwise it doesn't type-check)
  266. new Map();
  267. /**
  268. * @category instances
  269. * @since 2.5.0
  270. */
  271. export function getEq(SK, SA) {
  272. var isSubmapSKSA = isSubmap(SK, SA);
  273. return fromEquals(function (x, y) { return isSubmapSKSA(x, y) && isSubmapSKSA(y, x); });
  274. }
  275. /**
  276. * Gets `Monoid` instance for Maps given `Semigroup` instance for their values
  277. *
  278. * @category instances
  279. * @since 2.5.0
  280. */
  281. export function getMonoid(SK, SA) {
  282. var lookupWithKeyS = lookupWithKey(SK);
  283. return {
  284. concat: function (mx, my) {
  285. if (isEmpty(mx)) {
  286. return my;
  287. }
  288. if (isEmpty(my)) {
  289. return mx;
  290. }
  291. var r = new Map(mx);
  292. var entries = my.entries();
  293. var e;
  294. while (!(e = entries.next()).done) {
  295. var _a = e.value, k = _a[0], a = _a[1];
  296. var mxOptA = lookupWithKeyS(k, mx);
  297. if (_.isSome(mxOptA)) {
  298. r.set(mxOptA.value[0], SA.concat(mxOptA.value[1], a));
  299. }
  300. else {
  301. r.set(k, a);
  302. }
  303. }
  304. return r;
  305. },
  306. empty: empty
  307. };
  308. }
  309. /**
  310. * Create a map with one key/value pair
  311. *
  312. * @category constructors
  313. * @since 2.5.0
  314. */
  315. export var singleton = function (k, a) { return new Map([[k, a]]); };
  316. export function fromFoldable(E, M, F) {
  317. return function (fka) {
  318. var lookupWithKeyE = lookupWithKey(E);
  319. return F.reduce(fka, new Map(), function (b, _a) {
  320. var k = _a[0], a = _a[1];
  321. var bOpt = lookupWithKeyE(k, b);
  322. if (_.isSome(bOpt)) {
  323. b.set(bOpt.value[0], M.concat(bOpt.value[1], a));
  324. }
  325. else {
  326. b.set(k, a);
  327. }
  328. return b;
  329. });
  330. };
  331. }
  332. var _mapWithIndex = function (fa, f) {
  333. var m = new Map();
  334. var entries = fa.entries();
  335. var e;
  336. while (!(e = entries.next()).done) {
  337. var _a = e.value, key = _a[0], a = _a[1];
  338. m.set(key, f(key, a));
  339. }
  340. return m;
  341. };
  342. /**
  343. * @since 2.10.0
  344. */
  345. export var partitionMapWithIndex = function (f) {
  346. return function (fa) {
  347. var left = new Map();
  348. var right = new Map();
  349. var entries = fa.entries();
  350. var e;
  351. while (!(e = entries.next()).done) {
  352. var _a = e.value, k = _a[0], a = _a[1];
  353. var ei = f(k, a);
  354. if (_.isLeft(ei)) {
  355. left.set(k, ei.left);
  356. }
  357. else {
  358. right.set(k, ei.right);
  359. }
  360. }
  361. return separated(left, right);
  362. };
  363. };
  364. export function partitionWithIndex(predicateWithIndex) {
  365. return function (m) {
  366. var left = new Map();
  367. var right = new Map();
  368. var entries = m.entries();
  369. var e;
  370. while (!(e = entries.next()).done) {
  371. var _a = e.value, k = _a[0], a = _a[1];
  372. if (predicateWithIndex(k, a)) {
  373. right.set(k, a);
  374. }
  375. else {
  376. left.set(k, a);
  377. }
  378. }
  379. return separated(left, right);
  380. };
  381. }
  382. /**
  383. * @since 2.10.0
  384. */
  385. export var filterMapWithIndex = function (f) {
  386. return function (fa) {
  387. var m = new Map();
  388. var entries = fa.entries();
  389. var e;
  390. while (!(e = entries.next()).done) {
  391. var _a = e.value, k = _a[0], a = _a[1];
  392. var o = f(k, a);
  393. if (_.isSome(o)) {
  394. m.set(k, o.value);
  395. }
  396. }
  397. return m;
  398. };
  399. };
  400. export function filterWithIndex(predicateWithIndex) {
  401. return function (m) {
  402. var out = new Map();
  403. var entries = m.entries();
  404. var e;
  405. while (!(e = entries.next()).done) {
  406. var _a = e.value, k = _a[0], a = _a[1];
  407. if (predicateWithIndex(k, a)) {
  408. out.set(k, a);
  409. }
  410. }
  411. return out;
  412. };
  413. }
  414. var _map = function (fa, f) { return _mapWithIndex(fa, function (_, a) { return f(a); }); };
  415. var _filter = function (fa, p) {
  416. return _filterWithIndex(fa, function (_, a) { return p(a); });
  417. };
  418. var _filterMap = function (fa, f) { return _filterMapWithIndex(fa, function (_, a) { return f(a); }); };
  419. var _partition = function (fa, predicate) {
  420. return _partitionWithIndex(fa, function (_, a) { return predicate(a); });
  421. };
  422. var _partitionMap = function (fa, f) { return _partitionMapWithIndex(fa, function (_, a) { return f(a); }); };
  423. var _filterWithIndex = function (fa, p) { return pipe(fa, filterWithIndex(p)); };
  424. var _filterMapWithIndex = function (fa, f) {
  425. return pipe(fa, filterMapWithIndex(f));
  426. };
  427. var _partitionWithIndex = function (fa, p) { return pipe(fa, partitionWithIndex(p)); };
  428. var _partitionMapWithIndex = function (fa, f) {
  429. return pipe(fa, partitionMapWithIndex(f));
  430. };
  431. /**
  432. * @category filtering
  433. * @since 2.5.0
  434. */
  435. export var compact = function (fa) {
  436. var m = new Map();
  437. var entries = fa.entries();
  438. var e;
  439. while (!(e = entries.next()).done) {
  440. var _a = e.value, k = _a[0], oa = _a[1];
  441. if (_.isSome(oa)) {
  442. m.set(k, oa.value);
  443. }
  444. }
  445. return m;
  446. };
  447. /**
  448. * @category filtering
  449. * @since 2.5.0
  450. */
  451. export var filter = function (predicate) {
  452. return function (fa) {
  453. return _filter(fa, predicate);
  454. };
  455. };
  456. /**
  457. * @category filtering
  458. * @since 2.5.0
  459. */
  460. export var filterMap = function (f) { return function (fa) {
  461. return _filterMap(fa, f);
  462. }; };
  463. /**
  464. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  465. * use the type constructor `F` to represent some computational context.
  466. *
  467. * @category mapping
  468. * @since 2.5.0
  469. */
  470. export var map = function (f) { return function (fa) { return _map(fa, f); }; };
  471. /**
  472. * @category mapping
  473. * @since 2.7.1
  474. */
  475. export var mapWithIndex = function (f) { return function (fa) {
  476. return _mapWithIndex(fa, f);
  477. }; };
  478. /**
  479. * @category filtering
  480. * @since 2.5.0
  481. */
  482. export var partition = function (predicate) {
  483. return function (fa) {
  484. return _partition(fa, predicate);
  485. };
  486. };
  487. /**
  488. * @category filtering
  489. * @since 2.5.0
  490. */
  491. export var partitionMap = function (f) { return function (fa) { return _partitionMap(fa, f); }; };
  492. /**
  493. * @category filtering
  494. * @since 2.5.0
  495. */
  496. export var separate = function (fa) {
  497. var left = new Map();
  498. var right = new Map();
  499. var entries = fa.entries();
  500. var e;
  501. while (!(e = entries.next()).done) {
  502. var _a = e.value, k = _a[0], ei = _a[1];
  503. if (_.isLeft(ei)) {
  504. left.set(k, ei.left);
  505. }
  506. else {
  507. right.set(k, ei.right);
  508. }
  509. }
  510. return separated(left, right);
  511. };
  512. /**
  513. * @category type lambdas
  514. * @since 2.5.0
  515. */
  516. export var URI = 'ReadonlyMap';
  517. /**
  518. * @category instances
  519. * @since 2.11.0
  520. */
  521. export var getUnionSemigroup = function (E, S) {
  522. var unionES = union(E, S);
  523. return {
  524. concat: function (first, second) { return unionES(second)(first); }
  525. };
  526. };
  527. /**
  528. * @category instances
  529. * @since 2.11.0
  530. */
  531. export var getUnionMonoid = function (E, S) { return ({
  532. concat: getUnionSemigroup(E, S).concat,
  533. empty: empty
  534. }); };
  535. /**
  536. * @category instances
  537. * @since 2.11.0
  538. */
  539. export var getIntersectionSemigroup = function (E, S) {
  540. var intersectionES = intersection(E, S);
  541. return {
  542. concat: function (first, second) { return intersectionES(second)(first); }
  543. };
  544. };
  545. /**
  546. * @category instances
  547. * @since 2.11.0
  548. */
  549. export var getDifferenceMagma = function (E) {
  550. return function () {
  551. var differenceE = difference(E);
  552. return {
  553. concat: function (first, second) { return differenceE(second)(first); }
  554. };
  555. };
  556. };
  557. /**
  558. * @category filtering
  559. * @since 2.5.0
  560. */
  561. export function getFilterableWithIndex() {
  562. return {
  563. URI: URI,
  564. _E: undefined,
  565. map: _map,
  566. mapWithIndex: _mapWithIndex,
  567. compact: compact,
  568. separate: separate,
  569. filter: _filter,
  570. filterMap: _filterMap,
  571. partition: _partition,
  572. partitionMap: _partitionMap,
  573. partitionMapWithIndex: _partitionMapWithIndex,
  574. partitionWithIndex: _partitionWithIndex,
  575. filterMapWithIndex: _filterMapWithIndex,
  576. filterWithIndex: _filterWithIndex
  577. };
  578. }
  579. /**
  580. * @category instances
  581. * @since 2.7.0
  582. */
  583. export var Functor = {
  584. URI: URI,
  585. map: _map
  586. };
  587. /**
  588. * @category mapping
  589. * @since 2.10.0
  590. */
  591. export var flap = /*#__PURE__*/ flap_(Functor);
  592. /**
  593. * @category instances
  594. * @since 2.10.0
  595. */
  596. export var getFunctorWithIndex = function () { return ({
  597. URI: URI,
  598. _E: undefined,
  599. map: _map,
  600. mapWithIndex: _mapWithIndex
  601. }); };
  602. /**
  603. * @category instances
  604. * @since 2.7.0
  605. */
  606. export var Compactable = {
  607. URI: URI,
  608. compact: compact,
  609. separate: separate
  610. };
  611. /**
  612. * @category instances
  613. * @since 2.7.0
  614. */
  615. export var Filterable = {
  616. URI: URI,
  617. map: _map,
  618. compact: compact,
  619. separate: separate,
  620. filter: _filter,
  621. filterMap: _filterMap,
  622. partition: _partition,
  623. partitionMap: _partitionMap
  624. };
  625. /**
  626. * @category folding
  627. * @since 2.11.0
  628. */
  629. export var reduce = function (O) {
  630. var reduceWithIndexO = reduceWithIndex(O);
  631. return function (b, f) { return reduceWithIndexO(b, function (_, b, a) { return f(b, a); }); };
  632. };
  633. /**
  634. * @category folding
  635. * @since 2.11.0
  636. */
  637. export var foldMap = function (O) {
  638. var foldMapWithIndexO = foldMapWithIndex(O);
  639. return function (M) {
  640. var foldMapWithIndexOM = foldMapWithIndexO(M);
  641. return function (f) { return foldMapWithIndexOM(function (_, a) { return f(a); }); };
  642. };
  643. };
  644. /**
  645. * @category folding
  646. * @since 2.11.0
  647. */
  648. export var reduceRight = function (O) {
  649. var reduceRightWithIndexO = reduceRightWithIndex(O);
  650. return function (b, f) { return reduceRightWithIndexO(b, function (_, b, a) { return f(b, a); }); };
  651. };
  652. /**
  653. * @category folding
  654. * @since 2.10.0
  655. */
  656. export var getFoldable = function (O) {
  657. var reduceO = reduce(O);
  658. var foldMapO = foldMap(O);
  659. var reduceRightO = reduceRight(O);
  660. return {
  661. URI: URI,
  662. _E: undefined,
  663. reduce: function (fa, b, f) { return pipe(fa, reduceO(b, f)); },
  664. foldMap: function (M) {
  665. var foldMapOM = foldMapO(M);
  666. return function (fa, f) { return pipe(fa, foldMapOM(f)); };
  667. },
  668. reduceRight: function (fa, b, f) { return pipe(fa, reduceRightO(b, f)); }
  669. };
  670. };
  671. /**
  672. * @category folding
  673. * @since 2.11.0
  674. */
  675. export var reduceWithIndex = function (O) {
  676. var keysO = keys(O);
  677. return function (b, f) { return function (m) {
  678. var out = b;
  679. for (var _i = 0, _a = keysO(m); _i < _a.length; _i++) {
  680. var k = _a[_i];
  681. out = f(k, out, m.get(k));
  682. }
  683. return out;
  684. }; };
  685. };
  686. /**
  687. * @category folding
  688. * @since 2.11.0
  689. */
  690. export var foldMapWithIndex = function (O) {
  691. var keysO = keys(O);
  692. return function (M) { return function (f) { return function (m) {
  693. var out = M.empty;
  694. for (var _i = 0, _a = keysO(m); _i < _a.length; _i++) {
  695. var k = _a[_i];
  696. out = M.concat(out, f(k, m.get(k)));
  697. }
  698. return out;
  699. }; }; };
  700. };
  701. /**
  702. * @category folding
  703. * @since 2.11.0
  704. */
  705. export var reduceRightWithIndex = function (O) {
  706. var keysO = keys(O);
  707. return function (b, f) { return function (m) {
  708. var out = b;
  709. var ks = keysO(m);
  710. var len = ks.length;
  711. for (var i = len - 1; i >= 0; i--) {
  712. var k = ks[i];
  713. out = f(k, m.get(k), out);
  714. }
  715. return out;
  716. }; };
  717. };
  718. /**
  719. * @category folding
  720. * @since 2.10.0
  721. */
  722. export var getFoldableWithIndex = function (O) {
  723. var F = getFoldable(O);
  724. var reduceWithIndexO = reduceWithIndex(O);
  725. var foldMapWithIndexO = foldMapWithIndex(O);
  726. var reduceRightWithIndexO = reduceRightWithIndex(O);
  727. return {
  728. URI: URI,
  729. _E: undefined,
  730. reduce: F.reduce,
  731. foldMap: F.foldMap,
  732. reduceRight: F.reduceRight,
  733. reduceWithIndex: function (fa, b, f) { return pipe(fa, reduceWithIndexO(b, f)); },
  734. foldMapWithIndex: function (M) {
  735. var foldMapWithIndexOM = foldMapWithIndexO(M);
  736. return function (fa, f) { return pipe(fa, foldMapWithIndexOM(f)); };
  737. },
  738. reduceRightWithIndex: function (fa, b, f) { return pipe(fa, reduceRightWithIndexO(b, f)); }
  739. };
  740. };
  741. /**
  742. * @category traversing
  743. * @since 2.10.0
  744. */
  745. export var getTraversable = function (O) {
  746. var TWI = getTraversableWithIndex(O);
  747. var F = getFoldable(O);
  748. return {
  749. URI: URI,
  750. _E: undefined,
  751. map: _map,
  752. reduce: F.reduce,
  753. foldMap: F.foldMap,
  754. reduceRight: F.reduceRight,
  755. traverse: TWI.traverse,
  756. sequence: TWI.sequence
  757. };
  758. };
  759. /**
  760. * @category traversing
  761. * @since 2.10.0
  762. */
  763. export var getTraversableWithIndex = function (O) {
  764. var FWI = getFoldableWithIndex(O);
  765. var keysO = keys(O);
  766. var traverseWithIndex = function (F) {
  767. return function (ta, f) {
  768. var fm = F.of(new Map());
  769. var ks = keysO(ta);
  770. var len = ks.length;
  771. var _loop_1 = function (i) {
  772. var key = ks[i];
  773. var a = ta.get(key);
  774. fm = F.ap(F.map(fm, function (m) { return function (b) { return m.set(key, b); }; }), f(key, a));
  775. };
  776. for (var i = 0; i < len; i++) {
  777. _loop_1(i);
  778. }
  779. return fm;
  780. };
  781. };
  782. var traverse = function (F) {
  783. var traverseWithIndexF = traverseWithIndex(F);
  784. return function (ta, f) { return traverseWithIndexF(ta, function (_, a) { return f(a); }); };
  785. };
  786. var sequence = function (F) {
  787. var traverseWithIndexF = traverseWithIndex(F);
  788. return function (ta) { return traverseWithIndexF(ta, SK); };
  789. };
  790. return {
  791. URI: URI,
  792. _E: undefined,
  793. map: _map,
  794. mapWithIndex: _mapWithIndex,
  795. reduce: FWI.reduce,
  796. foldMap: FWI.foldMap,
  797. reduceRight: FWI.reduceRight,
  798. reduceWithIndex: FWI.reduceWithIndex,
  799. foldMapWithIndex: FWI.foldMapWithIndex,
  800. reduceRightWithIndex: FWI.reduceRightWithIndex,
  801. traverse: traverse,
  802. sequence: sequence,
  803. traverseWithIndex: traverseWithIndex
  804. };
  805. };
  806. /**
  807. * @category filtering
  808. * @since 2.5.0
  809. */
  810. export function getWitherable(O) {
  811. var TWI = getTraversableWithIndex(O);
  812. return {
  813. URI: URI,
  814. _E: undefined,
  815. map: _map,
  816. compact: compact,
  817. separate: separate,
  818. filter: _filter,
  819. filterMap: _filterMap,
  820. partition: _partition,
  821. partitionMap: _partitionMap,
  822. reduce: TWI.reduce,
  823. foldMap: TWI.foldMap,
  824. reduceRight: TWI.reduceRight,
  825. traverse: TWI.traverse,
  826. sequence: TWI.sequence,
  827. mapWithIndex: _mapWithIndex,
  828. reduceWithIndex: TWI.reduceWithIndex,
  829. foldMapWithIndex: TWI.foldMapWithIndex,
  830. reduceRightWithIndex: TWI.reduceRightWithIndex,
  831. traverseWithIndex: TWI.traverseWithIndex,
  832. wilt: wiltDefault(TWI, Compactable),
  833. wither: witherDefault(TWI, Compactable)
  834. };
  835. }
  836. // -------------------------------------------------------------------------------------
  837. // utils
  838. // -------------------------------------------------------------------------------------
  839. /**
  840. * @since 2.11.0
  841. */
  842. export var union = function (E, M) {
  843. var lookupE = lookup(E);
  844. return function (second) { return function (first) {
  845. if (isEmpty(first)) {
  846. return second;
  847. }
  848. if (isEmpty(second)) {
  849. return first;
  850. }
  851. var out = new Map();
  852. var firstEntries = first.entries();
  853. var e;
  854. while (!(e = firstEntries.next()).done) {
  855. var _a = e.value, k = _a[0], a = _a[1];
  856. var oka = lookupE(k)(second);
  857. if (_.isSome(oka)) {
  858. out.set(k, M.concat(a, oka.value));
  859. }
  860. else {
  861. out.set(k, a);
  862. }
  863. }
  864. var secondEntries = second.entries();
  865. while (!(e = secondEntries.next()).done) {
  866. var _b = e.value, k = _b[0], a = _b[1];
  867. var oka = lookupE(k)(out);
  868. if (_.isNone(oka)) {
  869. out.set(k, a);
  870. }
  871. }
  872. return out;
  873. }; };
  874. };
  875. /**
  876. * @since 2.11.0
  877. */
  878. export var intersection = function (E, M) {
  879. var lookupE = lookup(E);
  880. return function (second) { return function (first) {
  881. if (isEmpty(first) || isEmpty(second)) {
  882. return empty;
  883. }
  884. var out = new Map();
  885. var entries = first.entries();
  886. var e;
  887. while (!(e = entries.next()).done) {
  888. var _a = e.value, k = _a[0], a = _a[1];
  889. var oka = lookupE(k)(second);
  890. if (_.isSome(oka)) {
  891. out.set(k, M.concat(a, oka.value));
  892. }
  893. }
  894. return out;
  895. }; };
  896. };
  897. /**
  898. * @since 2.11.0
  899. */
  900. export var difference = function (E) {
  901. var memberE = member(E);
  902. return function (second) {
  903. return function (first) {
  904. if (isEmpty(first)) {
  905. return second;
  906. }
  907. if (isEmpty(second)) {
  908. return first;
  909. }
  910. var out = new Map();
  911. var firstEntries = first.entries();
  912. var e;
  913. while (!(e = firstEntries.next()).done) {
  914. var _a = e.value, k = _a[0], a = _a[1];
  915. if (!memberE(k)(second)) {
  916. out.set(k, a);
  917. }
  918. }
  919. var secondEntries = second.entries();
  920. while (!(e = secondEntries.next()).done) {
  921. var _b = e.value, k = _b[0], a = _b[1];
  922. if (!memberE(k)(first)) {
  923. out.set(k, a);
  924. }
  925. }
  926. return out;
  927. };
  928. };
  929. };
  930. // -------------------------------------------------------------------------------------
  931. // deprecated
  932. // -------------------------------------------------------------------------------------
  933. /**
  934. * Use [`upsertAt`](#upsertat) instead.
  935. *
  936. @category zone of death
  937. * @since 2.5.0
  938. * @deprecated
  939. */
  940. export var insertAt = upsertAt;
  941. /**
  942. * This instance is deprecated, use small, specific instances instead.
  943. * For example if a function needs a `Functor` instance, pass `RM.Functor` instead of `RM.readonlyMap`
  944. * (where `RM` is from `import RM from 'fp-ts/ReadonlyMap'`)
  945. *
  946. * @category zone of death
  947. * @since 2.5.0
  948. * @deprecated
  949. */
  950. export var readonlyMap = {
  951. URI: URI,
  952. map: _map,
  953. compact: compact,
  954. separate: separate,
  955. filter: _filter,
  956. filterMap: _filterMap,
  957. partition: _partition,
  958. partitionMap: _partitionMap
  959. };