版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

2192 lines
69 KiB

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