版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

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