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

1213 líneas
30 KiB

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