版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

2611 righe
80 KiB

  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.lefts = exports.rights = exports.reverse = exports.modifyAt = exports.deleteAt = exports.updateAt = exports.insertAt = exports.copy = exports.findLastIndex = exports.findLastMap = exports.findLast = exports.findFirstMap = exports.findFirst = exports.findIndex = exports.dropLeftWhile = exports.dropRight = exports.dropLeft = exports.spanLeft = exports.takeLeftWhile = exports.takeRight = exports.takeLeft = exports.init = exports.tail = exports.last = exports.head = exports.lookup = exports.isOutOfBound = exports.size = exports.scanRight = exports.scanLeft = exports.chainWithIndex = exports.foldRight = exports.matchRight = exports.matchRightW = exports.foldLeft = exports.matchLeft = exports.matchLeftW = exports.match = exports.matchW = exports.fromEither = exports.fromOption = exports.fromPredicate = exports.replicate = exports.makeBy = exports.appendW = exports.append = exports.prependW = exports.prepend = exports.isNonEmpty = exports.isEmpty = void 0;
  27. exports.traverseWithIndex = exports.sequence = exports.traverse = exports.reduceRightWithIndex = exports.reduceRight = exports.reduceWithIndex = exports.reduce = exports.foldMapWithIndex = exports.foldMap = exports.duplicate = exports.extend = exports.filterWithIndex = exports.alt = exports.altW = exports.partitionMapWithIndex = exports.partitionMap = exports.partitionWithIndex = exports.partition = exports.filter = exports.separate = exports.compact = exports.filterMap = exports.filterMapWithIndex = exports.mapWithIndex = exports.flatten = exports.chain = exports.ap = exports.map = exports.zero = exports.of = exports.difference = exports.intersection = exports.union = exports.concat = exports.concatW = exports.comprehension = exports.fromOptionK = exports.chunksOf = exports.splitAt = exports.chop = exports.sortBy = exports.uniq = exports.elem = exports.rotate = exports.intersperse = exports.prependAll = exports.unzip = exports.zip = exports.zipWith = exports.sort = void 0;
  28. exports.some = exports.every = exports.unsafeDeleteAt = exports.unsafeUpdateAt = exports.unsafeInsertAt = exports.fromEitherK = exports.FromEither = exports.filterE = exports.ChainRecBreadthFirst = exports.chainRecBreadthFirst = exports.ChainRecDepthFirst = exports.chainRecDepthFirst = exports.Witherable = exports.TraversableWithIndex = exports.Traversable = exports.FoldableWithIndex = exports.Foldable = exports.FilterableWithIndex = exports.Filterable = exports.Compactable = exports.Extend = exports.Alternative = exports.guard = exports.Zero = exports.Alt = exports.Unfoldable = exports.Monad = exports.chainFirst = exports.Chain = exports.Applicative = exports.apSecond = exports.apFirst = exports.Apply = exports.FunctorWithIndex = exports.Pointed = exports.flap = exports.Functor = exports.getDifferenceMagma = exports.getIntersectionSemigroup = exports.getUnionMonoid = exports.getUnionSemigroup = exports.getOrd = exports.getEq = exports.getMonoid = exports.getSemigroup = exports.getShow = exports.URI = exports.unfold = exports.wilt = exports.wither = void 0;
  29. exports.array = exports.prependToAll = exports.snoc = exports.cons = exports.empty = exports.range = exports.apS = exports.bind = exports.let = exports.bindTo = exports.Do = exports.intercalate = exports.exists = void 0;
  30. var Apply_1 = require("./Apply");
  31. var Chain_1 = require("./Chain");
  32. var FromEither_1 = require("./FromEither");
  33. var function_1 = require("./function");
  34. var Functor_1 = require("./Functor");
  35. var _ = __importStar(require("./internal"));
  36. var NEA = __importStar(require("./NonEmptyArray"));
  37. var RA = __importStar(require("./ReadonlyArray"));
  38. var Separated_1 = require("./Separated");
  39. var Witherable_1 = require("./Witherable");
  40. var Zero_1 = require("./Zero");
  41. // -------------------------------------------------------------------------------------
  42. // refinements
  43. // -------------------------------------------------------------------------------------
  44. /**
  45. * Test whether an array is empty
  46. *
  47. * @example
  48. * import { isEmpty } from 'fp-ts/Array'
  49. *
  50. * assert.strictEqual(isEmpty([]), true)
  51. * assert.strictEqual(isEmpty(['a']), false)
  52. *
  53. * @category refinements
  54. * @since 2.0.0
  55. */
  56. var isEmpty = function (as) { return as.length === 0; };
  57. exports.isEmpty = isEmpty;
  58. /**
  59. * Test whether an array is non empty narrowing down the type to `NonEmptyArray<A>`
  60. *
  61. * @example
  62. * import { isNonEmpty } from 'fp-ts/Array'
  63. *
  64. * assert.strictEqual(isNonEmpty([]), false)
  65. * assert.strictEqual(isNonEmpty(['a']), true)
  66. *
  67. * @category refinements
  68. * @since 2.0.0
  69. */
  70. exports.isNonEmpty = NEA.isNonEmpty;
  71. // -------------------------------------------------------------------------------------
  72. // constructors
  73. // -------------------------------------------------------------------------------------
  74. /**
  75. * Prepend an element to the front of a `Array`, creating a new `NonEmptyArray`.
  76. *
  77. * @example
  78. * import { prepend } from 'fp-ts/Array'
  79. * import { pipe } from 'fp-ts/function'
  80. *
  81. * assert.deepStrictEqual(pipe([2, 3, 4], prepend(1)), [1, 2, 3, 4])
  82. *
  83. * @since 2.10.0
  84. */
  85. exports.prepend = NEA.prepend;
  86. /**
  87. * Less strict version of [`prepend`](#prepend).
  88. *
  89. * @example
  90. * import { prependW } from 'fp-ts/Array'
  91. * import { pipe } from 'fp-ts/function'
  92. *
  93. * assert.deepStrictEqual(pipe([2, 3, 4], prependW("a")), ["a", 2, 3, 4]);
  94. *
  95. * @since 2.11.0
  96. */
  97. exports.prependW = NEA.prependW;
  98. /**
  99. * Append an element to the end of a `Array`, creating a new `NonEmptyArray`.
  100. *
  101. * @example
  102. * import { append } from 'fp-ts/Array'
  103. * import { pipe } from 'fp-ts/function'
  104. *
  105. * assert.deepStrictEqual(pipe([1, 2, 3], append(4)), [1, 2, 3, 4])
  106. *
  107. * @since 2.10.0
  108. */
  109. exports.append = NEA.append;
  110. /**
  111. * Less strict version of [`append`](#append).
  112. *
  113. * @example
  114. * import { appendW } from 'fp-ts/Array'
  115. * import { pipe } from 'fp-ts/function'
  116. *
  117. * assert.deepStrictEqual(pipe([1, 2, 3], appendW("d")), [1, 2, 3, "d"]);
  118. *
  119. * @since 2.11.0
  120. */
  121. exports.appendW = NEA.appendW;
  122. /**
  123. * Return a `Array` of length `n` with element `i` initialized with `f(i)`.
  124. *
  125. * **Note**. `n` is normalized to a non negative integer.
  126. *
  127. * @example
  128. * import { makeBy } from 'fp-ts/Array'
  129. *
  130. * const double = (i: number): number => i * 2
  131. * assert.deepStrictEqual(makeBy(5, double), [0, 2, 4, 6, 8])
  132. * assert.deepStrictEqual(makeBy(-3, double), [])
  133. * assert.deepStrictEqual(makeBy(4.32164, double), [0, 2, 4, 6])
  134. *
  135. * @category constructors
  136. * @since 2.0.0
  137. */
  138. var makeBy = function (n, f) { return (n <= 0 ? [] : NEA.makeBy(f)(n)); };
  139. exports.makeBy = makeBy;
  140. /**
  141. * Create a `Array` containing a value repeated the specified number of times.
  142. *
  143. * **Note**. `n` is normalized to a non negative integer.
  144. *
  145. * @example
  146. * import { replicate } from 'fp-ts/Array'
  147. *
  148. * assert.deepStrictEqual(replicate(3, 'a'), ['a', 'a', 'a'])
  149. * assert.deepStrictEqual(replicate(-3, 'a'), [])
  150. * assert.deepStrictEqual(replicate(2.985647, 'a'), ['a', 'a'])
  151. *
  152. * @category constructors
  153. * @since 2.0.0
  154. */
  155. var replicate = function (n, a) { return (0, exports.makeBy)(n, function () { return a; }); };
  156. exports.replicate = replicate;
  157. function fromPredicate(predicate) {
  158. return function (a) { return (predicate(a) ? [a] : []); };
  159. }
  160. exports.fromPredicate = fromPredicate;
  161. // -------------------------------------------------------------------------------------
  162. // conversions
  163. // -------------------------------------------------------------------------------------
  164. /**
  165. * Create an array from an `Option`. The resulting array will contain the content of the
  166. * `Option` if it is `Some` and it will be empty if the `Option` is `None`.
  167. *
  168. * @example
  169. * import { fromOption } from 'fp-ts/Array'
  170. * import { option } from "fp-ts";
  171. * import { pipe } from 'fp-ts/function'
  172. *
  173. * assert.deepStrictEqual(pipe(option.some("a"), fromOption),["a"])
  174. * assert.deepStrictEqual(pipe(option.none, fromOption),[])
  175. *
  176. * @category conversions
  177. * @since 2.11.0
  178. */
  179. var fromOption = function (ma) { return (_.isNone(ma) ? [] : [ma.value]); };
  180. exports.fromOption = fromOption;
  181. /**
  182. * Create an array from an `Either`. The resulting array will contain the content of the
  183. * `Either` if it is `Right` and it will be empty if the `Either` is `Left`.
  184. *
  185. * @example
  186. * import { fromEither } from 'fp-ts/Array'
  187. * import { either } from "fp-ts";
  188. * import { pipe } from 'fp-ts/function'
  189. *
  190. * assert.deepStrictEqual(pipe(either.right("r"), fromEither), ["r"]);
  191. * assert.deepStrictEqual(pipe(either.left("l"), fromEither), []);
  192. *
  193. * @category conversions
  194. * @since 2.11.0
  195. */
  196. var fromEither = function (e) { return (_.isLeft(e) ? [] : [e.right]); };
  197. exports.fromEither = fromEither;
  198. /**
  199. * Less strict version of [`match`](#match).
  200. *
  201. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  202. *
  203. * @example
  204. * import { matchW } from 'fp-ts/Array'
  205. * import { pipe } from 'fp-ts/function'
  206. *
  207. * const matcherW = matchW(
  208. * () => "No elements",
  209. * (as) => as.length
  210. * );
  211. * assert.deepStrictEqual(pipe([1, 2, 3, 4], matcherW), 4);
  212. * assert.deepStrictEqual(pipe([], matcherW), "No elements");
  213. *
  214. * @category pattern matching
  215. * @since 2.11.0
  216. */
  217. var matchW = function (onEmpty, onNonEmpty) {
  218. return function (as) {
  219. return (0, exports.isNonEmpty)(as) ? onNonEmpty(as) : onEmpty();
  220. };
  221. };
  222. exports.matchW = matchW;
  223. /**
  224. * Takes an array, if the array is empty it returns the result of `onEmpty`, otherwise
  225. * it passes the array to `onNonEmpty` and returns the result.
  226. *
  227. * @example
  228. * import { match } from 'fp-ts/Array'
  229. * import { pipe } from 'fp-ts/function'
  230. *
  231. * const matcher = match(
  232. * () => "No elements",
  233. * (as) => `Found ${as.length} element(s)`
  234. * );
  235. * assert.deepStrictEqual(pipe([1, 2, 3, 4], matcher), "Found 4 element(s)");
  236. * assert.deepStrictEqual(pipe([], matcher), "No elements");
  237. *
  238. * @category pattern matching
  239. * @since 2.11.0
  240. */
  241. exports.match = exports.matchW;
  242. /**
  243. * Less strict version of [`matchLeft`](#matchleft). It will work when `onEmpty` and
  244. * `onNonEmpty` have different return types.
  245. *
  246. * @example
  247. * import { matchLeftW } from 'fp-ts/Array'
  248. *
  249. * const f = matchLeftW(
  250. * () => 0,
  251. * (head: string, tail: string[]) => `Found "${head}" followed by ${tail.length} elements`
  252. * );
  253. * assert.strictEqual(f(["a", "b", "c"]), 'Found "a" followed by 2 elements');
  254. * assert.strictEqual(f([]), 0);
  255. *
  256. * @category pattern matching
  257. * @since 2.11.0
  258. */
  259. var matchLeftW = function (onEmpty, onNonEmpty) {
  260. return function (as) {
  261. return (0, exports.isNonEmpty)(as) ? onNonEmpty(NEA.head(as), NEA.tail(as)) : onEmpty();
  262. };
  263. };
  264. exports.matchLeftW = matchLeftW;
  265. /**
  266. * Takes an array, if the array is empty it returns the result of `onEmpty`, otherwise
  267. * it passes the array to `onNonEmpty` broken into its first element and remaining elements.
  268. *
  269. * @example
  270. * import { matchLeft } from 'fp-ts/Array'
  271. *
  272. * const len: <A>(as: Array<A>) => number = matchLeft(() => 0, (_, tail) => 1 + len(tail))
  273. * assert.strictEqual(len([1, 2, 3]), 3)
  274. *
  275. * @category pattern matching
  276. * @since 2.10.0
  277. */
  278. exports.matchLeft = exports.matchLeftW;
  279. /**
  280. * Alias of [`matchLeft`](#matchleft).
  281. *
  282. * @category pattern matching
  283. * @since 2.0.0
  284. */
  285. exports.foldLeft = exports.matchLeft;
  286. /**
  287. * Less strict version of [`matchRight`](#matchright). It will work when `onEmpty` and
  288. * `onNonEmpty` have different return types.
  289. *
  290. * @example
  291. * import { matchRightW } from 'fp-ts/Array'
  292. *
  293. * const f = matchRightW(
  294. * () => 0,
  295. * (head: string[], tail: string) => `Found ${head.length} elements folllowed by "${tail}"`
  296. * );
  297. * assert.strictEqual(f(["a", "b", "c"]), 'Found 2 elements folllowed by "c"');
  298. * assert.strictEqual(f([]), 0);
  299. *
  300. * @category pattern matching
  301. * @since 2.11.0
  302. */
  303. var matchRightW = function (onEmpty, onNonEmpty) {
  304. return function (as) {
  305. return (0, exports.isNonEmpty)(as) ? onNonEmpty(NEA.init(as), NEA.last(as)) : onEmpty();
  306. };
  307. };
  308. exports.matchRightW = matchRightW;
  309. /**
  310. * Takes an array, if the array is empty it returns the result of `onEmpty`, otherwise
  311. * it passes the array to `onNonEmpty` broken into its initial elements and the last element.
  312. *
  313. * @example
  314. * import { matchRight } from 'fp-ts/Array'
  315. *
  316. * const len: <A>(as: Array<A>) => number = matchRight(
  317. * () => 0,
  318. * (head, _) => 1 + len(head)
  319. * );
  320. * assert.strictEqual(len([1, 2, 3]), 3);
  321. *
  322. * @category pattern matching
  323. * @since 2.10.0
  324. */
  325. exports.matchRight = exports.matchRightW;
  326. /**
  327. * Alias of [`matchRight`](#matchright).
  328. *
  329. * @category pattern matching
  330. * @since 2.0.0
  331. */
  332. exports.foldRight = exports.matchRight;
  333. // -------------------------------------------------------------------------------------
  334. // combinators
  335. // -------------------------------------------------------------------------------------
  336. /**
  337. * Same as [`chain`](#chain), but passing also the index to the iterating function.
  338. *
  339. * @example
  340. * import { chainWithIndex, replicate } from 'fp-ts/Array'
  341. * import { pipe } from 'fp-ts/function'
  342. *
  343. * const f = (index: number, x: string) => replicate(2, `${x}${index}`);
  344. * assert.deepStrictEqual(pipe(["a", "b", "c"], chainWithIndex(f)), ["a0", "a0", "b1", "b1", "c2", "c2"]);
  345. *
  346. * @category sequencing
  347. * @since 2.7.0
  348. */
  349. var chainWithIndex = function (f) {
  350. return function (as) {
  351. var out = [];
  352. for (var i = 0; i < as.length; i++) {
  353. out.push.apply(out, f(i, as[i]));
  354. }
  355. return out;
  356. };
  357. };
  358. exports.chainWithIndex = chainWithIndex;
  359. /**
  360. * Same as `reduce` but it carries over the intermediate steps
  361. *
  362. * @example
  363. * import { scanLeft } from 'fp-ts/Array'
  364. *
  365. * assert.deepStrictEqual(scanLeft(10, (b, a: number) => b - a)([1, 2, 3]), [10, 9, 7, 4])
  366. *
  367. * @since 2.0.0
  368. */
  369. var scanLeft = function (b, f) {
  370. return function (as) {
  371. var len = as.length;
  372. var out = new Array(len + 1);
  373. out[0] = b;
  374. for (var i = 0; i < len; i++) {
  375. out[i + 1] = f(out[i], as[i]);
  376. }
  377. return out;
  378. };
  379. };
  380. exports.scanLeft = scanLeft;
  381. /**
  382. * Fold an array from the right, keeping all intermediate results instead of only the final result
  383. *
  384. * @example
  385. * import { scanRight } from 'fp-ts/Array'
  386. *
  387. * assert.deepStrictEqual(scanRight(10, (a: number, b) => b - a)([1, 2, 3]), [4, 5, 7, 10])
  388. *
  389. * @since 2.0.0
  390. */
  391. var scanRight = function (b, f) {
  392. return function (as) {
  393. var len = as.length;
  394. var out = new Array(len + 1);
  395. out[len] = b;
  396. for (var i = len - 1; i >= 0; i--) {
  397. out[i] = f(as[i], out[i + 1]);
  398. }
  399. return out;
  400. };
  401. };
  402. exports.scanRight = scanRight;
  403. /**
  404. * Calculate the number of elements in a `Array`.
  405. *
  406. * @example
  407. * import { size } from 'fp-ts/Array'
  408. *
  409. * assert.strictEqual(size(["a","b","c"]),3)
  410. *
  411. * @since 2.10.0
  412. */
  413. var size = function (as) { return as.length; };
  414. exports.size = size;
  415. /**
  416. * Test whether an array contains a particular index
  417. *
  418. * @example
  419. * import { isOutOfBound } from 'fp-ts/Array'
  420. *
  421. * assert.strictEqual(isOutOfBound(1,["a","b","c"]),false)
  422. * assert.strictEqual(isOutOfBound(-1,["a","b","c"]),true)
  423. * assert.strictEqual(isOutOfBound(3,["a","b","c"]),true)
  424. *
  425. * @since 2.0.0
  426. */
  427. exports.isOutOfBound = NEA.isOutOfBound;
  428. // TODO: remove non-curried overloading in v3
  429. /**
  430. * This function provides a safe way to read a value at a particular index from an array.
  431. * It returns a `none` if the index is out of bounds, and a `some` of the element if the
  432. * index is valid.
  433. *
  434. * @example
  435. * import { lookup } from 'fp-ts/Array'
  436. * import { some, none } from 'fp-ts/Option'
  437. * import { pipe } from 'fp-ts/function'
  438. *
  439. * assert.deepStrictEqual(pipe([1, 2, 3], lookup(1)), some(2))
  440. * assert.deepStrictEqual(pipe([1, 2, 3], lookup(3)), none)
  441. *
  442. * @since 2.0.0
  443. */
  444. exports.lookup = RA.lookup;
  445. /**
  446. * Get the first element in an array, or `None` if the array is empty
  447. *
  448. * @example
  449. * import { head } from 'fp-ts/Array'
  450. * import { some, none } from 'fp-ts/Option'
  451. *
  452. * assert.deepStrictEqual(head([1, 2, 3]), some(1))
  453. * assert.deepStrictEqual(head([]), none)
  454. *
  455. * @since 2.0.0
  456. */
  457. exports.head = RA.head;
  458. /**
  459. * Get the last element in an array, or `None` if the array is empty
  460. *
  461. * @example
  462. * import { last } from 'fp-ts/Array'
  463. * import { some, none } from 'fp-ts/Option'
  464. *
  465. * assert.deepStrictEqual(last([1, 2, 3]), some(3))
  466. * assert.deepStrictEqual(last([]), none)
  467. *
  468. * @since 2.0.0
  469. */
  470. exports.last = RA.last;
  471. /**
  472. * Get all but the first element of an array, creating a new array, or `None` if the array is empty
  473. *
  474. * @example
  475. * import { tail } from 'fp-ts/Array'
  476. * import { some, none } from 'fp-ts/Option'
  477. *
  478. * assert.deepStrictEqual(tail([1, 2, 3]), some([2, 3]))
  479. * assert.deepStrictEqual(tail([]), none)
  480. *
  481. * @since 2.0.0
  482. */
  483. var tail = function (as) { return ((0, exports.isNonEmpty)(as) ? _.some(NEA.tail(as)) : _.none); };
  484. exports.tail = tail;
  485. /**
  486. * Get all but the last element of an array, creating a new array, or `None` if the array is empty
  487. *
  488. * @example
  489. * import { init } from 'fp-ts/Array'
  490. * import { some, none } from 'fp-ts/Option'
  491. *
  492. * assert.deepStrictEqual(init([1, 2, 3]), some([1, 2]))
  493. * assert.deepStrictEqual(init([]), none)
  494. *
  495. * @since 2.0.0
  496. */
  497. var init = function (as) { return ((0, exports.isNonEmpty)(as) ? _.some(NEA.init(as)) : _.none); };
  498. exports.init = init;
  499. /**
  500. * Keep only a max number of elements from the start of an `Array`, creating a new `Array`.
  501. *
  502. * **Note**. `n` is normalized to a non negative integer.
  503. *
  504. * @example
  505. * import { takeLeft } from 'fp-ts/Array'
  506. *
  507. * assert.deepStrictEqual(takeLeft(2)([1, 2, 3, 4, 5]), [1, 2]);
  508. * assert.deepStrictEqual(takeLeft(7)([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]);
  509. * assert.deepStrictEqual(takeLeft(0)([1, 2, 3, 4, 5]), []);
  510. * assert.deepStrictEqual(takeLeft(-1)([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]);
  511. *
  512. * @since 2.0.0
  513. */
  514. var takeLeft = function (n) {
  515. return function (as) {
  516. return (0, exports.isOutOfBound)(n, as) ? (0, exports.copy)(as) : as.slice(0, n);
  517. };
  518. };
  519. exports.takeLeft = takeLeft;
  520. /**
  521. * Keep only a max number of elements from the end of an `Array`, creating a new `Array`.
  522. *
  523. * **Note**. `n` is normalized to a non negative integer.
  524. *
  525. * @example
  526. * import { takeRight } from 'fp-ts/Array'
  527. *
  528. * assert.deepStrictEqual(takeRight(2)([1, 2, 3, 4, 5]), [4, 5]);
  529. * assert.deepStrictEqual(takeRight(7)([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]);
  530. * assert.deepStrictEqual(takeRight(0)([1, 2, 3, 4, 5]), []);
  531. * assert.deepStrictEqual(takeRight(-1)([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]);
  532. *
  533. * @since 2.0.0
  534. */
  535. var takeRight = function (n) {
  536. return function (as) {
  537. return (0, exports.isOutOfBound)(n, as) ? (0, exports.copy)(as) : n === 0 ? [] : as.slice(-n);
  538. };
  539. };
  540. exports.takeRight = takeRight;
  541. function takeLeftWhile(predicate) {
  542. return function (as) {
  543. var out = [];
  544. for (var _i = 0, as_1 = as; _i < as_1.length; _i++) {
  545. var a = as_1[_i];
  546. if (!predicate(a)) {
  547. break;
  548. }
  549. out.push(a);
  550. }
  551. return out;
  552. };
  553. }
  554. exports.takeLeftWhile = takeLeftWhile;
  555. var spanLeftIndex = function (as, predicate) {
  556. var l = as.length;
  557. var i = 0;
  558. for (; i < l; i++) {
  559. if (!predicate(as[i])) {
  560. break;
  561. }
  562. }
  563. return i;
  564. };
  565. function spanLeft(predicate) {
  566. return function (as) {
  567. var _a = (0, exports.splitAt)(spanLeftIndex(as, predicate))(as), init = _a[0], rest = _a[1];
  568. return { init: init, rest: rest };
  569. };
  570. }
  571. exports.spanLeft = spanLeft;
  572. /**
  573. * Creates a new `Array` which is a copy of the input dropping a max number of elements from the start.
  574. *
  575. * **Note**. `n` is normalized to a non negative integer.
  576. *
  577. * @example
  578. * import { dropLeft } from 'fp-ts/Array'
  579. *
  580. * assert.deepStrictEqual(dropLeft(2)([1, 2, 3]), [3]);
  581. * assert.deepStrictEqual(dropLeft(5)([1, 2, 3]), []);
  582. * assert.deepStrictEqual(dropLeft(0)([1, 2, 3]), [1, 2, 3]);
  583. * assert.deepStrictEqual(dropLeft(-2)([1, 2, 3]), [1, 2, 3]);
  584. *
  585. * @since 2.0.0
  586. */
  587. var dropLeft = function (n) {
  588. return function (as) {
  589. return n <= 0 || (0, exports.isEmpty)(as) ? (0, exports.copy)(as) : n >= as.length ? [] : as.slice(n, as.length);
  590. };
  591. };
  592. exports.dropLeft = dropLeft;
  593. /**
  594. * Creates a new `Array` which is a copy of the input dropping a max number of elements from the end.
  595. *
  596. * **Note**. `n` is normalized to a non negative integer.
  597. *
  598. * @example
  599. * import { dropRight } from 'fp-ts/Array'
  600. *
  601. * assert.deepStrictEqual(dropRight(2)([1, 2, 3]), [1]);
  602. * assert.deepStrictEqual(dropRight(5)([1, 2, 3]), []);
  603. * assert.deepStrictEqual(dropRight(0)([1, 2, 3]), [1, 2, 3]);
  604. * assert.deepStrictEqual(dropRight(-2)([1, 2, 3]), [1, 2, 3]);
  605. *
  606. * @since 2.0.0
  607. */
  608. var dropRight = function (n) {
  609. return function (as) {
  610. return n <= 0 || (0, exports.isEmpty)(as) ? (0, exports.copy)(as) : n >= as.length ? [] : as.slice(0, as.length - n);
  611. };
  612. };
  613. exports.dropRight = dropRight;
  614. function dropLeftWhile(predicate) {
  615. return function (as) { return as.slice(spanLeftIndex(as, predicate)); };
  616. }
  617. exports.dropLeftWhile = dropLeftWhile;
  618. /**
  619. * `findIndex` returns an `Option` containing the first index for which a predicate holds.
  620. * It returns `None` if no element satisfies the predicate.
  621. * Similar to [`findFirst`](#findFirst) but returning the index instead of the element.
  622. *
  623. * @example
  624. * import { findIndex } from 'fp-ts/Array'
  625. * import { some, none } from 'fp-ts/Option'
  626. *
  627. * assert.deepStrictEqual(findIndex((n: number) => n === 2)([1, 2, 3]), some(1))
  628. * assert.deepStrictEqual(findIndex((n: number) => n === 2)([]), none)
  629. *
  630. * @since 2.0.0
  631. */
  632. exports.findIndex = RA.findIndex;
  633. function findFirst(predicate) {
  634. return RA.findFirst(predicate);
  635. }
  636. exports.findFirst = findFirst;
  637. /**
  638. * Given a selector function which takes an element and returns an option,
  639. * this function applies the selector to each element of the array and
  640. * returns the first `Some` result. Otherwise it returns `None`.
  641. *
  642. * @example
  643. * import { findFirstMap } from 'fp-ts/Array'
  644. * import { some, none } from 'fp-ts/Option'
  645. *
  646. * interface Person {
  647. * readonly name: string;
  648. * readonly age: number;
  649. * }
  650. *
  651. * const persons: Array<Person> = [
  652. * { name: "John", age: 16 },
  653. * { name: "Mary", age: 45 },
  654. * { name: "Joey", age: 28 },
  655. * ];
  656. *
  657. * const nameOfPersonAbove18 = (p: Person) => (p.age <= 18 ? none : some(p.name));
  658. * const nameOfPersonAbove70 = (p: Person) => (p.age <= 70 ? none : some(p.name));
  659. * assert.deepStrictEqual(findFirstMap(nameOfPersonAbove18)(persons), some("Mary"));
  660. * assert.deepStrictEqual(findFirstMap(nameOfPersonAbove70)(persons), none);
  661. *
  662. * @since 2.0.0
  663. */
  664. exports.findFirstMap = RA.findFirstMap;
  665. function findLast(predicate) {
  666. return RA.findLast(predicate);
  667. }
  668. exports.findLast = findLast;
  669. /**
  670. * Given a selector function which takes an element and returns an option,
  671. * this function applies the selector to each element of the array starting from the
  672. * end and returns the last `Some` result. Otherwise it returns `None`.
  673. *
  674. * @example
  675. * import { findLastMap } from 'fp-ts/Array'
  676. * import { some, none } from 'fp-ts/Option'
  677. *
  678. * interface Person {
  679. * readonly name: string;
  680. * readonly age: number;
  681. * }
  682. *
  683. * const persons: Array<Person> = [
  684. * { name: "John", age: 16 },
  685. * { name: "Mary", age: 45 },
  686. * { name: "Joey", age: 28 },
  687. * ];
  688. *
  689. * const nameOfPersonAbove18 = (p: Person) => (p.age <= 18 ? none : some(p.name));
  690. * const nameOfPersonAbove70 = (p: Person) => (p.age <= 70 ? none : some(p.name));
  691. * assert.deepStrictEqual(findLastMap(nameOfPersonAbove18)(persons), some("Joey"));
  692. * assert.deepStrictEqual(findLastMap(nameOfPersonAbove70)(persons), none);
  693. *
  694. * @since 2.0.0
  695. */
  696. exports.findLastMap = RA.findLastMap;
  697. /**
  698. * Returns the index of the last element of the list which matches the predicate.
  699. * It returns an `Option` containing the index or `None` if not found.
  700. *
  701. * @example
  702. * import { findLastIndex } from 'fp-ts/Array'
  703. * import { some, none } from 'fp-ts/Option'
  704. *
  705. * interface X {
  706. * readonly a: number
  707. * readonly b: number
  708. * }
  709. * const xs: Array<X> = [{ a: 1, b: 0 }, { a: 1, b: 1 }]
  710. * assert.deepStrictEqual(findLastIndex((x: { readonly a: number }) => x.a === 1)(xs), some(1))
  711. * assert.deepStrictEqual(findLastIndex((x: { readonly a: number }) => x.a === 4)(xs), none)
  712. *
  713. * @since 2.0.0
  714. */
  715. exports.findLastIndex = RA.findLastIndex;
  716. /**
  717. * This function takes an array and makes a new array containing the same elements.
  718. *
  719. * @since 2.0.0
  720. */
  721. var copy = function (as) { return as.slice(); };
  722. exports.copy = copy;
  723. /**
  724. * Insert an element at the specified index, creating a new array,
  725. * or returning `None` if the index is out of bounds.
  726. *
  727. * @example
  728. * import { insertAt } from 'fp-ts/Array'
  729. * import { some } from 'fp-ts/Option'
  730. *
  731. * assert.deepStrictEqual(insertAt(2, 5)([1, 2, 3, 4]), some([1, 2, 5, 3, 4]))
  732. *
  733. * @since 2.0.0
  734. */
  735. var insertAt = function (i, a) {
  736. return function (as) {
  737. return i < 0 || i > as.length ? _.none : _.some((0, exports.unsafeInsertAt)(i, a, as));
  738. };
  739. };
  740. exports.insertAt = insertAt;
  741. /**
  742. * Change the element at the specified index, creating a new array,
  743. * or returning `None` if the index is out of bounds.
  744. *
  745. * @example
  746. * import { updateAt } from 'fp-ts/Array'
  747. * import { some, none } from 'fp-ts/Option'
  748. *
  749. * assert.deepStrictEqual(updateAt(1, 1)([1, 2, 3]), some([1, 1, 3]))
  750. * assert.deepStrictEqual(updateAt(1, 1)([]), none)
  751. *
  752. * @since 2.0.0
  753. */
  754. var updateAt = function (i, a) { return (0, exports.modifyAt)(i, function () { return a; }); };
  755. exports.updateAt = updateAt;
  756. /**
  757. * Delete the element at the specified index, creating a new array, or returning `None` if the index is out of bounds.
  758. *
  759. * @example
  760. * import { deleteAt } from 'fp-ts/Array'
  761. * import { some, none } from 'fp-ts/Option'
  762. *
  763. * assert.deepStrictEqual(deleteAt(0)([1, 2, 3]), some([2, 3]))
  764. * assert.deepStrictEqual(deleteAt(1)([]), none)
  765. *
  766. * @since 2.0.0
  767. */
  768. var deleteAt = function (i) {
  769. return function (as) {
  770. return (0, exports.isOutOfBound)(i, as) ? _.none : _.some((0, exports.unsafeDeleteAt)(i, as));
  771. };
  772. };
  773. exports.deleteAt = deleteAt;
  774. /**
  775. * Apply a function to the element at the specified index, creating a new array, or returning `None` if the index is out
  776. * of bounds.
  777. *
  778. * @example
  779. * import { modifyAt } from 'fp-ts/Array'
  780. * import { some, none } from 'fp-ts/Option'
  781. *
  782. * const double = (x: number): number => x * 2
  783. * assert.deepStrictEqual(modifyAt(1, double)([1, 2, 3]), some([1, 4, 3]))
  784. * assert.deepStrictEqual(modifyAt(1, double)([]), none)
  785. *
  786. * @since 2.0.0
  787. */
  788. var modifyAt = function (i, f) {
  789. return function (as) {
  790. return (0, exports.isOutOfBound)(i, as) ? _.none : _.some((0, exports.unsafeUpdateAt)(i, f(as[i]), as));
  791. };
  792. };
  793. exports.modifyAt = modifyAt;
  794. /**
  795. * Reverse an array, creating a new array
  796. *
  797. * @example
  798. * import { reverse } from 'fp-ts/Array'
  799. *
  800. * assert.deepStrictEqual(reverse([1, 2, 3]), [3, 2, 1])
  801. *
  802. * @since 2.0.0
  803. */
  804. var reverse = function (as) { return ((0, exports.isEmpty)(as) ? [] : as.slice().reverse()); };
  805. exports.reverse = reverse;
  806. /**
  807. * Takes an `Array` of `Either` and produces a new `Array` containing
  808. * the values of all the `Right` elements in the same order.
  809. *
  810. * @example
  811. * import { rights } from 'fp-ts/Array'
  812. * import { right, left } from 'fp-ts/Either'
  813. *
  814. * assert.deepStrictEqual(rights([right(1), left('foo'), right(2)]), [1, 2])
  815. *
  816. * @since 2.0.0
  817. */
  818. var rights = function (as) {
  819. var r = [];
  820. for (var i = 0; i < as.length; i++) {
  821. var a = as[i];
  822. if (a._tag === 'Right') {
  823. r.push(a.right);
  824. }
  825. }
  826. return r;
  827. };
  828. exports.rights = rights;
  829. /**
  830. * Takes an `Array` of `Either` and produces a new `Array` containing
  831. * the values of all the `Left` elements in the same order.
  832. *
  833. * @example
  834. * import { lefts } from 'fp-ts/Array'
  835. * import { left, right } from 'fp-ts/Either'
  836. *
  837. * assert.deepStrictEqual(lefts([right(1), left('foo'), right(2)]), ['foo'])
  838. *
  839. * @since 2.0.0
  840. */
  841. var lefts = function (as) {
  842. var r = [];
  843. for (var i = 0; i < as.length; i++) {
  844. var a = as[i];
  845. if (a._tag === 'Left') {
  846. r.push(a.left);
  847. }
  848. }
  849. return r;
  850. };
  851. exports.lefts = lefts;
  852. /**
  853. * Sort the elements of an array in increasing order, creating a new array
  854. *
  855. * @example
  856. * import { sort } from 'fp-ts/Array'
  857. * import * as N from 'fp-ts/number'
  858. *
  859. * assert.deepStrictEqual(sort(N.Ord)([3, 2, 1]), [1, 2, 3])
  860. *
  861. * @since 2.0.0
  862. */
  863. var sort = function (O) {
  864. return function (as) {
  865. return as.length <= 1 ? (0, exports.copy)(as) : as.slice().sort(O.compare);
  866. };
  867. };
  868. exports.sort = sort;
  869. /**
  870. * Apply a function to pairs of elements at the same index in two arrays, collecting the results in a new array. If one
  871. * input array is short, excess elements of the longer array are discarded.
  872. *
  873. * @example
  874. * import { zipWith } from 'fp-ts/Array'
  875. *
  876. * assert.deepStrictEqual(zipWith([1, 2, 3], ['a', 'b', 'c', 'd'], (n, s) => s + n), ['a1', 'b2', 'c3'])
  877. *
  878. * @since 2.0.0
  879. */
  880. var zipWith = function (fa, fb, f) {
  881. var fc = [];
  882. var len = Math.min(fa.length, fb.length);
  883. for (var i = 0; i < len; i++) {
  884. fc[i] = f(fa[i], fb[i]);
  885. }
  886. return fc;
  887. };
  888. exports.zipWith = zipWith;
  889. function zip(as, bs) {
  890. if (bs === undefined) {
  891. return function (bs) { return zip(bs, as); };
  892. }
  893. return (0, exports.zipWith)(as, bs, function (a, b) { return [a, b]; });
  894. }
  895. exports.zip = zip;
  896. /**
  897. * The function is reverse of `zip`. Takes an array of pairs and return two corresponding arrays
  898. *
  899. * @example
  900. * import { unzip } from 'fp-ts/Array'
  901. *
  902. * assert.deepStrictEqual(unzip([[1, 'a'], [2, 'b'], [3, 'c']]), [[1, 2, 3], ['a', 'b', 'c']])
  903. *
  904. * @since 2.0.0
  905. */
  906. var unzip = function (as) {
  907. var fa = [];
  908. var fb = [];
  909. for (var i = 0; i < as.length; i++) {
  910. fa[i] = as[i][0];
  911. fb[i] = as[i][1];
  912. }
  913. return [fa, fb];
  914. };
  915. exports.unzip = unzip;
  916. /**
  917. * Creates a new `Array`, prepending an element to every member of the input `Array`.
  918. *
  919. * @example
  920. * import { prependAll } from 'fp-ts/Array'
  921. *
  922. * assert.deepStrictEqual(prependAll(9)([1, 2, 3, 4]), [9, 1, 9, 2, 9, 3, 9, 4])
  923. *
  924. * @since 2.10.0
  925. */
  926. var prependAll = function (middle) {
  927. var f = NEA.prependAll(middle);
  928. return function (as) { return ((0, exports.isNonEmpty)(as) ? f(as) : []); };
  929. };
  930. exports.prependAll = prependAll;
  931. /**
  932. * Creates a new `Array` placing an element in between members of the input `Array`.
  933. *
  934. * @example
  935. * import { intersperse } from 'fp-ts/Array'
  936. *
  937. * assert.deepStrictEqual(intersperse(9)([1, 2, 3, 4]), [1, 9, 2, 9, 3, 9, 4])
  938. *
  939. * @since 2.9.0
  940. */
  941. var intersperse = function (middle) {
  942. var f = NEA.intersperse(middle);
  943. return function (as) { return ((0, exports.isNonEmpty)(as) ? f(as) : (0, exports.copy)(as)); };
  944. };
  945. exports.intersperse = intersperse;
  946. /**
  947. * Creates a new `Array` rotating the input `Array` by `n` steps.
  948. *
  949. * @example
  950. * import { rotate } from 'fp-ts/Array'
  951. *
  952. * assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3])
  953. *
  954. * @since 2.0.0
  955. */
  956. var rotate = function (n) {
  957. var f = NEA.rotate(n);
  958. return function (as) { return ((0, exports.isNonEmpty)(as) ? f(as) : (0, exports.copy)(as)); };
  959. };
  960. exports.rotate = rotate;
  961. // TODO: remove non-curried overloading in v3
  962. /**
  963. * Test if a value is a member of an `Array`. Takes a `Eq<A>` as a single
  964. * argument which returns the function to use to search for a value of type `A` in
  965. * an `Array<A>`.
  966. *
  967. * @example
  968. * import { elem } from 'fp-ts/Array'
  969. * import * as N from 'fp-ts/number'
  970. * import { pipe } from 'fp-ts/function'
  971. *
  972. * assert.strictEqual(pipe([1, 2, 3], elem(N.Eq)(2)), true)
  973. * assert.strictEqual(pipe([1, 2, 3], elem(N.Eq)(0)), false)
  974. *
  975. * @since 2.0.0
  976. */
  977. exports.elem = RA.elem;
  978. /**
  979. * Creates a new `Array` removing duplicate elements, keeping the first occurrence of an element,
  980. * based on a `Eq<A>`.
  981. *
  982. * @example
  983. * import { uniq } from 'fp-ts/Array'
  984. * import * as N from 'fp-ts/number'
  985. *
  986. * assert.deepStrictEqual(uniq(N.Eq)([1, 2, 1]), [1, 2])
  987. *
  988. * @since 2.0.0
  989. */
  990. var uniq = function (E) {
  991. var f = NEA.uniq(E);
  992. return function (as) { return ((0, exports.isNonEmpty)(as) ? f(as) : (0, exports.copy)(as)); };
  993. };
  994. exports.uniq = uniq;
  995. /**
  996. * Sort the elements of an array in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`,
  997. * etc...
  998. *
  999. * @example
  1000. * import { sortBy } from 'fp-ts/Array'
  1001. * import { contramap } from 'fp-ts/Ord'
  1002. * import * as S from 'fp-ts/string'
  1003. * import * as N from 'fp-ts/number'
  1004. * import { pipe } from 'fp-ts/function'
  1005. *
  1006. * interface Person {
  1007. * readonly name: string
  1008. * readonly age: number
  1009. * }
  1010. * const byName = pipe(S.Ord, contramap((p: Person) => p.name))
  1011. * const byAge = pipe(N.Ord, contramap((p: Person) => p.age))
  1012. *
  1013. * const sortByNameByAge = sortBy([byName, byAge])
  1014. *
  1015. * const persons = [{ name: 'a', age: 1 }, { name: 'b', age: 3 }, { name: 'c', age: 2 }, { name: 'b', age: 2 }]
  1016. * assert.deepStrictEqual(sortByNameByAge(persons), [
  1017. * { name: 'a', age: 1 },
  1018. * { name: 'b', age: 2 },
  1019. * { name: 'b', age: 3 },
  1020. * { name: 'c', age: 2 }
  1021. * ])
  1022. *
  1023. * @since 2.0.0
  1024. */
  1025. var sortBy = function (ords) {
  1026. var f = NEA.sortBy(ords);
  1027. return function (as) { return ((0, exports.isNonEmpty)(as) ? f(as) : (0, exports.copy)(as)); };
  1028. };
  1029. exports.sortBy = sortBy;
  1030. /**
  1031. * A useful recursion pattern for processing an array to produce a new array, often used for "chopping" up the input
  1032. * array. Typically chop is called with some function that will consume an initial prefix of the array and produce a
  1033. * value and the rest of the array.
  1034. *
  1035. * @example
  1036. * import { Eq } from 'fp-ts/Eq'
  1037. * import * as A from 'fp-ts/Array'
  1038. * import * as N from 'fp-ts/number'
  1039. * import { pipe } from 'fp-ts/function'
  1040. *
  1041. * const group = <A>(S: Eq<A>): ((as: Array<A>) => Array<Array<A>>) => {
  1042. * return A.chop(as => {
  1043. * const { init, rest } = pipe(as, A.spanLeft((a: A) => S.equals(a, as[0])))
  1044. * return [init, rest]
  1045. * })
  1046. * }
  1047. * assert.deepStrictEqual(group(N.Eq)([1, 1, 2, 3, 3, 4]), [[1, 1], [2], [3, 3], [4]])
  1048. *
  1049. * @since 2.0.0
  1050. */
  1051. var chop = function (f) {
  1052. var g = NEA.chop(f);
  1053. return function (as) { return ((0, exports.isNonEmpty)(as) ? g(as) : []); };
  1054. };
  1055. exports.chop = chop;
  1056. /**
  1057. * Splits an `Array` into two pieces, the first piece has max `n` elements.
  1058. *
  1059. * @example
  1060. * import { splitAt } from 'fp-ts/Array'
  1061. *
  1062. * assert.deepStrictEqual(splitAt(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4, 5]])
  1063. *
  1064. * @since 2.0.0
  1065. */
  1066. var splitAt = function (n) {
  1067. return function (as) {
  1068. return n >= 1 && (0, exports.isNonEmpty)(as) ? NEA.splitAt(n)(as) : (0, exports.isEmpty)(as) ? [(0, exports.copy)(as), []] : [[], (0, exports.copy)(as)];
  1069. };
  1070. };
  1071. exports.splitAt = splitAt;
  1072. /**
  1073. * Splits an array into length-`n` pieces. The last piece will be shorter if `n` does not evenly divide the length of
  1074. * the array. Note that `chunksOf(n)([])` is `[]`, not `[[]]`. This is intentional, and is consistent with a recursive
  1075. * definition of `chunksOf`; it satisfies the property that
  1076. *
  1077. * ```ts
  1078. * chunksOf(n)(xs).concat(chunksOf(n)(ys)) == chunksOf(n)(xs.concat(ys)))
  1079. * ```
  1080. *
  1081. * whenever `n` evenly divides the length of `xs`.
  1082. *
  1083. * @example
  1084. * import { chunksOf } from 'fp-ts/Array'
  1085. *
  1086. * assert.deepStrictEqual(chunksOf(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4], [5]])
  1087. *
  1088. * @since 2.0.0
  1089. */
  1090. var chunksOf = function (n) {
  1091. var f = NEA.chunksOf(n);
  1092. return function (as) { return ((0, exports.isNonEmpty)(as) ? f(as) : []); };
  1093. };
  1094. exports.chunksOf = chunksOf;
  1095. /**
  1096. * @category lifting
  1097. * @since 2.11.0
  1098. */
  1099. var fromOptionK = function (f) {
  1100. return function () {
  1101. var a = [];
  1102. for (var _i = 0; _i < arguments.length; _i++) {
  1103. a[_i] = arguments[_i];
  1104. }
  1105. return (0, exports.fromOption)(f.apply(void 0, a));
  1106. };
  1107. };
  1108. exports.fromOptionK = fromOptionK;
  1109. function comprehension(input, f, g) {
  1110. if (g === void 0) { g = function () { return true; }; }
  1111. var go = function (scope, input) {
  1112. return (0, exports.isNonEmpty)(input)
  1113. ? (0, function_1.pipe)(NEA.head(input), (0, exports.chain)(function (x) { return go((0, function_1.pipe)(scope, (0, exports.append)(x)), NEA.tail(input)); }))
  1114. : g.apply(void 0, scope) ? [f.apply(void 0, scope)]
  1115. : [];
  1116. };
  1117. return go([], input);
  1118. }
  1119. exports.comprehension = comprehension;
  1120. /**
  1121. * @since 2.11.0
  1122. */
  1123. var concatW = function (second) {
  1124. return function (first) {
  1125. return (0, exports.isEmpty)(first) ? (0, exports.copy)(second) : (0, exports.isEmpty)(second) ? (0, exports.copy)(first) : first.concat(second);
  1126. };
  1127. };
  1128. exports.concatW = concatW;
  1129. /**
  1130. * @since 2.11.0
  1131. */
  1132. exports.concat = exports.concatW;
  1133. function union(E) {
  1134. var unionE = NEA.union(E);
  1135. return function (first, second) {
  1136. if (second === undefined) {
  1137. var unionE_1 = union(E);
  1138. return function (second) { return unionE_1(second, first); };
  1139. }
  1140. return (0, exports.isNonEmpty)(first) && (0, exports.isNonEmpty)(second)
  1141. ? unionE(second)(first)
  1142. : (0, exports.isNonEmpty)(first)
  1143. ? (0, exports.copy)(first)
  1144. : (0, exports.copy)(second);
  1145. };
  1146. }
  1147. exports.union = union;
  1148. function intersection(E) {
  1149. var elemE = (0, exports.elem)(E);
  1150. return function (xs, ys) {
  1151. if (ys === undefined) {
  1152. var intersectionE_1 = intersection(E);
  1153. return function (ys) { return intersectionE_1(ys, xs); };
  1154. }
  1155. return xs.filter(function (a) { return elemE(a, ys); });
  1156. };
  1157. }
  1158. exports.intersection = intersection;
  1159. function difference(E) {
  1160. var elemE = (0, exports.elem)(E);
  1161. return function (xs, ys) {
  1162. if (ys === undefined) {
  1163. var differenceE_1 = difference(E);
  1164. return function (ys) { return differenceE_1(ys, xs); };
  1165. }
  1166. return xs.filter(function (a) { return !elemE(a, ys); });
  1167. };
  1168. }
  1169. exports.difference = difference;
  1170. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
  1171. /* istanbul ignore next */
  1172. var _mapWithIndex = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.mapWithIndex)(f)); };
  1173. var _ap = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); };
  1174. var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
  1175. /* istanbul ignore next */
  1176. var _filter = function (fa, predicate) { return (0, function_1.pipe)(fa, (0, exports.filter)(predicate)); };
  1177. /* istanbul ignore next */
  1178. var _filterMap = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.filterMap)(f)); };
  1179. /* istanbul ignore next */
  1180. var _partition = function (fa, predicate) {
  1181. return (0, function_1.pipe)(fa, (0, exports.partition)(predicate));
  1182. };
  1183. /* istanbul ignore next */
  1184. var _partitionMap = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.partitionMap)(f)); };
  1185. /* istanbul ignore next */
  1186. var _partitionWithIndex = function (fa, predicateWithIndex) { return (0, function_1.pipe)(fa, (0, exports.partitionWithIndex)(predicateWithIndex)); };
  1187. /* istanbul ignore next */
  1188. var _partitionMapWithIndex = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.partitionMapWithIndex)(f)); };
  1189. /* istanbul ignore next */
  1190. var _alt = function (fa, that) { return (0, function_1.pipe)(fa, (0, exports.alt)(that)); };
  1191. var _reduce = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduce)(b, f)); };
  1192. /* istanbul ignore next */
  1193. var _foldMap = function (M) {
  1194. var foldMapM = (0, exports.foldMap)(M);
  1195. return function (fa, f) { return (0, function_1.pipe)(fa, foldMapM(f)); };
  1196. };
  1197. /* istanbul ignore next */
  1198. var _reduceRight = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduceRight)(b, f)); };
  1199. /* istanbul ignore next */
  1200. var _reduceWithIndex = function (fa, b, f) {
  1201. return (0, function_1.pipe)(fa, (0, exports.reduceWithIndex)(b, f));
  1202. };
  1203. /* istanbul ignore next */
  1204. var _foldMapWithIndex = function (M) {
  1205. var foldMapWithIndexM = (0, exports.foldMapWithIndex)(M);
  1206. return function (fa, f) { return (0, function_1.pipe)(fa, foldMapWithIndexM(f)); };
  1207. };
  1208. /* istanbul ignore next */
  1209. var _reduceRightWithIndex = function (fa, b, f) {
  1210. return (0, function_1.pipe)(fa, (0, exports.reduceRightWithIndex)(b, f));
  1211. };
  1212. /* istanbul ignore next */
  1213. var _filterMapWithIndex = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.filterMapWithIndex)(f)); };
  1214. /* istanbul ignore next */
  1215. var _filterWithIndex = function (fa, predicateWithIndex) { return (0, function_1.pipe)(fa, (0, exports.filterWithIndex)(predicateWithIndex)); };
  1216. /* istanbul ignore next */
  1217. var _extend = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.extend)(f)); };
  1218. /* istanbul ignore next */
  1219. var _traverse = function (F) {
  1220. var traverseF = (0, exports.traverse)(F);
  1221. return function (ta, f) { return (0, function_1.pipe)(ta, traverseF(f)); };
  1222. };
  1223. /* istanbul ignore next */
  1224. var _traverseWithIndex = function (F) {
  1225. var traverseWithIndexF = (0, exports.traverseWithIndex)(F);
  1226. return function (ta, f) { return (0, function_1.pipe)(ta, traverseWithIndexF(f)); };
  1227. };
  1228. var _chainRecDepthFirst = RA._chainRecDepthFirst;
  1229. var _chainRecBreadthFirst = RA._chainRecBreadthFirst;
  1230. /**
  1231. * Given an element of the base type, `of` builds an `Array` containing just that
  1232. * element of the base type (this is useful for building a `Monad`).
  1233. *
  1234. * @example
  1235. * import { of } from 'fp-ts/Array'
  1236. *
  1237. * assert.deepStrictEqual(of("a"), ["a"]);
  1238. *
  1239. * @category constructors
  1240. * @since 2.0.0
  1241. */
  1242. exports.of = NEA.of;
  1243. /**
  1244. * Makes an empty `Array`, useful for building a [`Monoid`](#Monoid)
  1245. *
  1246. * @since 2.7.0
  1247. */
  1248. var zero = function () { return []; };
  1249. exports.zero = zero;
  1250. /**
  1251. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: Array<A>) => Array<B>`.
  1252. * In practice it applies the base function to each element of the array and collects the
  1253. * results in a new array.
  1254. *
  1255. * @example
  1256. * import { map } from 'fp-ts/Array'
  1257. * import { pipe } from 'fp-ts/function'
  1258. *
  1259. * const f = (n: number) => n * 2;
  1260. * assert.deepStrictEqual(pipe([1, 2, 3], map(f)), [2, 4, 6]);
  1261. *
  1262. * @category mapping
  1263. * @since 2.0.0
  1264. */
  1265. var map = function (f) { return function (fa) { return fa.map(function (a) { return f(a); }); }; };
  1266. exports.map = map;
  1267. /**
  1268. * @example
  1269. * import { ap, map, of } from 'fp-ts/Array'
  1270. * import { pipe } from 'fp-ts/function'
  1271. *
  1272. * // a curried function with 3 input parameteres
  1273. * const f = (s1: string) => (n: number) => (s2: string) => s1 + n + s2;
  1274. *
  1275. * // let's use `ap` to iterate `f` over an array for each input parameter
  1276. * assert.deepStrictEqual(pipe(["a", "b"], map(f), ap([1, 2]), ap(["😀", "😫", "😎"])), [
  1277. * "a1😀", "a1😫", "a1😎",
  1278. * "a2😀", "a2😫", "a2😎",
  1279. * "b1😀", "b1😫", "b1😎",
  1280. * "b2😀", "b2😫", "b2😎",
  1281. * ]);
  1282. *
  1283. * // given Array implements the Applicative interface with the `of` method,
  1284. * // we can write exactly the same thing in a more symmetric way
  1285. * // using `of` on `f` and `ap` on each array in input
  1286. * assert.deepStrictEqual(
  1287. * pipe(of(f), ap(["a", "b"]), ap([1, 2]), ap(["😀", "😫", "😎"])),
  1288. * pipe(["a", "b"], map(f), ap([1, 2]), ap(["😀", "😫", "😎"]))
  1289. * );
  1290. *
  1291. * @since 2.0.0
  1292. */
  1293. var ap = function (fa) { return (0, exports.chain)(function (f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); }); };
  1294. exports.ap = ap;
  1295. /**
  1296. * Composes computations in sequence, using the return value of one computation to
  1297. * determine the next computation.
  1298. *
  1299. * In other words it takes a function `f` that produces an array from a single element of
  1300. * the base type `A` and returns a new function which applies `f` to each element of the
  1301. * input array (like [`map`](#map)) and, instead of returning an array of arrays, concatenates the
  1302. * results into a single array (like [`flatten`](#flatten)).
  1303. *
  1304. * This is the `chain` component of the array `Monad`.
  1305. *
  1306. * @example
  1307. * import { chain, map, replicate } from 'fp-ts/Array'
  1308. * import { pipe } from 'fp-ts/function'
  1309. *
  1310. * const f = (n: number) => replicate(n, `${n}`);
  1311. * assert.deepStrictEqual(pipe([1, 2, 3], map(f)), [["1"], ["2", "2"], ["3", "3", "3"]]);
  1312. * assert.deepStrictEqual(pipe([1, 2, 3], chain(f)), ["1", "2", "2", "3", "3", "3"]);
  1313. *
  1314. * @category sequencing
  1315. * @since 2.0.0
  1316. */
  1317. var chain = function (f) { return function (ma) {
  1318. return (0, function_1.pipe)(ma, (0, exports.chainWithIndex)(function (_, a) { return f(a); }));
  1319. }; };
  1320. exports.chain = chain;
  1321. /**
  1322. * Takes an array of arrays of `A` and flattens them into an array of `A`
  1323. * by concatenating the elements of each array in order.
  1324. *
  1325. * @example
  1326. * import { flatten } from 'fp-ts/Array'
  1327. *
  1328. * assert.deepStrictEqual(flatten([["a"], ["b", "c"], ["d", "e", "f"]]), ["a", "b", "c", "d", "e", "f"]);
  1329. *
  1330. * @category sequencing
  1331. * @since 2.5.0
  1332. */
  1333. exports.flatten = (0, exports.chain)(function_1.identity);
  1334. /**
  1335. * Same as [`map`](#map), but the iterating function takes both the index and the value
  1336. * of the element.
  1337. *
  1338. * @example
  1339. * import { mapWithIndex } from 'fp-ts/Array'
  1340. * import { pipe } from 'fp-ts/function'
  1341. *
  1342. * const f = (i: number, s: string) => `${s} - ${i}`;
  1343. * assert.deepStrictEqual(pipe(["a", "b", "c"], mapWithIndex(f)), ["a - 0", "b - 1", "c - 2"]);
  1344. *
  1345. * @category mapping
  1346. * @since 2.0.0
  1347. */
  1348. var mapWithIndex = function (f) { return function (fa) {
  1349. return fa.map(function (a, i) { return f(i, a); });
  1350. }; };
  1351. exports.mapWithIndex = mapWithIndex;
  1352. /**
  1353. * Maps an array with an iterating function that takes the index and the value of
  1354. * each element and returns an `Option`. It keeps only the `Some` values discarding
  1355. * the `None`s.
  1356. *
  1357. * Same as [`filterMap`](#filterMap), but with an iterating function which takes also
  1358. * the index as input.
  1359. *
  1360. * @example
  1361. * import { filterMapWithIndex } from 'fp-ts/Array'
  1362. * import { pipe } from 'fp-ts/function'
  1363. * import { option } from "fp-ts";
  1364. *
  1365. * const f = (i: number, s: string) => (i % 2 === 1 ? option.some(s.toUpperCase()) : option.none);
  1366. * assert.deepStrictEqual(pipe(["a", "no", "neither", "b"], filterMapWithIndex(f)), ["NO", "B"]);
  1367. *
  1368. * @category filtering
  1369. * @since 2.0.0
  1370. */
  1371. var filterMapWithIndex = function (f) {
  1372. return function (fa) {
  1373. var out = [];
  1374. for (var i = 0; i < fa.length; i++) {
  1375. var optionB = f(i, fa[i]);
  1376. if (_.isSome(optionB)) {
  1377. out.push(optionB.value);
  1378. }
  1379. }
  1380. return out;
  1381. };
  1382. };
  1383. exports.filterMapWithIndex = filterMapWithIndex;
  1384. /**
  1385. * Maps an array with an iterating function that returns an `Option`
  1386. * and it keeps only the `Some` values discarding the `None`s.
  1387. *
  1388. * @example
  1389. * import { filterMap } from 'fp-ts/Array'
  1390. * import { pipe } from 'fp-ts/function'
  1391. * import { option } from "fp-ts";
  1392. *
  1393. * const f = (s: string) => s.length === 1 ? option.some(s.toUpperCase()) : option.none;
  1394. * assert.deepStrictEqual(pipe(["a", "no", "neither", "b"], filterMap(f)), ["A", "B"]);
  1395. *
  1396. * @category filtering
  1397. * @since 2.0.0
  1398. */
  1399. var filterMap = function (f) {
  1400. return (0, exports.filterMapWithIndex)(function (_, a) { return f(a); });
  1401. };
  1402. exports.filterMap = filterMap;
  1403. /**
  1404. * Compact an array of `Option`s discarding the `None` values and
  1405. * keeping the `Some` values. It returns a new array containing the values of
  1406. * the `Some` options.
  1407. *
  1408. * @example
  1409. * import { compact } from 'fp-ts/Array'
  1410. * import { option } from "fp-ts";
  1411. *
  1412. * assert.deepStrictEqual(compact([option.some("a"), option.none, option.some("b")]), ["a", "b"]);
  1413. *
  1414. * @category filtering
  1415. * @since 2.0.0
  1416. */
  1417. exports.compact = (0, exports.filterMap)(function_1.identity);
  1418. /**
  1419. * Separate an array of `Either`s into `Left`s and `Right`s, creating two new arrays:
  1420. * one containing all the left values and one containing all the right values.
  1421. *
  1422. * @example
  1423. * import { separate } from 'fp-ts/Array'
  1424. * import { either } from "fp-ts";
  1425. *
  1426. * assert.deepStrictEqual(separate([either.right("r1"), either.left("l1"), either.right("r2")]), {
  1427. * left: ["l1"],
  1428. * right: ["r1", "r2"],
  1429. * });
  1430. *
  1431. * @category filtering
  1432. * @since 2.0.0
  1433. */
  1434. var separate = function (fa) {
  1435. var left = [];
  1436. var right = [];
  1437. for (var _i = 0, fa_1 = fa; _i < fa_1.length; _i++) {
  1438. var e = fa_1[_i];
  1439. if (e._tag === 'Left') {
  1440. left.push(e.left);
  1441. }
  1442. else {
  1443. right.push(e.right);
  1444. }
  1445. }
  1446. return (0, Separated_1.separated)(left, right);
  1447. };
  1448. exports.separate = separate;
  1449. /**
  1450. * Given an iterating function that is a `Predicate` or a `Refinement`,
  1451. * `filter` creates a new `Array` containing the elements of the original
  1452. * `Array` for which the iterating function is `true`.
  1453. *
  1454. * @example
  1455. * import { filter } from 'fp-ts/Array'
  1456. * import { isString } from "fp-ts/lib/string";
  1457. *
  1458. * assert.deepStrictEqual(filter(isString)(["a", 1, {}, "b", 5]), ["a", "b"]);
  1459. * assert.deepStrictEqual(filter((x:number) => x > 0)([-3, 1, -2, 5]), [1, 5]);
  1460. *
  1461. * @category filtering
  1462. * @since 2.0.0
  1463. */
  1464. var filter = function (predicate) {
  1465. return function (as) {
  1466. return as.filter(predicate);
  1467. };
  1468. };
  1469. exports.filter = filter;
  1470. /**
  1471. * Given an iterating function that is a `Predicate` or a `Refinement`,
  1472. * `partition` creates two new `Array`s: `right` containing the elements of the original
  1473. * `Array` for which the iterating function is `true`, `left` containing the elements
  1474. * for which it is false.
  1475. *
  1476. * @example
  1477. * import { partition } from 'fp-ts/Array'
  1478. * import { isString } from "fp-ts/lib/string";
  1479. *
  1480. * assert.deepStrictEqual(partition(isString)(["a", 1, {}, "b", 5]), { left: [1, {}, 5], right: ["a", "b"] });
  1481. * assert.deepStrictEqual(partition((x: number) => x > 0)([-3, 1, -2, 5]), { left: [-3, -2], right: [1, 5] });
  1482. *
  1483. * @category filtering
  1484. * @since 2.0.0
  1485. */
  1486. var partition = function (predicate) {
  1487. return (0, exports.partitionWithIndex)(function (_, a) { return predicate(a); });
  1488. };
  1489. exports.partition = partition;
  1490. /**
  1491. * Same as [`partition`](#partition), but passing also the index to the iterating function.
  1492. *
  1493. * @example
  1494. * import { partitionWithIndex } from 'fp-ts/Array'
  1495. *
  1496. * assert.deepStrictEqual(partitionWithIndex((index, x: number) => index < 3 && x > 0)([-2, 5, 6, 7]), {
  1497. * left: [-2, 7],
  1498. * right: [5, 6],
  1499. * });
  1500. *
  1501. * @category filtering
  1502. * @since 2.0.0
  1503. */
  1504. var partitionWithIndex = function (predicateWithIndex) {
  1505. return function (as) {
  1506. var left = [];
  1507. var right = [];
  1508. for (var i = 0; i < as.length; i++) {
  1509. var b = as[i];
  1510. if (predicateWithIndex(i, b)) {
  1511. right.push(b);
  1512. }
  1513. else {
  1514. left.push(b);
  1515. }
  1516. }
  1517. return (0, Separated_1.separated)(left, right);
  1518. };
  1519. };
  1520. exports.partitionWithIndex = partitionWithIndex;
  1521. /**
  1522. * Given an iterating function that returns an `Either`,
  1523. * `partitionMap` applies the iterating function to each element and it creates two `Array`s:
  1524. * `right` containing the values of `Right` results, `left` containing the values of `Left` results.
  1525. *
  1526. * @example
  1527. * import { partitionMap } from 'fp-ts/Array'
  1528. * import { Either, left, right } from "fp-ts/lib/Either";
  1529. *
  1530. * const upperIfString = <B>(x: B): Either<B, string> =>
  1531. * typeof x === "string" ? right(x.toUpperCase()) : left(x);
  1532. * assert.deepStrictEqual(partitionMap(upperIfString)([-2, "hello", 6, 7, "world"]), {
  1533. * left: [-2, 6, 7],
  1534. * right: [ 'HELLO', 'WORLD' ],
  1535. * });
  1536. *
  1537. * @category filtering
  1538. * @since 2.0.0
  1539. */
  1540. var partitionMap = function (f) { return (0, exports.partitionMapWithIndex)(function (_, a) { return f(a); }); };
  1541. exports.partitionMap = partitionMap;
  1542. /**
  1543. * Same as [`partitionMap`](#partitionMap), but passing also the index to the iterating function.
  1544. *
  1545. * @example
  1546. * import { partitionMapWithIndex } from 'fp-ts/Array'
  1547. * import { Either, left, right } from "fp-ts/lib/Either";
  1548. *
  1549. * const upperIfStringBefore3 = <B>(index: number, x: B): Either<B, string> =>
  1550. * index < 3 && typeof x === "string" ? right(x.toUpperCase()) : left(x);
  1551. * assert.deepStrictEqual(partitionMapWithIndex(upperIfStringBefore3)([-2, "hello", 6, 7, "world"]), {
  1552. * left: [-2, 6, 7, "world"],
  1553. * right: ["HELLO"],
  1554. * });
  1555. *
  1556. * @category filtering
  1557. * @since 2.0.0
  1558. */
  1559. var partitionMapWithIndex = function (f) {
  1560. return function (fa) {
  1561. var left = [];
  1562. var right = [];
  1563. for (var i = 0; i < fa.length; i++) {
  1564. var e = f(i, fa[i]);
  1565. if (e._tag === 'Left') {
  1566. left.push(e.left);
  1567. }
  1568. else {
  1569. right.push(e.right);
  1570. }
  1571. }
  1572. return (0, Separated_1.separated)(left, right);
  1573. };
  1574. };
  1575. exports.partitionMapWithIndex = partitionMapWithIndex;
  1576. /**
  1577. * Less strict version of [`alt`](#alt).
  1578. *
  1579. * The `W` suffix (short for **W**idening) means that the return types will be merged.
  1580. *
  1581. * @example
  1582. * import * as A from 'fp-ts/Array'
  1583. * import { pipe } from 'fp-ts/function'
  1584. *
  1585. * assert.deepStrictEqual(
  1586. * pipe(
  1587. * [1, 2, 3],
  1588. * A.altW(() => ['a', 'b'])
  1589. * ),
  1590. * [1, 2, 3, 'a', 'b']
  1591. * )
  1592. *
  1593. * @category error handling
  1594. * @since 2.9.0
  1595. */
  1596. var altW = function (that) {
  1597. return function (fa) {
  1598. return fa.concat(that());
  1599. };
  1600. };
  1601. exports.altW = altW;
  1602. /**
  1603. * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
  1604. * types of kind `* -> *`.
  1605. *
  1606. * In case of `Array` concatenates the inputs into a single array.
  1607. *
  1608. * @example
  1609. * import * as A from 'fp-ts/Array'
  1610. * import { pipe } from 'fp-ts/function'
  1611. *
  1612. * assert.deepStrictEqual(
  1613. * pipe(
  1614. * [1, 2, 3],
  1615. * A.alt(() => [4, 5])
  1616. * ),
  1617. * [1, 2, 3, 4, 5]
  1618. * )
  1619. *
  1620. * @category error handling
  1621. * @since 2.0.0
  1622. */
  1623. exports.alt = exports.altW;
  1624. /**
  1625. * Same as [`filter`](#filter), but passing also the index to the iterating function.
  1626. *
  1627. * @example
  1628. * import { filterWithIndex } from 'fp-ts/Array';
  1629. *
  1630. * const f = (index: number, x: number) => x > 0 && index <= 2;
  1631. * assert.deepStrictEqual(filterWithIndex(f)([-3, 1, -2, 5]), [1]);
  1632. *
  1633. * @category filtering
  1634. * @since 2.0.0
  1635. */
  1636. var filterWithIndex = function (predicateWithIndex) {
  1637. return function (as) {
  1638. return as.filter(function (b, i) { return predicateWithIndex(i, b); });
  1639. };
  1640. };
  1641. exports.filterWithIndex = filterWithIndex;
  1642. /**
  1643. * Given an iterating function that takes `Array<A>` as input, `extend` returns
  1644. * an array containing the results of the iterating function applied to the whole input
  1645. * `Array`, then to the input `Array` without the first element, then to the input
  1646. * `Array` without the first two elements, etc.
  1647. *
  1648. * @example
  1649. * import { extend } from 'fp-ts/Array'
  1650. *
  1651. * const f = (a: string[]) => a.join(",");
  1652. * assert.deepStrictEqual(extend(f)(["a", "b", "c"]), ["a,b,c", "b,c", "c"]);
  1653. *
  1654. * @since 2.0.0
  1655. */
  1656. var extend = function (f) { return function (wa) {
  1657. return wa.map(function (_, i) { return f(wa.slice(i)); });
  1658. }; };
  1659. exports.extend = extend;
  1660. /**
  1661. * `duplicate` returns an array containing the whole input `Array`,
  1662. * then to the input `Array` dropping the first element, then to the input
  1663. * `Array` dropping the first two elements, etc.
  1664. *
  1665. * @example
  1666. * import { duplicate } from 'fp-ts/Array'
  1667. *
  1668. * assert.deepStrictEqual(duplicate(["a", "b", "c"]), [["a", "b", "c"], ["b", "c"], ["c"]]);
  1669. *
  1670. * @since 2.0.0
  1671. */
  1672. exports.duplicate = (0, exports.extend)(function_1.identity);
  1673. /**
  1674. * Map and fold an `Array`.
  1675. * Map the `Array` passing each value to the iterating function.
  1676. * Then fold the results using the provided `Monoid`.
  1677. *
  1678. * @example
  1679. * import { foldMap } from 'fp-ts/Array'
  1680. *
  1681. * const monoid = { concat: (a: string, b: string) => a + b, empty: "" };
  1682. * const f = (s: string) => s.toUpperCase()
  1683. * assert.deepStrictEqual(foldMap(monoid)(f)(["a", "b", "c"]), "ABC");
  1684. *
  1685. * @category folding
  1686. * @since 2.0.0
  1687. */
  1688. exports.foldMap = RA.foldMap;
  1689. /**
  1690. * Same as [`foldMap`](#foldMap) but passing also the index to the iterating function.
  1691. *
  1692. * @example
  1693. * import { foldMapWithIndex } from 'fp-ts/Array'
  1694. *
  1695. * const monoid = { concat: (a: string, b: string) => a + b, empty: "" };
  1696. * const f = (index:number, s: string) => `${s.toUpperCase()}(${index})`
  1697. * assert.deepStrictEqual(foldMapWithIndex(monoid)(f)(["a", "b", "c"]), "A(0)B(1)C(2)");
  1698. *
  1699. * @category folding
  1700. * @since 2.0.0
  1701. */
  1702. exports.foldMapWithIndex = RA.foldMapWithIndex;
  1703. /**
  1704. * Reduces an `Array`.
  1705. *
  1706. * `reduce` executes the supplied iterating function on each element of the array,
  1707. * in order, passing in the element and the return value from the calculation on the preceding element.
  1708. *
  1709. * The first time that the iterating function is called there is no "return value of the
  1710. * previous calculation", the initial value is used in its place.
  1711. *
  1712. * @example
  1713. * import { reduce } from 'fp-ts/Array'
  1714. *
  1715. * assert.deepStrictEqual(reduce(5, (acc: number, cur: number) => acc * cur)([2, 3]), 5 * 2 * 3);
  1716. *
  1717. * @category folding
  1718. * @since 2.0.0
  1719. */
  1720. exports.reduce = RA.reduce;
  1721. /**
  1722. * Same as [`reduce`](#reduce) but passing also the index to the iterating function.
  1723. *
  1724. * @example
  1725. * import { reduceWithIndex } from 'fp-ts/Array'
  1726. *
  1727. * const f = (index: number, acc: string, cur: unknown) =>
  1728. * acc + (typeof cur === "string" ? cur.toUpperCase() + index : "");
  1729. * assert.deepStrictEqual(reduceWithIndex("", f)([2, "a", "b", null]), "A1B2");
  1730. *
  1731. * @category folding
  1732. * @since 2.0.0
  1733. */
  1734. exports.reduceWithIndex = RA.reduceWithIndex;
  1735. /**
  1736. * Same as [`reduce`](#reduce) but applied from the end to the start.
  1737. *
  1738. * *Note*: the iterating function in this case takes the accumulator as the last argument.
  1739. *
  1740. * @example
  1741. * import { reduceRight } from 'fp-ts/Array'
  1742. *
  1743. * assert.deepStrictEqual(reduceRight("", (cur: string, acc: string) => acc + cur)(["a", "b", "c"]), "cba");
  1744. *
  1745. * @category folding
  1746. * @since 2.0.0
  1747. */
  1748. exports.reduceRight = RA.reduceRight;
  1749. /**
  1750. * Same as [`reduceRight`](#reduceRight) but passing also the index to the iterating function.
  1751. *
  1752. * @example
  1753. * import { reduceRightWithIndex } from 'fp-ts/Array'
  1754. *
  1755. * const f = (index: number, cur: unknown, acc: string) =>
  1756. * acc + (typeof cur === "string" ? cur.toUpperCase() + index : "");
  1757. * assert.deepStrictEqual(reduceRightWithIndex("", f)([2, "a", "b", null]), "B2A1");
  1758. *
  1759. * @category folding
  1760. * @since 2.0.0
  1761. */
  1762. exports.reduceRightWithIndex = RA.reduceRightWithIndex;
  1763. /**
  1764. * Given an iterating function that returns a `HKT` (higher kinded type), `traverse`
  1765. * applies the iterating function to each element of the `Array` and then [`sequence`](#sequence)-s
  1766. * the results using the provided `Applicative`.
  1767. *
  1768. * E.g. suppose you have an `Array` and you want to format each element with a function
  1769. * that returns a result or an error as `f = (a: A) => Either<Error, B>`, using `traverse`
  1770. * you can apply `f` to all elements and directly obtain as a result an `Either<Error,Array<B>>`
  1771. * i.e. an `Array<B>` if all the results are `B`, or an `Error` if some of the results
  1772. * are `Error`s.
  1773. *
  1774. * @example
  1775. * import { traverse } from 'fp-ts/Array'
  1776. * import { Applicative, left, right } from "fp-ts/lib/Either";
  1777. *
  1778. * const f = (x: unknown) =>
  1779. * typeof x === "string" ? right(x.toUpperCase()) : left(new Error("not a string"));
  1780. * assert.deepStrictEqual(traverse(Applicative)(f)(["a", "b"]), right(["A", "B"]));
  1781. * assert.deepStrictEqual(traverse(Applicative)(f)(["a", 5]), left(new Error("not a string")));
  1782. *
  1783. * @category traversing
  1784. * @since 2.6.3
  1785. */
  1786. var traverse = function (F) {
  1787. var traverseWithIndexF = (0, exports.traverseWithIndex)(F);
  1788. return function (f) { return traverseWithIndexF(function (_, a) { return f(a); }); };
  1789. };
  1790. exports.traverse = traverse;
  1791. /**
  1792. * `sequence` takes an `Array` where elements are `HKT<A>` (higher kinded type) and,
  1793. * using an applicative of that `HKT`, returns an `HKT` of `Array<A>`.
  1794. * E.g. it can turn an `Array<Either<Error, string>>` into an `Either<Error, Array<string>>`.
  1795. *
  1796. * `sequence` requires an `Applicative` of the `HKT` you are targeting, e.g. to turn an
  1797. * `Array<Either<E, A>>` into an `Either<E, Array<A>>`, it needs an
  1798. * `Applicative` for `Either`, to to turn an `Array<Option<A>>` into an `Option<Array<A>>`,
  1799. * it needs an `Applicative` for `Option`.
  1800. *
  1801. * @example
  1802. * import { sequence } from 'fp-ts/Array'
  1803. * import { Applicative, left, right } from "fp-ts/lib/Either";
  1804. *
  1805. * assert.deepStrictEqual(sequence(Applicative)([right("a"), right("b")]), right(["a", "b"]));
  1806. * assert.deepStrictEqual(
  1807. * sequence(Applicative)([right("a"), left(new Error("not a string"))]),
  1808. * left(new Error("not a string"))
  1809. * );
  1810. *
  1811. * @category traversing
  1812. * @since 2.6.3
  1813. */
  1814. var sequence = function (F) {
  1815. return function (ta) {
  1816. return _reduce(ta, F.of((0, exports.zero)()), function (fas, fa) {
  1817. return F.ap(F.map(fas, function (as) { return function (a) { return (0, function_1.pipe)(as, (0, exports.append)(a)); }; }), fa);
  1818. });
  1819. };
  1820. };
  1821. exports.sequence = sequence;
  1822. /**
  1823. * Same as [`traverse`](#traverse) but passing also the index to the iterating function.
  1824. *
  1825. * @example
  1826. * import { traverseWithIndex } from 'fp-ts/Array'
  1827. * import { Applicative, left, right } from "fp-ts/lib/Either";
  1828. *
  1829. * const f = (index:number, x:unknown) =>
  1830. * typeof x === "string" ? right(x.toUpperCase() + index) : left(new Error("not a string"));
  1831. * assert.deepStrictEqual(traverseWithIndex(Applicative)(f)(["a", "b"]), right(["A0", "B1"]));
  1832. * assert.deepStrictEqual(traverseWithIndex(Applicative)(f)(["a", 5]), left(new Error("not a string")));
  1833. *
  1834. * @category sequencing
  1835. * @since 2.6.3
  1836. */
  1837. var traverseWithIndex = function (F) {
  1838. return function (f) {
  1839. return (0, exports.reduceWithIndex)(F.of((0, exports.zero)()), function (i, fbs, a) {
  1840. return F.ap(F.map(fbs, function (bs) { return function (b) { return (0, function_1.pipe)(bs, (0, exports.append)(b)); }; }), f(i, a));
  1841. });
  1842. };
  1843. };
  1844. exports.traverseWithIndex = traverseWithIndex;
  1845. /**
  1846. * @category filtering
  1847. * @since 2.6.5
  1848. */
  1849. var wither = function (F) {
  1850. var _witherF = _wither(F);
  1851. return function (f) { return function (fa) { return _witherF(fa, f); }; };
  1852. };
  1853. exports.wither = wither;
  1854. /**
  1855. * @category filtering
  1856. * @since 2.6.5
  1857. */
  1858. var wilt = function (F) {
  1859. var _wiltF = _wilt(F);
  1860. return function (f) { return function (fa) { return _wiltF(fa, f); }; };
  1861. };
  1862. exports.wilt = wilt;
  1863. /**
  1864. * `unfold` takes a function `f` which returns an `Option` of a tuple containing an outcome
  1865. * value and an input for the following iteration.
  1866. * `unfold` applies `f` to the initial value `b` and then recursively to the second
  1867. * element of the tuple contained in the returned `option` of the previous
  1868. * calculation until `f` returns `Option.none`.
  1869. *
  1870. * @example
  1871. * import { unfold } from 'fp-ts/Array'
  1872. * import { option } from 'fp-ts'
  1873. *
  1874. * const f = (n: number) => {
  1875. * if (n <= 0) return option.none;
  1876. * const returnValue = n * 2;
  1877. * const inputForNextRound = n - 1;
  1878. * return option.some([returnValue, inputForNextRound] as const);
  1879. * };
  1880. * assert.deepStrictEqual(unfold(5, f), [10, 8, 6, 4, 2]);
  1881. *
  1882. * @since 2.6.6
  1883. */
  1884. var unfold = function (b, f) {
  1885. var out = [];
  1886. var bb = b;
  1887. // eslint-disable-next-line no-constant-condition
  1888. while (true) {
  1889. var mt = f(bb);
  1890. if (_.isSome(mt)) {
  1891. var _a = mt.value, a = _a[0], b_1 = _a[1];
  1892. out.push(a);
  1893. bb = b_1;
  1894. }
  1895. else {
  1896. break;
  1897. }
  1898. }
  1899. return out;
  1900. };
  1901. exports.unfold = unfold;
  1902. /**
  1903. * @category type lambdas
  1904. * @since 2.0.0
  1905. */
  1906. exports.URI = 'Array';
  1907. /**
  1908. * `getShow` makes a `Show` for an `Array<A>` from a `Show` for
  1909. * an `A`.
  1910. *
  1911. * @example
  1912. * import { getShow } from 'fp-ts/Array'
  1913. *
  1914. * const numShow = { show: (n: number) => (n >= 0 ? `${n}` : `(${-n})`) };
  1915. * assert.deepStrictEqual(getShow(numShow).show([-2, -1, 0, 1]), "[(2), (1), 0, 1]");
  1916. *
  1917. * @category instances
  1918. * @since 2.0.0
  1919. */
  1920. exports.getShow = RA.getShow;
  1921. /**
  1922. * Get a `Semigroup` based on the concatenation of `Array`s.
  1923. * See also [`getMonoid`](#getMonoid).
  1924. *
  1925. * @example
  1926. * import { getSemigroup } from 'fp-ts/Array'
  1927. *
  1928. * const S = getSemigroup<number>();
  1929. * assert.deepStrictEqual(S.concat([1, 2], [2, 3]), [1, 2, 2, 3]);
  1930. *
  1931. * @category instances
  1932. * @since 2.10.0
  1933. */
  1934. var getSemigroup = function () { return ({
  1935. concat: function (first, second) { return first.concat(second); }
  1936. }); };
  1937. exports.getSemigroup = getSemigroup;
  1938. /**
  1939. * Returns a `Monoid` for `Array<A>` based on the concatenation of `Array`s.
  1940. *
  1941. * @example
  1942. * import { getMonoid } from 'fp-ts/Array'
  1943. *
  1944. * const M = getMonoid<number>()
  1945. * assert.deepStrictEqual(M.concat([1, 2], [3, 4]), [1, 2, 3, 4])
  1946. *
  1947. * @category instances
  1948. * @since 2.0.0
  1949. */
  1950. var getMonoid = function () { return ({
  1951. concat: (0, exports.getSemigroup)().concat,
  1952. empty: []
  1953. }); };
  1954. exports.getMonoid = getMonoid;
  1955. /**
  1956. * Derives an `Eq` over the `Array` of a given element type from the `Eq` of that type. The derived `Eq` defines two
  1957. * arrays as equal if all elements of both arrays are compared equal pairwise with the given `E`. In case of arrays of
  1958. * different lengths, the result is non equality.
  1959. *
  1960. * @example
  1961. * import * as S from 'fp-ts/string'
  1962. * import { getEq } from 'fp-ts/Array'
  1963. *
  1964. * const E = getEq(S.Eq)
  1965. * assert.strictEqual(E.equals(['a', 'b'], ['a', 'b']), true)
  1966. * assert.strictEqual(E.equals(['a'], []), false)
  1967. *
  1968. * @category instances
  1969. * @since 2.0.0
  1970. */
  1971. exports.getEq = RA.getEq;
  1972. /**
  1973. * Derives an `Ord` over the `Array` of a given element type from the `Ord` of that type. The ordering between two such
  1974. * arrays is equal to: the first non equal comparison of each arrays elements taken pairwise in increasing order, in
  1975. * case of equality over all the pairwise elements; the longest array is considered the greatest, if both arrays have
  1976. * the same length, the result is equality.
  1977. *
  1978. * @example
  1979. * import { getOrd } from 'fp-ts/Array'
  1980. * import * as S from 'fp-ts/string'
  1981. *
  1982. * const O = getOrd(S.Ord)
  1983. * assert.strictEqual(O.compare(['b'], ['a']), 1)
  1984. * assert.strictEqual(O.compare(['a'], ['a']), 0)
  1985. * assert.strictEqual(O.compare(['a'], ['b']), -1)
  1986. *
  1987. * @category instances
  1988. * @since 2.0.0
  1989. */
  1990. exports.getOrd = RA.getOrd;
  1991. /**
  1992. * Get a `Semigroup` based on the union of the elements of `Array`s.
  1993. * Elements which equal according to the provided `Eq` are included
  1994. * only once in the result.
  1995. * See also [`getUnionMonoid`](#getUnionMonoid).
  1996. *
  1997. * @example
  1998. * import { getUnionSemigroup } from 'fp-ts/Array';
  1999. * import { Eq } from 'fp-ts/number';
  2000. *
  2001. * const S = getUnionSemigroup<number>(Eq);
  2002. * assert.deepStrictEqual(S.concat([1, 2], [2, 3]), [1, 2, 3]);
  2003. *
  2004. * @category instances
  2005. * @since 2.11.0
  2006. */
  2007. var getUnionSemigroup = function (E) {
  2008. var unionE = union(E);
  2009. return {
  2010. concat: function (first, second) { return unionE(second)(first); }
  2011. };
  2012. };
  2013. exports.getUnionSemigroup = getUnionSemigroup;
  2014. /**
  2015. * Get a `Monoid` based on the union of the elements of `Array`s.
  2016. * Elements which equal according to the provided `Eq` are included
  2017. * only once in the result.
  2018. *
  2019. * @example
  2020. * import { getUnionMonoid } from 'fp-ts/Array'
  2021. * import { Eq } from 'fp-ts/number';
  2022. *
  2023. * const M = getUnionMonoid<number>(Eq);
  2024. * assert.deepStrictEqual(M.concat([1, 2], [2, 3]), [1, 2, 3]);
  2025. * assert.deepStrictEqual(M.empty,[]);
  2026. *
  2027. * @category instances
  2028. * @since 2.11.0
  2029. */
  2030. var getUnionMonoid = function (E) { return ({
  2031. concat: (0, exports.getUnionSemigroup)(E).concat,
  2032. empty: []
  2033. }); };
  2034. exports.getUnionMonoid = getUnionMonoid;
  2035. /**
  2036. * Get a `Semigroup` based on the intersection of the elements of `Array`s.
  2037. * Only elements present in the two arrays which are equal according to the
  2038. * provided `Eq` are included in the result.
  2039. *
  2040. * @example
  2041. * import { getIntersectionSemigroup } from 'fp-ts/Array'
  2042. * import { Eq } from 'fp-ts/number';
  2043. *
  2044. * const S = getIntersectionSemigroup<number>(Eq);
  2045. * assert.deepStrictEqual(S.concat([1, 2], [2, 3]), [2]);
  2046. *
  2047. * @category instances
  2048. * @since 2.11.0
  2049. */
  2050. var getIntersectionSemigroup = function (E) {
  2051. var intersectionE = intersection(E);
  2052. return {
  2053. concat: function (first, second) { return intersectionE(second)(first); }
  2054. };
  2055. };
  2056. exports.getIntersectionSemigroup = getIntersectionSemigroup;
  2057. /**
  2058. * Get a `Magma` for `Array` where the `concat` function is the differnce between
  2059. * the first and the second array, i.e. the result contains all the elements of the
  2060. * first array for which their is no equal element in the second array according
  2061. * to the `Eq` provided.
  2062. *
  2063. *
  2064. * @example
  2065. * import { getDifferenceMagma } from 'fp-ts/Array'
  2066. * import { Eq } from 'fp-ts/number';
  2067. *
  2068. * const S = getDifferenceMagma<number>(Eq);
  2069. * assert.deepStrictEqual(S.concat([1, 2], [2, 3]), [1]);
  2070. *
  2071. * @category instances
  2072. * @since 2.11.0
  2073. */
  2074. var getDifferenceMagma = function (E) {
  2075. var differenceE = difference(E);
  2076. return {
  2077. concat: function (first, second) { return differenceE(second)(first); }
  2078. };
  2079. };
  2080. exports.getDifferenceMagma = getDifferenceMagma;
  2081. /**
  2082. * @category instances
  2083. * @since 2.7.0
  2084. */
  2085. exports.Functor = {
  2086. URI: exports.URI,
  2087. map: _map
  2088. };
  2089. /**
  2090. * Given an input an `Array` of functions, `flap` returns an `Array` containing
  2091. * the results of applying each function to the given input.
  2092. *
  2093. * @example
  2094. * import { flap } from 'fp-ts/Array'
  2095. *
  2096. * const funs = [
  2097. * (n: number) => `Double: ${n * 2}`,
  2098. * (n: number) => `Triple: ${n * 3}`,
  2099. * (n: number) => `Square: ${n * n}`,
  2100. * ];
  2101. * assert.deepStrictEqual(flap(4)(funs), ['Double: 8', 'Triple: 12', 'Square: 16']);
  2102. *
  2103. * @category mapping
  2104. * @since 2.10.0
  2105. */
  2106. exports.flap = (0, Functor_1.flap)(exports.Functor);
  2107. /**
  2108. * @category instances
  2109. * @since 2.10.0
  2110. */
  2111. exports.Pointed = {
  2112. URI: exports.URI,
  2113. of: exports.of
  2114. };
  2115. /**
  2116. * @category instances
  2117. * @since 2.7.0
  2118. */
  2119. exports.FunctorWithIndex = {
  2120. URI: exports.URI,
  2121. map: _map,
  2122. mapWithIndex: _mapWithIndex
  2123. };
  2124. /**
  2125. * @category instances
  2126. * @since 2.10.0
  2127. */
  2128. exports.Apply = {
  2129. URI: exports.URI,
  2130. map: _map,
  2131. ap: _ap
  2132. };
  2133. /**
  2134. * Combine two effectful actions, keeping only the result of the first.
  2135. *
  2136. * @since 2.5.0
  2137. */
  2138. exports.apFirst = (0, Apply_1.apFirst)(exports.Apply);
  2139. /**
  2140. * Combine two effectful actions, keeping only the result of the second.
  2141. *
  2142. * @since 2.5.0
  2143. */
  2144. exports.apSecond = (0, Apply_1.apSecond)(exports.Apply);
  2145. /**
  2146. * @category instances
  2147. * @since 2.7.0
  2148. */
  2149. exports.Applicative = {
  2150. URI: exports.URI,
  2151. map: _map,
  2152. ap: _ap,
  2153. of: exports.of
  2154. };
  2155. /**
  2156. * @category instances
  2157. * @since 2.10.0
  2158. */
  2159. exports.Chain = {
  2160. URI: exports.URI,
  2161. map: _map,
  2162. ap: _ap,
  2163. chain: _chain
  2164. };
  2165. /**
  2166. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  2167. * keeping only the result of the first.
  2168. *
  2169. * @example
  2170. * import * as A from 'fp-ts/Array'
  2171. * import { pipe } from 'fp-ts/function'
  2172. *
  2173. * assert.deepStrictEqual(
  2174. * pipe(
  2175. * [1, 2, 3],
  2176. * A.chainFirst(() => ['a', 'b'])
  2177. * ),
  2178. * [1, 1, 2, 2, 3, 3]
  2179. * )
  2180. * assert.deepStrictEqual(
  2181. * pipe(
  2182. * [1, 2, 3],
  2183. * A.chainFirst(() => [])
  2184. * ),
  2185. * []
  2186. * )
  2187. *
  2188. * @category sequencing
  2189. * @since 2.0.0
  2190. */
  2191. exports.chainFirst =
  2192. /*#__PURE__*/ (0, Chain_1.chainFirst)(exports.Chain);
  2193. /**
  2194. * @category instances
  2195. * @since 2.7.0
  2196. */
  2197. exports.Monad = {
  2198. URI: exports.URI,
  2199. map: _map,
  2200. ap: _ap,
  2201. of: exports.of,
  2202. chain: _chain
  2203. };
  2204. /**
  2205. * @category instances
  2206. * @since 2.7.0
  2207. */
  2208. exports.Unfoldable = {
  2209. URI: exports.URI,
  2210. unfold: exports.unfold
  2211. };
  2212. /**
  2213. * @category instances
  2214. * @since 2.7.0
  2215. */
  2216. exports.Alt = {
  2217. URI: exports.URI,
  2218. map: _map,
  2219. alt: _alt
  2220. };
  2221. /**
  2222. * @category instances
  2223. * @since 2.11.0
  2224. */
  2225. exports.Zero = {
  2226. URI: exports.URI,
  2227. zero: exports.zero
  2228. };
  2229. /**
  2230. * @category do notation
  2231. * @since 2.11.0
  2232. */
  2233. exports.guard = (0, Zero_1.guard)(exports.Zero, exports.Pointed);
  2234. /**
  2235. * @category instances
  2236. * @since 2.7.0
  2237. */
  2238. exports.Alternative = {
  2239. URI: exports.URI,
  2240. map: _map,
  2241. ap: _ap,
  2242. of: exports.of,
  2243. alt: _alt,
  2244. zero: exports.zero
  2245. };
  2246. /**
  2247. * @category instances
  2248. * @since 2.7.0
  2249. */
  2250. exports.Extend = {
  2251. URI: exports.URI,
  2252. map: _map,
  2253. extend: _extend
  2254. };
  2255. /**
  2256. * @category instances
  2257. * @since 2.7.0
  2258. */
  2259. exports.Compactable = {
  2260. URI: exports.URI,
  2261. compact: exports.compact,
  2262. separate: exports.separate
  2263. };
  2264. /**
  2265. * @category instances
  2266. * @since 2.7.0
  2267. */
  2268. exports.Filterable = {
  2269. URI: exports.URI,
  2270. map: _map,
  2271. compact: exports.compact,
  2272. separate: exports.separate,
  2273. filter: _filter,
  2274. filterMap: _filterMap,
  2275. partition: _partition,
  2276. partitionMap: _partitionMap
  2277. };
  2278. /**
  2279. * @category instances
  2280. * @since 2.7.0
  2281. */
  2282. exports.FilterableWithIndex = {
  2283. URI: exports.URI,
  2284. map: _map,
  2285. mapWithIndex: _mapWithIndex,
  2286. compact: exports.compact,
  2287. separate: exports.separate,
  2288. filter: _filter,
  2289. filterMap: _filterMap,
  2290. partition: _partition,
  2291. partitionMap: _partitionMap,
  2292. partitionMapWithIndex: _partitionMapWithIndex,
  2293. partitionWithIndex: _partitionWithIndex,
  2294. filterMapWithIndex: _filterMapWithIndex,
  2295. filterWithIndex: _filterWithIndex
  2296. };
  2297. /**
  2298. * @category instances
  2299. * @since 2.7.0
  2300. */
  2301. exports.Foldable = {
  2302. URI: exports.URI,
  2303. reduce: _reduce,
  2304. foldMap: _foldMap,
  2305. reduceRight: _reduceRight
  2306. };
  2307. /**
  2308. * @category instances
  2309. * @since 2.7.0
  2310. */
  2311. exports.FoldableWithIndex = {
  2312. URI: exports.URI,
  2313. reduce: _reduce,
  2314. foldMap: _foldMap,
  2315. reduceRight: _reduceRight,
  2316. reduceWithIndex: _reduceWithIndex,
  2317. foldMapWithIndex: _foldMapWithIndex,
  2318. reduceRightWithIndex: _reduceRightWithIndex
  2319. };
  2320. /**
  2321. * @category instances
  2322. * @since 2.7.0
  2323. */
  2324. exports.Traversable = {
  2325. URI: exports.URI,
  2326. map: _map,
  2327. reduce: _reduce,
  2328. foldMap: _foldMap,
  2329. reduceRight: _reduceRight,
  2330. traverse: _traverse,
  2331. sequence: exports.sequence
  2332. };
  2333. /**
  2334. * @category instances
  2335. * @since 2.7.0
  2336. */
  2337. exports.TraversableWithIndex = {
  2338. URI: exports.URI,
  2339. map: _map,
  2340. mapWithIndex: _mapWithIndex,
  2341. reduce: _reduce,
  2342. foldMap: _foldMap,
  2343. reduceRight: _reduceRight,
  2344. reduceWithIndex: _reduceWithIndex,
  2345. foldMapWithIndex: _foldMapWithIndex,
  2346. reduceRightWithIndex: _reduceRightWithIndex,
  2347. traverse: _traverse,
  2348. sequence: exports.sequence,
  2349. traverseWithIndex: _traverseWithIndex
  2350. };
  2351. var _wither = /*#__PURE__*/ (0, Witherable_1.witherDefault)(exports.Traversable, exports.Compactable);
  2352. var _wilt = /*#__PURE__*/ (0, Witherable_1.wiltDefault)(exports.Traversable, exports.Compactable);
  2353. /**
  2354. * @category instances
  2355. * @since 2.7.0
  2356. */
  2357. exports.Witherable = {
  2358. URI: exports.URI,
  2359. map: _map,
  2360. compact: exports.compact,
  2361. separate: exports.separate,
  2362. filter: _filter,
  2363. filterMap: _filterMap,
  2364. partition: _partition,
  2365. partitionMap: _partitionMap,
  2366. reduce: _reduce,
  2367. foldMap: _foldMap,
  2368. reduceRight: _reduceRight,
  2369. traverse: _traverse,
  2370. sequence: exports.sequence,
  2371. wither: _wither,
  2372. wilt: _wilt
  2373. };
  2374. /**
  2375. * @category sequencing
  2376. * @since 2.11.0
  2377. */
  2378. exports.chainRecDepthFirst = RA.chainRecDepthFirst;
  2379. /**
  2380. * @category instances
  2381. * @since 2.11.0
  2382. */
  2383. exports.ChainRecDepthFirst = {
  2384. URI: exports.URI,
  2385. map: _map,
  2386. ap: _ap,
  2387. chain: _chain,
  2388. chainRec: _chainRecDepthFirst
  2389. };
  2390. /**
  2391. * @category sequencing
  2392. * @since 2.11.0
  2393. */
  2394. exports.chainRecBreadthFirst = RA.chainRecBreadthFirst;
  2395. /**
  2396. * @category instances
  2397. * @since 2.11.0
  2398. */
  2399. exports.ChainRecBreadthFirst = {
  2400. URI: exports.URI,
  2401. map: _map,
  2402. ap: _ap,
  2403. chain: _chain,
  2404. chainRec: _chainRecBreadthFirst
  2405. };
  2406. /**
  2407. * Filter values inside a context.
  2408. *
  2409. * @since 2.11.0
  2410. */
  2411. exports.filterE = (0, Witherable_1.filterE)(exports.Witherable);
  2412. /**
  2413. * @category instances
  2414. * @since 2.11.0
  2415. */
  2416. exports.FromEither = {
  2417. URI: exports.URI,
  2418. fromEither: exports.fromEither
  2419. };
  2420. /**
  2421. * @category lifting
  2422. * @since 2.11.0
  2423. */
  2424. exports.fromEitherK = (0, FromEither_1.fromEitherK)(exports.FromEither);
  2425. // -------------------------------------------------------------------------------------
  2426. // unsafe
  2427. // -------------------------------------------------------------------------------------
  2428. /**
  2429. * @category unsafe
  2430. * @since 2.0.0
  2431. */
  2432. exports.unsafeInsertAt = NEA.unsafeInsertAt;
  2433. /**
  2434. * @category unsafe
  2435. * @since 2.0.0
  2436. */
  2437. var unsafeUpdateAt = function (i, a, as) {
  2438. return (0, exports.isNonEmpty)(as) ? NEA.unsafeUpdateAt(i, a, as) : [];
  2439. };
  2440. exports.unsafeUpdateAt = unsafeUpdateAt;
  2441. /**
  2442. * @category unsafe
  2443. * @since 2.0.0
  2444. */
  2445. var unsafeDeleteAt = function (i, as) {
  2446. var xs = as.slice();
  2447. xs.splice(i, 1);
  2448. return xs;
  2449. };
  2450. exports.unsafeDeleteAt = unsafeDeleteAt;
  2451. // -------------------------------------------------------------------------------------
  2452. // utils
  2453. // -------------------------------------------------------------------------------------
  2454. /**
  2455. * `every` tells if the provided predicate holds true for every element in the `Array`.
  2456. *
  2457. * @example
  2458. * import { every } from 'fp-ts/Array'
  2459. *
  2460. * assert.equal(every((x: number) => x >= 0)([1, 2, 3]), true);
  2461. * assert.equal(every((x: number) => x >= 0)([-1, 2, 3]), false);
  2462. *
  2463. * @since 2.9.0
  2464. */
  2465. exports.every = RA.every;
  2466. /**
  2467. * `some` tells if the provided predicate holds true at least for one element in the `Array`.
  2468. *
  2469. * @example
  2470. * import { some } from 'fp-ts/Array'
  2471. *
  2472. * assert.equal(some((x: number) => x >= 0)([1, 2, 3]), true);
  2473. * assert.equal(some((x: number) => x >= 10)([1, 2, 3]), false);
  2474. *
  2475. * @since 2.9.0
  2476. */
  2477. var some = function (predicate) {
  2478. return function (as) {
  2479. return as.some(predicate);
  2480. };
  2481. };
  2482. exports.some = some;
  2483. /**
  2484. * Alias of [`some`](#some)
  2485. *
  2486. * @since 2.11.0
  2487. */
  2488. exports.exists = exports.some;
  2489. /**
  2490. * Places an element in between members of an `Array`, then folds the results using the provided `Monoid`.
  2491. *
  2492. * @example
  2493. * import * as S from 'fp-ts/string'
  2494. * import { intercalate } from 'fp-ts/Array'
  2495. *
  2496. * assert.deepStrictEqual(intercalate(S.Monoid)('-')(['a', 'b', 'c']), 'a-b-c')
  2497. *
  2498. * @since 2.12.0
  2499. */
  2500. exports.intercalate = RA.intercalate;
  2501. // -------------------------------------------------------------------------------------
  2502. // do notation
  2503. // -------------------------------------------------------------------------------------
  2504. /**
  2505. * @category do notation
  2506. * @since 2.9.0
  2507. */
  2508. exports.Do = (0, exports.of)(_.emptyRecord);
  2509. /**
  2510. * @category do notation
  2511. * @since 2.8.0
  2512. */
  2513. exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
  2514. var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
  2515. exports.let = let_;
  2516. /**
  2517. * @category do notation
  2518. * @since 2.8.0
  2519. */
  2520. exports.bind = (0, Chain_1.bind)(exports.Chain);
  2521. /**
  2522. * @category do notation
  2523. * @since 2.8.0
  2524. */
  2525. exports.apS = (0, Apply_1.apS)(exports.Apply);
  2526. // -------------------------------------------------------------------------------------
  2527. // deprecated
  2528. // -------------------------------------------------------------------------------------
  2529. /**
  2530. * Use `NonEmptyArray` module instead.
  2531. *
  2532. * @category zone of death
  2533. * @since 2.0.0
  2534. * @deprecated
  2535. */
  2536. exports.range = NEA.range;
  2537. /**
  2538. * Use a new `[]` instead.
  2539. *
  2540. * @category zone of death
  2541. * @since 2.0.0
  2542. * @deprecated
  2543. */
  2544. exports.empty = [];
  2545. /**
  2546. * Use `prepend` instead.
  2547. *
  2548. * @category zone of death
  2549. * @since 2.0.0
  2550. * @deprecated
  2551. */
  2552. exports.cons = NEA.cons;
  2553. /**
  2554. * Use `append` instead.
  2555. *
  2556. * @category zone of death
  2557. * @since 2.0.0
  2558. * @deprecated
  2559. */
  2560. exports.snoc = NEA.snoc;
  2561. /**
  2562. * Use `prependAll` instead
  2563. *
  2564. * @category zone of death
  2565. * @since 2.9.0
  2566. * @deprecated
  2567. */
  2568. exports.prependToAll = exports.prependAll;
  2569. /**
  2570. * This instance is deprecated, use small, specific instances instead.
  2571. * For example if a function needs a `Functor` instance, pass `A.Functor` instead of `A.array`
  2572. * (where `A` is from `import A from 'fp-ts/Array'`)
  2573. *
  2574. * @category zone of death
  2575. * @since 2.0.0
  2576. * @deprecated
  2577. */
  2578. exports.array = {
  2579. URI: exports.URI,
  2580. compact: exports.compact,
  2581. separate: exports.separate,
  2582. map: _map,
  2583. ap: _ap,
  2584. of: exports.of,
  2585. chain: _chain,
  2586. filter: _filter,
  2587. filterMap: _filterMap,
  2588. partition: _partition,
  2589. partitionMap: _partitionMap,
  2590. mapWithIndex: _mapWithIndex,
  2591. partitionMapWithIndex: _partitionMapWithIndex,
  2592. partitionWithIndex: _partitionWithIndex,
  2593. filterMapWithIndex: _filterMapWithIndex,
  2594. filterWithIndex: _filterWithIndex,
  2595. alt: _alt,
  2596. zero: exports.zero,
  2597. unfold: exports.unfold,
  2598. reduce: _reduce,
  2599. foldMap: _foldMap,
  2600. reduceRight: _reduceRight,
  2601. traverse: _traverse,
  2602. sequence: exports.sequence,
  2603. reduceWithIndex: _reduceWithIndex,
  2604. foldMapWithIndex: _foldMapWithIndex,
  2605. reduceRightWithIndex: _reduceRightWithIndex,
  2606. traverseWithIndex: _traverseWithIndex,
  2607. extend: _extend,
  2608. wither: _wither,
  2609. wilt: _wilt
  2610. };