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

2243 строки
64 KiB

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