版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

1227 líneas
33 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. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  26. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  27. if (ar || !(i in from)) {
  28. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  29. ar[i] = from[i];
  30. }
  31. }
  32. return to.concat(ar || Array.prototype.slice.call(from));
  33. };
  34. Object.defineProperty(exports, "__esModule", { value: true });
  35. exports.mapWithIndex = exports.map = exports.flatten = exports.duplicate = exports.extend = exports.chain = exports.ap = exports.alt = exports.altW = exports.chunksOf = exports.splitAt = exports.chop = exports.chainWithIndex = exports.foldMap = exports.foldMapWithIndex = exports.intersperse = exports.prependAll = exports.unzip = exports.zip = exports.zipWith = exports.of = exports.copy = exports.modifyAt = exports.updateAt = exports.insertAt = exports.sort = exports.groupBy = exports.group = exports.reverse = exports.concat = exports.concatW = exports.unappend = exports.unprepend = exports.range = exports.replicate = exports.makeBy = exports.fromArray = exports.fromReadonlyNonEmptyArray = exports.rotate = exports.union = exports.sortBy = exports.uniq = exports.unsafeUpdateAt = exports.unsafeInsertAt = exports.append = exports.appendW = exports.prepend = exports.prependW = exports.isOutOfBound = exports.isNonEmpty = void 0;
  36. exports.groupSort = exports.intercalate = exports.updateLast = exports.modifyLast = exports.updateHead = exports.modifyHead = exports.matchRight = exports.matchLeft = exports.concatAll = exports.max = exports.min = exports.init = exports.last = exports.tail = exports.head = exports.apS = exports.bind = exports.let = exports.bindTo = exports.Do = exports.Comonad = exports.Alt = exports.TraversableWithIndex = exports.Traversable = exports.FoldableWithIndex = exports.Foldable = exports.Monad = exports.chainFirst = exports.Chain = exports.Applicative = exports.apSecond = exports.apFirst = exports.Apply = exports.FunctorWithIndex = exports.Pointed = exports.flap = exports.Functor = exports.getUnionSemigroup = exports.getEq = exports.getSemigroup = exports.getShow = exports.URI = exports.extract = exports.traverseWithIndex = exports.sequence = exports.traverse = exports.reduceRightWithIndex = exports.reduceRight = exports.reduceWithIndex = exports.reduce = void 0;
  37. exports.nonEmptyArray = exports.fold = exports.prependToAll = exports.snoc = exports.cons = exports.unsnoc = exports.uncons = exports.filterWithIndex = exports.filter = void 0;
  38. var Apply_1 = require("./Apply");
  39. var Chain_1 = require("./Chain");
  40. var function_1 = require("./function");
  41. var Functor_1 = require("./Functor");
  42. var _ = __importStar(require("./internal"));
  43. var Ord_1 = require("./Ord");
  44. var RNEA = __importStar(require("./ReadonlyNonEmptyArray"));
  45. // -------------------------------------------------------------------------------------
  46. // internal
  47. // -------------------------------------------------------------------------------------
  48. /**
  49. * @internal
  50. */
  51. var isNonEmpty = function (as) { return as.length > 0; };
  52. exports.isNonEmpty = isNonEmpty;
  53. /**
  54. * @internal
  55. */
  56. var isOutOfBound = function (i, as) { return i < 0 || i >= as.length; };
  57. exports.isOutOfBound = isOutOfBound;
  58. /**
  59. * @internal
  60. */
  61. var prependW = function (head) {
  62. return function (tail) {
  63. return __spreadArray([head], tail, true);
  64. };
  65. };
  66. exports.prependW = prependW;
  67. /**
  68. * @internal
  69. */
  70. exports.prepend = exports.prependW;
  71. /**
  72. * @internal
  73. */
  74. var appendW = function (end) {
  75. return function (init) {
  76. return __spreadArray(__spreadArray([], init, true), [end], false);
  77. };
  78. };
  79. exports.appendW = appendW;
  80. /**
  81. * @internal
  82. */
  83. exports.append = exports.appendW;
  84. /**
  85. * @internal
  86. */
  87. var unsafeInsertAt = function (i, a, as) {
  88. if ((0, exports.isNonEmpty)(as)) {
  89. var xs = (0, exports.fromReadonlyNonEmptyArray)(as);
  90. xs.splice(i, 0, a);
  91. return xs;
  92. }
  93. return [a];
  94. };
  95. exports.unsafeInsertAt = unsafeInsertAt;
  96. /**
  97. * @internal
  98. */
  99. var unsafeUpdateAt = function (i, a, as) {
  100. var xs = (0, exports.fromReadonlyNonEmptyArray)(as);
  101. xs[i] = a;
  102. return xs;
  103. };
  104. exports.unsafeUpdateAt = unsafeUpdateAt;
  105. /**
  106. * Remove duplicates from a `NonEmptyArray`, keeping the first occurrence of an element.
  107. *
  108. * @example
  109. * import { uniq } from 'fp-ts/NonEmptyArray'
  110. * import * as N from 'fp-ts/number'
  111. *
  112. * assert.deepStrictEqual(uniq(N.Eq)([1, 2, 1]), [1, 2])
  113. *
  114. * @since 2.11.0
  115. */
  116. var uniq = function (E) {
  117. return function (as) {
  118. if (as.length === 1) {
  119. return (0, exports.copy)(as);
  120. }
  121. var out = [(0, exports.head)(as)];
  122. var rest = (0, exports.tail)(as);
  123. var _loop_1 = function (a) {
  124. if (out.every(function (o) { return !E.equals(o, a); })) {
  125. out.push(a);
  126. }
  127. };
  128. for (var _i = 0, rest_1 = rest; _i < rest_1.length; _i++) {
  129. var a = rest_1[_i];
  130. _loop_1(a);
  131. }
  132. return out;
  133. };
  134. };
  135. exports.uniq = uniq;
  136. /**
  137. * Sort the elements of a `NonEmptyArray` in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`,
  138. * etc...
  139. *
  140. * @example
  141. * import * as NEA from 'fp-ts/NonEmptyArray'
  142. * import { contramap } from 'fp-ts/Ord'
  143. * import * as S from 'fp-ts/string'
  144. * import * as N from 'fp-ts/number'
  145. * import { pipe } from 'fp-ts/function'
  146. *
  147. * interface Person {
  148. * name: string
  149. * age: number
  150. * }
  151. *
  152. * const byName = pipe(S.Ord, contramap((p: Person) => p.name))
  153. *
  154. * const byAge = pipe(N.Ord, contramap((p: Person) => p.age))
  155. *
  156. * const sortByNameByAge = NEA.sortBy([byName, byAge])
  157. *
  158. * const persons: NEA.NonEmptyArray<Person> = [
  159. * { name: 'a', age: 1 },
  160. * { name: 'b', age: 3 },
  161. * { name: 'c', age: 2 },
  162. * { name: 'b', age: 2 }
  163. * ]
  164. *
  165. * assert.deepStrictEqual(sortByNameByAge(persons), [
  166. * { name: 'a', age: 1 },
  167. * { name: 'b', age: 2 },
  168. * { name: 'b', age: 3 },
  169. * { name: 'c', age: 2 }
  170. * ])
  171. *
  172. * @since 2.11.0
  173. */
  174. var sortBy = function (ords) {
  175. if ((0, exports.isNonEmpty)(ords)) {
  176. var M = (0, Ord_1.getMonoid)();
  177. return (0, exports.sort)(ords.reduce(M.concat, M.empty));
  178. }
  179. return exports.copy;
  180. };
  181. exports.sortBy = sortBy;
  182. /**
  183. * @since 2.11.0
  184. */
  185. var union = function (E) {
  186. var uniqE = (0, exports.uniq)(E);
  187. return function (second) { return function (first) { return uniqE((0, function_1.pipe)(first, concat(second))); }; };
  188. };
  189. exports.union = union;
  190. /**
  191. * Rotate a `NonEmptyArray` by `n` steps.
  192. *
  193. * @example
  194. * import { rotate } from 'fp-ts/NonEmptyArray'
  195. *
  196. * assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3])
  197. * assert.deepStrictEqual(rotate(-2)([1, 2, 3, 4, 5]), [3, 4, 5, 1, 2])
  198. *
  199. * @since 2.11.0
  200. */
  201. var rotate = function (n) {
  202. return function (as) {
  203. var len = as.length;
  204. var m = Math.round(n) % len;
  205. if ((0, exports.isOutOfBound)(Math.abs(m), as) || m === 0) {
  206. return (0, exports.copy)(as);
  207. }
  208. if (m < 0) {
  209. var _a = (0, exports.splitAt)(-m)(as), f = _a[0], s = _a[1];
  210. return (0, function_1.pipe)(s, concat(f));
  211. }
  212. else {
  213. return (0, exports.rotate)(m - len)(as);
  214. }
  215. };
  216. };
  217. exports.rotate = rotate;
  218. // -------------------------------------------------------------------------------------
  219. // constructors
  220. // -------------------------------------------------------------------------------------
  221. /**
  222. * @category conversions
  223. * @since 2.10.0
  224. */
  225. exports.fromReadonlyNonEmptyArray = _.fromReadonlyNonEmptyArray;
  226. /**
  227. * Builds a `NonEmptyArray` from an `Array` returning `none` if `as` is an empty array
  228. *
  229. * @category conversions
  230. * @since 2.0.0
  231. */
  232. var fromArray = function (as) { return ((0, exports.isNonEmpty)(as) ? _.some(as) : _.none); };
  233. exports.fromArray = fromArray;
  234. /**
  235. * Return a `NonEmptyArray` of length `n` with element `i` initialized with `f(i)`.
  236. *
  237. * **Note**. `n` is normalized to a natural number.
  238. *
  239. * @example
  240. * import { makeBy } from 'fp-ts/NonEmptyArray'
  241. * import { pipe } from 'fp-ts/function'
  242. *
  243. * const double = (n: number): number => n * 2
  244. * assert.deepStrictEqual(pipe(5, makeBy(double)), [0, 2, 4, 6, 8])
  245. *
  246. * @category constructors
  247. * @since 2.11.0
  248. */
  249. var makeBy = function (f) {
  250. return function (n) {
  251. var j = Math.max(0, Math.floor(n));
  252. var out = [f(0)];
  253. for (var i = 1; i < j; i++) {
  254. out.push(f(i));
  255. }
  256. return out;
  257. };
  258. };
  259. exports.makeBy = makeBy;
  260. /**
  261. * Create a `NonEmptyArray` containing a value repeated the specified number of times.
  262. *
  263. * **Note**. `n` is normalized to a natural number.
  264. *
  265. * @example
  266. * import { replicate } from 'fp-ts/NonEmptyArray'
  267. * import { pipe } from 'fp-ts/function'
  268. *
  269. * assert.deepStrictEqual(pipe(3, replicate('a')), ['a', 'a', 'a'])
  270. *
  271. * @category constructors
  272. * @since 2.11.0
  273. */
  274. var replicate = function (a) { return (0, exports.makeBy)(function () { return a; }); };
  275. exports.replicate = replicate;
  276. /**
  277. * Create a `NonEmptyArray` containing a range of integers, including both endpoints.
  278. *
  279. * @example
  280. * import { range } from 'fp-ts/NonEmptyArray'
  281. *
  282. * assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5])
  283. *
  284. * @category constructors
  285. * @since 2.11.0
  286. */
  287. var range = function (start, end) {
  288. return start <= end ? (0, exports.makeBy)(function (i) { return start + i; })(end - start + 1) : [start];
  289. };
  290. exports.range = range;
  291. /**
  292. * Return the tuple of the `head` and the `tail`.
  293. *
  294. * @example
  295. * import { unprepend } from 'fp-ts/NonEmptyArray'
  296. *
  297. * assert.deepStrictEqual(unprepend([1, 2, 3]), [1, [2, 3]])
  298. *
  299. * @since 2.9.0
  300. */
  301. var unprepend = function (as) { return [(0, exports.head)(as), (0, exports.tail)(as)]; };
  302. exports.unprepend = unprepend;
  303. /**
  304. * Return the tuple of the `init` and the `last`.
  305. *
  306. * @example
  307. * import { unappend } from 'fp-ts/NonEmptyArray'
  308. *
  309. * assert.deepStrictEqual(unappend([1, 2, 3, 4]), [[1, 2, 3], 4])
  310. *
  311. * @since 2.9.0
  312. */
  313. var unappend = function (as) { return [(0, exports.init)(as), (0, exports.last)(as)]; };
  314. exports.unappend = unappend;
  315. function concatW(second) {
  316. return function (first) { return first.concat(second); };
  317. }
  318. exports.concatW = concatW;
  319. function concat(x, y) {
  320. return y ? x.concat(y) : function (y) { return y.concat(x); };
  321. }
  322. exports.concat = concat;
  323. /**
  324. * @since 2.0.0
  325. */
  326. var reverse = function (as) { return __spreadArray([(0, exports.last)(as)], as.slice(0, -1).reverse(), true); };
  327. exports.reverse = reverse;
  328. function group(E) {
  329. return function (as) {
  330. var len = as.length;
  331. if (len === 0) {
  332. return [];
  333. }
  334. var out = [];
  335. var head = as[0];
  336. var nea = [head];
  337. for (var i = 1; i < len; i++) {
  338. var a = as[i];
  339. if (E.equals(a, head)) {
  340. nea.push(a);
  341. }
  342. else {
  343. out.push(nea);
  344. head = a;
  345. nea = [head];
  346. }
  347. }
  348. out.push(nea);
  349. return out;
  350. };
  351. }
  352. exports.group = group;
  353. /**
  354. * Splits an array into sub-non-empty-arrays stored in an object, based on the result of calling a `string`-returning
  355. * function on each element, and grouping the results according to values returned
  356. *
  357. * @example
  358. * import { groupBy } from 'fp-ts/NonEmptyArray'
  359. *
  360. * assert.deepStrictEqual(groupBy((s: string) => String(s.length))(['a', 'b', 'ab']), {
  361. * '1': ['a', 'b'],
  362. * '2': ['ab']
  363. * })
  364. *
  365. * @since 2.0.0
  366. */
  367. var groupBy = function (f) {
  368. return function (as) {
  369. var out = {};
  370. for (var _i = 0, as_1 = as; _i < as_1.length; _i++) {
  371. var a = as_1[_i];
  372. var k = f(a);
  373. if (_.has.call(out, k)) {
  374. out[k].push(a);
  375. }
  376. else {
  377. out[k] = [a];
  378. }
  379. }
  380. return out;
  381. };
  382. };
  383. exports.groupBy = groupBy;
  384. /**
  385. * @since 2.0.0
  386. */
  387. var sort = function (O) {
  388. return function (as) {
  389. return as.slice().sort(O.compare);
  390. };
  391. };
  392. exports.sort = sort;
  393. /**
  394. * @since 2.0.0
  395. */
  396. var insertAt = function (i, a) {
  397. return function (as) {
  398. return i < 0 || i > as.length ? _.none : _.some((0, exports.unsafeInsertAt)(i, a, as));
  399. };
  400. };
  401. exports.insertAt = insertAt;
  402. /**
  403. * @since 2.0.0
  404. */
  405. var updateAt = function (i, a) {
  406. return (0, exports.modifyAt)(i, function () { return a; });
  407. };
  408. exports.updateAt = updateAt;
  409. /**
  410. * @since 2.0.0
  411. */
  412. var modifyAt = function (i, f) {
  413. return function (as) {
  414. return (0, exports.isOutOfBound)(i, as) ? _.none : _.some((0, exports.unsafeUpdateAt)(i, f(as[i]), as));
  415. };
  416. };
  417. exports.modifyAt = modifyAt;
  418. /**
  419. * @since 2.0.0
  420. */
  421. exports.copy = exports.fromReadonlyNonEmptyArray;
  422. /**
  423. * @category constructors
  424. * @since 2.0.0
  425. */
  426. var of = function (a) { return [a]; };
  427. exports.of = of;
  428. /**
  429. * @since 2.5.1
  430. */
  431. var zipWith = function (as, bs, f) {
  432. var cs = [f(as[0], bs[0])];
  433. var len = Math.min(as.length, bs.length);
  434. for (var i = 1; i < len; i++) {
  435. cs[i] = f(as[i], bs[i]);
  436. }
  437. return cs;
  438. };
  439. exports.zipWith = zipWith;
  440. function zip(as, bs) {
  441. if (bs === undefined) {
  442. return function (bs) { return zip(bs, as); };
  443. }
  444. return (0, exports.zipWith)(as, bs, function (a, b) { return [a, b]; });
  445. }
  446. exports.zip = zip;
  447. /**
  448. * @since 2.5.1
  449. */
  450. var unzip = function (abs) {
  451. var fa = [abs[0][0]];
  452. var fb = [abs[0][1]];
  453. for (var i = 1; i < abs.length; i++) {
  454. fa[i] = abs[i][0];
  455. fb[i] = abs[i][1];
  456. }
  457. return [fa, fb];
  458. };
  459. exports.unzip = unzip;
  460. /**
  461. * Prepend an element to every member of an array
  462. *
  463. * @example
  464. * import { prependAll } from 'fp-ts/NonEmptyArray'
  465. *
  466. * assert.deepStrictEqual(prependAll(9)([1, 2, 3, 4]), [9, 1, 9, 2, 9, 3, 9, 4])
  467. *
  468. * @since 2.10.0
  469. */
  470. var prependAll = function (middle) {
  471. return function (as) {
  472. var out = [middle, as[0]];
  473. for (var i = 1; i < as.length; i++) {
  474. out.push(middle, as[i]);
  475. }
  476. return out;
  477. };
  478. };
  479. exports.prependAll = prependAll;
  480. /**
  481. * Places an element in between members of an array
  482. *
  483. * @example
  484. * import { intersperse } from 'fp-ts/NonEmptyArray'
  485. *
  486. * assert.deepStrictEqual(intersperse(9)([1, 2, 3, 4]), [1, 9, 2, 9, 3, 9, 4])
  487. *
  488. * @since 2.9.0
  489. */
  490. var intersperse = function (middle) {
  491. return function (as) {
  492. var rest = (0, exports.tail)(as);
  493. return (0, exports.isNonEmpty)(rest) ? (0, function_1.pipe)(rest, (0, exports.prependAll)(middle), (0, exports.prepend)((0, exports.head)(as))) : (0, exports.copy)(as);
  494. };
  495. };
  496. exports.intersperse = intersperse;
  497. /**
  498. * @category folding
  499. * @since 2.0.0
  500. */
  501. exports.foldMapWithIndex = RNEA.foldMapWithIndex;
  502. /**
  503. * @category folding
  504. * @since 2.0.0
  505. */
  506. exports.foldMap = RNEA.foldMap;
  507. /**
  508. * @category sequencing
  509. * @since 2.10.0
  510. */
  511. var chainWithIndex = function (f) {
  512. return function (as) {
  513. var out = (0, exports.fromReadonlyNonEmptyArray)(f(0, (0, exports.head)(as)));
  514. for (var i = 1; i < as.length; i++) {
  515. out.push.apply(out, f(i, as[i]));
  516. }
  517. return out;
  518. };
  519. };
  520. exports.chainWithIndex = chainWithIndex;
  521. /**
  522. * @since 2.10.0
  523. */
  524. var chop = function (f) {
  525. return function (as) {
  526. var _a = f(as), b = _a[0], rest = _a[1];
  527. var out = [b];
  528. var next = rest;
  529. while ((0, exports.isNonEmpty)(next)) {
  530. var _b = f(next), b_1 = _b[0], rest_2 = _b[1];
  531. out.push(b_1);
  532. next = rest_2;
  533. }
  534. return out;
  535. };
  536. };
  537. exports.chop = chop;
  538. /**
  539. * Splits a `NonEmptyArray` into two pieces, the first piece has max `n` elements.
  540. *
  541. * @since 2.10.0
  542. */
  543. var splitAt = function (n) {
  544. return function (as) {
  545. var m = Math.max(1, n);
  546. return m >= as.length ? [(0, exports.copy)(as), []] : [(0, function_1.pipe)(as.slice(1, m), (0, exports.prepend)((0, exports.head)(as))), as.slice(m)];
  547. };
  548. };
  549. exports.splitAt = splitAt;
  550. /**
  551. * @since 2.10.0
  552. */
  553. var chunksOf = function (n) { return (0, exports.chop)((0, exports.splitAt)(n)); };
  554. exports.chunksOf = chunksOf;
  555. /* istanbul ignore next */
  556. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
  557. /* istanbul ignore next */
  558. var _mapWithIndex = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.mapWithIndex)(f)); };
  559. /* istanbul ignore next */
  560. var _ap = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); };
  561. /* istanbul ignore next */
  562. var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
  563. /* istanbul ignore next */
  564. var _extend = function (wa, f) { return (0, function_1.pipe)(wa, (0, exports.extend)(f)); };
  565. /* istanbul ignore next */
  566. var _reduce = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduce)(b, f)); };
  567. /* istanbul ignore next */
  568. var _foldMap = function (M) {
  569. var foldMapM = (0, exports.foldMap)(M);
  570. return function (fa, f) { return (0, function_1.pipe)(fa, foldMapM(f)); };
  571. };
  572. /* istanbul ignore next */
  573. var _reduceRight = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduceRight)(b, f)); };
  574. /* istanbul ignore next */
  575. var _traverse = function (F) {
  576. var traverseF = (0, exports.traverse)(F);
  577. return function (ta, f) { return (0, function_1.pipe)(ta, traverseF(f)); };
  578. };
  579. /* istanbul ignore next */
  580. var _alt = function (fa, that) { return (0, function_1.pipe)(fa, (0, exports.alt)(that)); };
  581. /* istanbul ignore next */
  582. var _reduceWithIndex = function (fa, b, f) {
  583. return (0, function_1.pipe)(fa, (0, exports.reduceWithIndex)(b, f));
  584. };
  585. /* istanbul ignore next */
  586. var _foldMapWithIndex = function (M) {
  587. var foldMapWithIndexM = (0, exports.foldMapWithIndex)(M);
  588. return function (fa, f) { return (0, function_1.pipe)(fa, foldMapWithIndexM(f)); };
  589. };
  590. /* istanbul ignore next */
  591. var _reduceRightWithIndex = function (fa, b, f) {
  592. return (0, function_1.pipe)(fa, (0, exports.reduceRightWithIndex)(b, f));
  593. };
  594. /* istanbul ignore next */
  595. var _traverseWithIndex = function (F) {
  596. var traverseWithIndexF = (0, exports.traverseWithIndex)(F);
  597. return function (ta, f) { return (0, function_1.pipe)(ta, traverseWithIndexF(f)); };
  598. };
  599. /**
  600. * Less strict version of [`alt`](#alt).
  601. *
  602. * The `W` suffix (short for **W**idening) means that the return types will be merged.
  603. *
  604. * @example
  605. * import * as NEA from 'fp-ts/NonEmptyArray'
  606. * import { pipe } from 'fp-ts/function'
  607. *
  608. * assert.deepStrictEqual(
  609. * pipe(
  610. * [1, 2, 3] as NEA.NonEmptyArray<number>,
  611. * NEA.altW(() => ['a', 'b'])
  612. * ),
  613. * [1, 2, 3, 'a', 'b']
  614. * )
  615. *
  616. * @category error handling
  617. * @since 2.9.0
  618. */
  619. var altW = function (that) {
  620. return function (as) {
  621. return (0, function_1.pipe)(as, concatW(that()));
  622. };
  623. };
  624. exports.altW = altW;
  625. /**
  626. * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
  627. * types of kind `* -> *`.
  628. *
  629. * In case of `NonEmptyArray` concatenates the inputs into a single array.
  630. *
  631. * @example
  632. * import * as NEA from 'fp-ts/NonEmptyArray'
  633. * import { pipe } from 'fp-ts/function'
  634. *
  635. * assert.deepStrictEqual(
  636. * pipe(
  637. * [1, 2, 3],
  638. * NEA.alt(() => [4, 5])
  639. * ),
  640. * [1, 2, 3, 4, 5]
  641. * )
  642. *
  643. * @category error handling
  644. * @since 2.6.2
  645. */
  646. exports.alt = exports.altW;
  647. /**
  648. * Apply a function to an argument under a type constructor.
  649. *
  650. * @since 2.0.0
  651. */
  652. var ap = function (as) {
  653. return (0, exports.chain)(function (f) { return (0, function_1.pipe)(as, (0, exports.map)(f)); });
  654. };
  655. exports.ap = ap;
  656. /**
  657. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  658. *
  659. * @example
  660. * import * as NEA from 'fp-ts/NonEmptyArray'
  661. * import { pipe } from 'fp-ts/function'
  662. *
  663. * assert.deepStrictEqual(
  664. * pipe(
  665. * [1, 2, 3],
  666. * NEA.chain((n) => [`a${n}`, `b${n}`])
  667. * ),
  668. * ['a1', 'b1', 'a2', 'b2', 'a3', 'b3']
  669. * )
  670. *
  671. * @category sequencing
  672. * @since 2.0.0
  673. */
  674. var chain = function (f) {
  675. return (0, exports.chainWithIndex)(function (_, a) { return f(a); });
  676. };
  677. exports.chain = chain;
  678. /**
  679. * @since 2.0.0
  680. */
  681. var extend = function (f) {
  682. return function (as) {
  683. var next = (0, exports.tail)(as);
  684. var out = [f(as)];
  685. while ((0, exports.isNonEmpty)(next)) {
  686. out.push(f(next));
  687. next = (0, exports.tail)(next);
  688. }
  689. return out;
  690. };
  691. };
  692. exports.extend = extend;
  693. /**
  694. * @since 2.5.0
  695. */
  696. exports.duplicate = (0, exports.extend)(function_1.identity);
  697. /**
  698. * @category sequencing
  699. * @since 2.5.0
  700. */
  701. exports.flatten = (0, exports.chain)(function_1.identity);
  702. /**
  703. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  704. * use the type constructor `F` to represent some computational context.
  705. *
  706. * @category mapping
  707. * @since 2.0.0
  708. */
  709. var map = function (f) { return (0, exports.mapWithIndex)(function (_, a) { return f(a); }); };
  710. exports.map = map;
  711. /**
  712. * @category mapping
  713. * @since 2.0.0
  714. */
  715. var mapWithIndex = function (f) {
  716. return function (as) {
  717. var out = [f(0, (0, exports.head)(as))];
  718. for (var i = 1; i < as.length; i++) {
  719. out.push(f(i, as[i]));
  720. }
  721. return out;
  722. };
  723. };
  724. exports.mapWithIndex = mapWithIndex;
  725. /**
  726. * @category folding
  727. * @since 2.0.0
  728. */
  729. exports.reduce = RNEA.reduce;
  730. /**
  731. * @category folding
  732. * @since 2.0.0
  733. */
  734. exports.reduceWithIndex = RNEA.reduceWithIndex;
  735. /**
  736. * @category folding
  737. * @since 2.0.0
  738. */
  739. exports.reduceRight = RNEA.reduceRight;
  740. /**
  741. * @category folding
  742. * @since 2.0.0
  743. */
  744. exports.reduceRightWithIndex = RNEA.reduceRightWithIndex;
  745. /**
  746. * @category traversing
  747. * @since 2.6.3
  748. */
  749. var traverse = function (F) {
  750. var traverseWithIndexF = (0, exports.traverseWithIndex)(F);
  751. return function (f) { return traverseWithIndexF(function (_, a) { return f(a); }); };
  752. };
  753. exports.traverse = traverse;
  754. /**
  755. * @category traversing
  756. * @since 2.6.3
  757. */
  758. var sequence = function (F) { return (0, exports.traverseWithIndex)(F)(function (_, a) { return a; }); };
  759. exports.sequence = sequence;
  760. /**
  761. * @category sequencing
  762. * @since 2.6.3
  763. */
  764. var traverseWithIndex = function (F) {
  765. return function (f) {
  766. return function (as) {
  767. var out = F.map(f(0, (0, exports.head)(as)), exports.of);
  768. for (var i = 1; i < as.length; i++) {
  769. out = F.ap(F.map(out, function (bs) { return function (b) { return (0, function_1.pipe)(bs, (0, exports.append)(b)); }; }), f(i, as[i]));
  770. }
  771. return out;
  772. };
  773. };
  774. };
  775. exports.traverseWithIndex = traverseWithIndex;
  776. /**
  777. * @since 2.7.0
  778. */
  779. exports.extract = RNEA.head;
  780. /**
  781. * @category type lambdas
  782. * @since 2.0.0
  783. */
  784. exports.URI = 'NonEmptyArray';
  785. /**
  786. * @category instances
  787. * @since 2.0.0
  788. */
  789. exports.getShow = RNEA.getShow;
  790. /**
  791. * Builds a `Semigroup` instance for `NonEmptyArray`
  792. *
  793. * @category instances
  794. * @since 2.0.0
  795. */
  796. var getSemigroup = function () { return ({
  797. concat: concat
  798. }); };
  799. exports.getSemigroup = getSemigroup;
  800. /**
  801. * @example
  802. * import { getEq } from 'fp-ts/NonEmptyArray'
  803. * import * as N from 'fp-ts/number'
  804. *
  805. * const E = getEq(N.Eq)
  806. * assert.strictEqual(E.equals([1, 2], [1, 2]), true)
  807. * assert.strictEqual(E.equals([1, 2], [1, 3]), false)
  808. *
  809. * @category instances
  810. * @since 2.0.0
  811. */
  812. exports.getEq = RNEA.getEq;
  813. /**
  814. * @since 2.11.0
  815. */
  816. var getUnionSemigroup = function (E) {
  817. var unionE = (0, exports.union)(E);
  818. return {
  819. concat: function (first, second) { return unionE(second)(first); }
  820. };
  821. };
  822. exports.getUnionSemigroup = getUnionSemigroup;
  823. /**
  824. * @category instances
  825. * @since 2.7.0
  826. */
  827. exports.Functor = {
  828. URI: exports.URI,
  829. map: _map
  830. };
  831. /**
  832. * @category mapping
  833. * @since 2.10.0
  834. */
  835. exports.flap = (0, Functor_1.flap)(exports.Functor);
  836. /**
  837. * @category instances
  838. * @since 2.10.0
  839. */
  840. exports.Pointed = {
  841. URI: exports.URI,
  842. of: exports.of
  843. };
  844. /**
  845. * @category instances
  846. * @since 2.7.0
  847. */
  848. exports.FunctorWithIndex = {
  849. URI: exports.URI,
  850. map: _map,
  851. mapWithIndex: _mapWithIndex
  852. };
  853. /**
  854. * @category instances
  855. * @since 2.10.0
  856. */
  857. exports.Apply = {
  858. URI: exports.URI,
  859. map: _map,
  860. ap: _ap
  861. };
  862. /**
  863. * Combine two effectful actions, keeping only the result of the first.
  864. *
  865. * @since 2.5.0
  866. */
  867. exports.apFirst = (0, Apply_1.apFirst)(exports.Apply);
  868. /**
  869. * Combine two effectful actions, keeping only the result of the second.
  870. *
  871. * @since 2.5.0
  872. */
  873. exports.apSecond = (0, Apply_1.apSecond)(exports.Apply);
  874. /**
  875. * @category instances
  876. * @since 2.7.0
  877. */
  878. exports.Applicative = {
  879. URI: exports.URI,
  880. map: _map,
  881. ap: _ap,
  882. of: exports.of
  883. };
  884. /**
  885. * @category instances
  886. * @since 2.10.0
  887. */
  888. exports.Chain = {
  889. URI: exports.URI,
  890. map: _map,
  891. ap: _ap,
  892. chain: _chain
  893. };
  894. /**
  895. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  896. * keeping only the result of the first.
  897. *
  898. * @category sequencing
  899. * @since 2.5.0
  900. */
  901. exports.chainFirst =
  902. /*#__PURE__*/ (0, Chain_1.chainFirst)(exports.Chain);
  903. /**
  904. * @category instances
  905. * @since 2.7.0
  906. */
  907. exports.Monad = {
  908. URI: exports.URI,
  909. map: _map,
  910. ap: _ap,
  911. of: exports.of,
  912. chain: _chain
  913. };
  914. /**
  915. * @category instances
  916. * @since 2.7.0
  917. */
  918. exports.Foldable = {
  919. URI: exports.URI,
  920. reduce: _reduce,
  921. foldMap: _foldMap,
  922. reduceRight: _reduceRight
  923. };
  924. /**
  925. * @category instances
  926. * @since 2.7.0
  927. */
  928. exports.FoldableWithIndex = {
  929. URI: exports.URI,
  930. reduce: _reduce,
  931. foldMap: _foldMap,
  932. reduceRight: _reduceRight,
  933. reduceWithIndex: _reduceWithIndex,
  934. foldMapWithIndex: _foldMapWithIndex,
  935. reduceRightWithIndex: _reduceRightWithIndex
  936. };
  937. /**
  938. * @category instances
  939. * @since 2.7.0
  940. */
  941. exports.Traversable = {
  942. URI: exports.URI,
  943. map: _map,
  944. reduce: _reduce,
  945. foldMap: _foldMap,
  946. reduceRight: _reduceRight,
  947. traverse: _traverse,
  948. sequence: exports.sequence
  949. };
  950. /**
  951. * @category instances
  952. * @since 2.7.0
  953. */
  954. exports.TraversableWithIndex = {
  955. URI: exports.URI,
  956. map: _map,
  957. mapWithIndex: _mapWithIndex,
  958. reduce: _reduce,
  959. foldMap: _foldMap,
  960. reduceRight: _reduceRight,
  961. traverse: _traverse,
  962. sequence: exports.sequence,
  963. reduceWithIndex: _reduceWithIndex,
  964. foldMapWithIndex: _foldMapWithIndex,
  965. reduceRightWithIndex: _reduceRightWithIndex,
  966. traverseWithIndex: _traverseWithIndex
  967. };
  968. /**
  969. * @category instances
  970. * @since 2.7.0
  971. */
  972. exports.Alt = {
  973. URI: exports.URI,
  974. map: _map,
  975. alt: _alt
  976. };
  977. /**
  978. * @category instances
  979. * @since 2.7.0
  980. */
  981. exports.Comonad = {
  982. URI: exports.URI,
  983. map: _map,
  984. extend: _extend,
  985. extract: exports.extract
  986. };
  987. // -------------------------------------------------------------------------------------
  988. // do notation
  989. // -------------------------------------------------------------------------------------
  990. /**
  991. * @category do notation
  992. * @since 2.9.0
  993. */
  994. exports.Do = (0, exports.of)(_.emptyRecord);
  995. /**
  996. * @category do notation
  997. * @since 2.8.0
  998. */
  999. exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
  1000. var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
  1001. exports.let = let_;
  1002. /**
  1003. * @category do notation
  1004. * @since 2.8.0
  1005. */
  1006. exports.bind = (0, Chain_1.bind)(exports.Chain);
  1007. /**
  1008. * @category do notation
  1009. * @since 2.8.0
  1010. */
  1011. exports.apS = (0, Apply_1.apS)(exports.Apply);
  1012. // -------------------------------------------------------------------------------------
  1013. // utils
  1014. // -------------------------------------------------------------------------------------
  1015. /**
  1016. * @since 2.0.0
  1017. */
  1018. exports.head = RNEA.head;
  1019. /**
  1020. * @since 2.0.0
  1021. */
  1022. var tail = function (as) { return as.slice(1); };
  1023. exports.tail = tail;
  1024. /**
  1025. * @since 2.0.0
  1026. */
  1027. exports.last = RNEA.last;
  1028. /**
  1029. * Get all but the last element of a non empty array, creating a new array.
  1030. *
  1031. * @example
  1032. * import { init } from 'fp-ts/NonEmptyArray'
  1033. *
  1034. * assert.deepStrictEqual(init([1, 2, 3]), [1, 2])
  1035. * assert.deepStrictEqual(init([1]), [])
  1036. *
  1037. * @since 2.2.0
  1038. */
  1039. var init = function (as) { return as.slice(0, -1); };
  1040. exports.init = init;
  1041. /**
  1042. * @since 2.0.0
  1043. */
  1044. exports.min = RNEA.min;
  1045. /**
  1046. * @since 2.0.0
  1047. */
  1048. exports.max = RNEA.max;
  1049. /**
  1050. * @since 2.10.0
  1051. */
  1052. var concatAll = function (S) {
  1053. return function (as) {
  1054. return as.reduce(S.concat);
  1055. };
  1056. };
  1057. exports.concatAll = concatAll;
  1058. /**
  1059. * Break an `Array` into its first element and remaining elements.
  1060. *
  1061. * @category pattern matching
  1062. * @since 2.11.0
  1063. */
  1064. var matchLeft = function (f) {
  1065. return function (as) {
  1066. return f((0, exports.head)(as), (0, exports.tail)(as));
  1067. };
  1068. };
  1069. exports.matchLeft = matchLeft;
  1070. /**
  1071. * Break an `Array` into its initial elements and the last element.
  1072. *
  1073. * @category pattern matching
  1074. * @since 2.11.0
  1075. */
  1076. var matchRight = function (f) {
  1077. return function (as) {
  1078. return f((0, exports.init)(as), (0, exports.last)(as));
  1079. };
  1080. };
  1081. exports.matchRight = matchRight;
  1082. /**
  1083. * Apply a function to the head, creating a new `NonEmptyArray`.
  1084. *
  1085. * @since 2.11.0
  1086. */
  1087. var modifyHead = function (f) {
  1088. return function (as) {
  1089. return __spreadArray([f((0, exports.head)(as))], (0, exports.tail)(as), true);
  1090. };
  1091. };
  1092. exports.modifyHead = modifyHead;
  1093. /**
  1094. * Change the head, creating a new `NonEmptyArray`.
  1095. *
  1096. * @since 2.11.0
  1097. */
  1098. var updateHead = function (a) { return (0, exports.modifyHead)(function () { return a; }); };
  1099. exports.updateHead = updateHead;
  1100. /**
  1101. * Apply a function to the last element, creating a new `NonEmptyArray`.
  1102. *
  1103. * @since 2.11.0
  1104. */
  1105. var modifyLast = function (f) {
  1106. return function (as) {
  1107. return (0, function_1.pipe)((0, exports.init)(as), (0, exports.append)(f((0, exports.last)(as))));
  1108. };
  1109. };
  1110. exports.modifyLast = modifyLast;
  1111. /**
  1112. * Change the last element, creating a new `NonEmptyArray`.
  1113. *
  1114. * @since 2.11.0
  1115. */
  1116. var updateLast = function (a) { return (0, exports.modifyLast)(function () { return a; }); };
  1117. exports.updateLast = updateLast;
  1118. /**
  1119. * Places an element in between members of a `NonEmptyArray`, then folds the results using the provided `Semigroup`.
  1120. *
  1121. * @example
  1122. * import * as S from 'fp-ts/string'
  1123. * import { intercalate } from 'fp-ts/NonEmptyArray'
  1124. *
  1125. * assert.deepStrictEqual(intercalate(S.Semigroup)('-')(['a', 'b', 'c']), 'a-b-c')
  1126. *
  1127. * @since 2.12.0
  1128. */
  1129. exports.intercalate = RNEA.intercalate;
  1130. function groupSort(O) {
  1131. var sortO = (0, exports.sort)(O);
  1132. var groupO = group(O);
  1133. return function (as) { return ((0, exports.isNonEmpty)(as) ? groupO(sortO(as)) : []); };
  1134. }
  1135. exports.groupSort = groupSort;
  1136. function filter(predicate) {
  1137. return (0, exports.filterWithIndex)(function (_, a) { return predicate(a); });
  1138. }
  1139. exports.filter = filter;
  1140. /**
  1141. * Use [`filterWithIndex`](./Array.ts.html#filterwithindex) instead.
  1142. *
  1143. * @category zone of death
  1144. * @since 2.0.0
  1145. * @deprecated
  1146. */
  1147. var filterWithIndex = function (predicate) {
  1148. return function (as) {
  1149. return (0, exports.fromArray)(as.filter(function (a, i) { return predicate(i, a); }));
  1150. };
  1151. };
  1152. exports.filterWithIndex = filterWithIndex;
  1153. /**
  1154. * Use [`unprepend`](#unprepend) instead.
  1155. *
  1156. * @category zone of death
  1157. * @since 2.9.0
  1158. * @deprecated
  1159. */
  1160. exports.uncons = exports.unprepend;
  1161. /**
  1162. * Use [`unappend`](#unappend) instead.
  1163. *
  1164. * @category zone of death
  1165. * @since 2.9.0
  1166. * @deprecated
  1167. */
  1168. exports.unsnoc = exports.unappend;
  1169. function cons(head, tail) {
  1170. return tail === undefined ? (0, exports.prepend)(head) : (0, function_1.pipe)(tail, (0, exports.prepend)(head));
  1171. }
  1172. exports.cons = cons;
  1173. /**
  1174. * Use [`append`](./Array.ts.html#append) instead.
  1175. *
  1176. * @category zone of death
  1177. * @since 2.0.0
  1178. * @deprecated
  1179. */
  1180. var snoc = function (init, end) { return (0, function_1.pipe)(init, (0, exports.append)(end)); };
  1181. exports.snoc = snoc;
  1182. /**
  1183. * Use [`prependAll`](#prependall) instead.
  1184. *
  1185. * @category zone of death
  1186. * @since 2.9.0
  1187. * @deprecated
  1188. */
  1189. exports.prependToAll = exports.prependAll;
  1190. /**
  1191. * Use [`concatAll`](#concatall) instead.
  1192. *
  1193. * @category zone of death
  1194. * @since 2.5.0
  1195. * @deprecated
  1196. */
  1197. exports.fold = RNEA.concatAll;
  1198. /**
  1199. * This instance is deprecated, use small, specific instances instead.
  1200. * For example if a function needs a `Functor` instance, pass `NEA.Functor` instead of `NEA.nonEmptyArray`
  1201. * (where `NEA` is from `import NEA from 'fp-ts/NonEmptyArray'`)
  1202. *
  1203. * @category zone of death
  1204. * @since 2.0.0
  1205. * @deprecated
  1206. */
  1207. exports.nonEmptyArray = {
  1208. URI: exports.URI,
  1209. of: exports.of,
  1210. map: _map,
  1211. mapWithIndex: _mapWithIndex,
  1212. ap: _ap,
  1213. chain: _chain,
  1214. extend: _extend,
  1215. extract: exports.extract,
  1216. reduce: _reduce,
  1217. foldMap: _foldMap,
  1218. reduceRight: _reduceRight,
  1219. traverse: _traverse,
  1220. sequence: exports.sequence,
  1221. reduceWithIndex: _reduceWithIndex,
  1222. foldMapWithIndex: _foldMapWithIndex,
  1223. reduceRightWithIndex: _reduceRightWithIndex,
  1224. traverseWithIndex: _traverseWithIndex,
  1225. alt: _alt
  1226. };