版博士V2.0程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

1278 řádky
35 KiB

  1. /**
  2. * ```ts
  3. * type Either<E, A> = Left<E> | Right<A>
  4. * ```
  5. *
  6. * Represents a value of one of two possible types (a disjoint union).
  7. *
  8. * An instance of `Either` is either an instance of `Left` or `Right`.
  9. *
  10. * A common use of `Either` is as an alternative to `Option` for dealing with possible missing values. In this usage,
  11. * `None` is replaced with a `Left` which can contain useful information. `Right` takes the place of `Some`. Convention
  12. * dictates that `Left` is used for failure and `Right` is used for success.
  13. *
  14. *
  15. * @example
  16. * import * as E from 'fp-ts/Either'
  17. * import { pipe } from 'fp-ts/function'
  18. *
  19. * const double = (n: number): number => n * 2
  20. *
  21. * export const imperative = (as: ReadonlyArray<number>): string => {
  22. * const head = (as: ReadonlyArray<number>): number => {
  23. * if (as.length === 0) {
  24. * throw new Error('empty array')
  25. * }
  26. * return as[0]
  27. * }
  28. * const inverse = (n: number): number => {
  29. * if (n === 0) {
  30. * throw new Error('cannot divide by zero')
  31. * }
  32. * return 1 / n
  33. * }
  34. * try {
  35. * return `Result is ${inverse(double(head(as)))}`
  36. * } catch (err: any) {
  37. * return `Error is ${err.message}`
  38. * }
  39. * }
  40. *
  41. * export const functional = (as: ReadonlyArray<number>): string => {
  42. * const head = <A>(as: ReadonlyArray<A>): E.Either<string, A> =>
  43. * as.length === 0 ? E.left('empty array') : E.right(as[0])
  44. * const inverse = (n: number): E.Either<string, number> =>
  45. * n === 0 ? E.left('cannot divide by zero') : E.right(1 / n)
  46. * return pipe(
  47. * as,
  48. * head,
  49. * E.map(double),
  50. * E.chain(inverse),
  51. * E.match(
  52. * (err) => `Error is ${err}`, // onLeft handler
  53. * (head) => `Result is ${head}` // onRight handler
  54. * )
  55. * )
  56. * }
  57. *
  58. * assert.deepStrictEqual(imperative([1, 2, 3]), functional([1, 2, 3]))
  59. * assert.deepStrictEqual(imperative([]), functional([]))
  60. * assert.deepStrictEqual(imperative([0]), functional([0]))
  61. *
  62. * @since 2.0.0
  63. */
  64. import { Alt2, Alt2C } from './Alt'
  65. import { Applicative2, Applicative2C } from './Applicative'
  66. import { Apply2 } from './Apply'
  67. import { Bifunctor2 } from './Bifunctor'
  68. import { Chain2 } from './Chain'
  69. import { ChainRec2, ChainRec2C } from './ChainRec'
  70. import { Compactable2C } from './Compactable'
  71. import { Eq } from './Eq'
  72. import { Extend2 } from './Extend'
  73. import { Filterable2C } from './Filterable'
  74. import { Foldable2 } from './Foldable'
  75. import { FromEither2 } from './FromEither'
  76. import { Lazy } from './function'
  77. import { Functor2 } from './Functor'
  78. import { Monad2, Monad2C } from './Monad'
  79. import { MonadThrow2, MonadThrow2C } from './MonadThrow'
  80. import { Monoid } from './Monoid'
  81. import { Option } from './Option'
  82. import { Pointed2 } from './Pointed'
  83. import { Predicate } from './Predicate'
  84. import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray'
  85. import { Refinement } from './Refinement'
  86. import { Semigroup } from './Semigroup'
  87. import { Show } from './Show'
  88. import { PipeableTraverse2, Traversable2 } from './Traversable'
  89. import { Witherable2C } from './Witherable'
  90. /**
  91. * @category model
  92. * @since 2.0.0
  93. */
  94. export interface Left<E> {
  95. readonly _tag: 'Left'
  96. readonly left: E
  97. }
  98. /**
  99. * @category model
  100. * @since 2.0.0
  101. */
  102. export interface Right<A> {
  103. readonly _tag: 'Right'
  104. readonly right: A
  105. }
  106. /**
  107. * @category model
  108. * @since 2.0.0
  109. */
  110. export declare type Either<E, A> = Left<E> | Right<A>
  111. /**
  112. * Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this
  113. * structure.
  114. *
  115. * @category constructors
  116. * @since 2.0.0
  117. */
  118. export declare const left: <E = never, A = never>(e: E) => Either<E, A>
  119. /**
  120. * Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias
  121. * of this structure.
  122. *
  123. * @category constructors
  124. * @since 2.0.0
  125. */
  126. export declare const right: <E = never, A = never>(a: A) => Either<E, A>
  127. /**
  128. * @category type lambdas
  129. * @since 2.0.0
  130. */
  131. export declare const URI = 'Either'
  132. /**
  133. * @category type lambdas
  134. * @since 2.0.0
  135. */
  136. export declare type URI = typeof URI
  137. declare module './HKT' {
  138. interface URItoKind2<E, A> {
  139. readonly [URI]: Either<E, A>
  140. }
  141. }
  142. /**
  143. * @category instances
  144. * @since 2.0.0
  145. */
  146. export declare const getShow: <E, A>(SE: Show<E>, SA: Show<A>) => Show<Either<E, A>>
  147. /**
  148. * @category instances
  149. * @since 2.0.0
  150. */
  151. export declare const getEq: <E, A>(EL: Eq<E>, EA: Eq<A>) => Eq<Either<E, A>>
  152. /**
  153. * Semigroup returning the left-most non-`Left` value. If both operands are `Right`s then the inner values are
  154. * concatenated using the provided `Semigroup`
  155. *
  156. * @example
  157. * import { getSemigroup, left, right } from 'fp-ts/Either'
  158. * import { SemigroupSum } from 'fp-ts/number'
  159. *
  160. * const S = getSemigroup<string, number>(SemigroupSum)
  161. * assert.deepStrictEqual(S.concat(left('a'), left('b')), left('a'))
  162. * assert.deepStrictEqual(S.concat(left('a'), right(2)), right(2))
  163. * assert.deepStrictEqual(S.concat(right(1), left('b')), right(1))
  164. * assert.deepStrictEqual(S.concat(right(1), right(2)), right(3))
  165. *
  166. * @category instances
  167. * @since 2.0.0
  168. */
  169. export declare const getSemigroup: <E, A>(S: Semigroup<A>) => Semigroup<Either<E, A>>
  170. /**
  171. * Builds a `Compactable` instance for `Either` given `Monoid` for the left side.
  172. *
  173. * @category filtering
  174. * @since 2.10.0
  175. */
  176. export declare const getCompactable: <E>(M: Monoid<E>) => Compactable2C<'Either', E>
  177. /**
  178. * Builds a `Filterable` instance for `Either` given `Monoid` for the left side
  179. *
  180. * @category filtering
  181. * @since 2.10.0
  182. */
  183. export declare const getFilterable: <E>(M: Monoid<E>) => Filterable2C<'Either', E>
  184. /**
  185. * Builds `Witherable` instance for `Either` given `Monoid` for the left side
  186. *
  187. * @category filtering
  188. * @since 2.0.0
  189. */
  190. export declare const getWitherable: <E>(M: Monoid<E>) => Witherable2C<'Either', E>
  191. /**
  192. * The default [`Applicative`](#applicative) instance returns the first error, if you want to
  193. * get all errors you need to provide a way to concatenate them via a `Semigroup`.
  194. *
  195. * @example
  196. * import * as A from 'fp-ts/Apply'
  197. * import * as E from 'fp-ts/Either'
  198. * import { pipe } from 'fp-ts/function'
  199. * import * as S from 'fp-ts/Semigroup'
  200. * import * as string from 'fp-ts/string'
  201. *
  202. * const parseString = (u: unknown): E.Either<string, string> =>
  203. * typeof u === 'string' ? E.right(u) : E.left('not a string')
  204. *
  205. * const parseNumber = (u: unknown): E.Either<string, number> =>
  206. * typeof u === 'number' ? E.right(u) : E.left('not a number')
  207. *
  208. * interface Person {
  209. * readonly name: string
  210. * readonly age: number
  211. * }
  212. *
  213. * const parsePerson = (
  214. * input: Record<string, unknown>
  215. * ): E.Either<string, Person> =>
  216. * pipe(
  217. * E.Do,
  218. * E.apS('name', parseString(input.name)),
  219. * E.apS('age', parseNumber(input.age))
  220. * )
  221. *
  222. * assert.deepStrictEqual(parsePerson({}), E.left('not a string')) // <= first error
  223. *
  224. * const Applicative = E.getApplicativeValidation(
  225. * pipe(string.Semigroup, S.intercalate(', '))
  226. * )
  227. *
  228. * const apS = A.apS(Applicative)
  229. *
  230. * const parsePersonAll = (
  231. * input: Record<string, unknown>
  232. * ): E.Either<string, Person> =>
  233. * pipe(
  234. * E.Do,
  235. * apS('name', parseString(input.name)),
  236. * apS('age', parseNumber(input.age))
  237. * )
  238. *
  239. * assert.deepStrictEqual(parsePersonAll({}), E.left('not a string, not a number')) // <= all errors
  240. *
  241. * @category error handling
  242. * @since 2.7.0
  243. */
  244. export declare const getApplicativeValidation: <E>(SE: Semigroup<E>) => Applicative2C<'Either', E>
  245. /**
  246. * The default [`Alt`](#alt) instance returns the last error, if you want to
  247. * get all errors you need to provide a way to concatenate them via a `Semigroup`.
  248. *
  249. * @example
  250. * import * as E from 'fp-ts/Either'
  251. * import { pipe } from 'fp-ts/function'
  252. * import * as S from 'fp-ts/Semigroup'
  253. * import * as string from 'fp-ts/string'
  254. *
  255. * const parseString = (u: unknown): E.Either<string, string> =>
  256. * typeof u === 'string' ? E.right(u) : E.left('not a string')
  257. *
  258. * const parseNumber = (u: unknown): E.Either<string, number> =>
  259. * typeof u === 'number' ? E.right(u) : E.left('not a number')
  260. *
  261. * const parse = (u: unknown): E.Either<string, string | number> =>
  262. * pipe(
  263. * parseString(u),
  264. * E.alt<string, string | number>(() => parseNumber(u))
  265. * )
  266. *
  267. * assert.deepStrictEqual(parse(true), E.left('not a number')) // <= last error
  268. *
  269. * const Alt = E.getAltValidation(pipe(string.Semigroup, S.intercalate(', ')))
  270. *
  271. * const parseAll = (u: unknown): E.Either<string, string | number> =>
  272. * Alt.alt<string | number>(parseString(u), () => parseNumber(u))
  273. *
  274. * assert.deepStrictEqual(parseAll(true), E.left('not a string, not a number')) // <= all errors
  275. *
  276. * @category error handling
  277. * @since 2.7.0
  278. */
  279. export declare const getAltValidation: <E>(SE: Semigroup<E>) => Alt2C<'Either', E>
  280. /**
  281. * @category mapping
  282. * @since 2.0.0
  283. */
  284. export declare const map: <A, B>(f: (a: A) => B) => <E>(fa: Either<E, A>) => Either<E, B>
  285. /**
  286. * @category instances
  287. * @since 2.7.0
  288. */
  289. export declare const Functor: Functor2<URI>
  290. /**
  291. * @category constructors
  292. * @since 2.7.0
  293. */
  294. export declare const of: <E = never, A = never>(a: A) => Either<E, A>
  295. /**
  296. * @category instances
  297. * @since 2.10.0
  298. */
  299. export declare const Pointed: Pointed2<URI>
  300. /**
  301. * Less strict version of [`ap`](#ap).
  302. *
  303. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  304. *
  305. * @since 2.8.0
  306. */
  307. export declare const apW: <E2, A>(fa: Either<E2, A>) => <E1, B>(fab: Either<E1, (a: A) => B>) => Either<E1 | E2, B>
  308. /**
  309. * @since 2.0.0
  310. */
  311. export declare const ap: <E, A>(fa: Either<E, A>) => <B>(fab: Either<E, (a: A) => B>) => Either<E, B>
  312. /**
  313. * @category instances
  314. * @since 2.10.0
  315. */
  316. export declare const Apply: Apply2<URI>
  317. /**
  318. * @category instances
  319. * @since 2.7.0
  320. */
  321. export declare const Applicative: Applicative2<URI>
  322. /**
  323. * Less strict version of [`chain`](#chain).
  324. *
  325. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  326. *
  327. * @example
  328. * import * as E from 'fp-ts/Either'
  329. * import { pipe } from 'fp-ts/function'
  330. *
  331. * const e1: E.Either<string, number> = E.right(1)
  332. * const e2: E.Either<number, number> = E.right(2)
  333. *
  334. * export const result1 = pipe(
  335. * // @ts-expect-error
  336. * e1,
  337. * E.chain(() => e2)
  338. * )
  339. *
  340. * // merged error types -----v-------------v
  341. * // const result2: E.Either<string | number, number>
  342. * export const result2 = pipe(
  343. * e1, // no error
  344. * E.chainW(() => e2)
  345. * )
  346. *
  347. * @category sequencing
  348. * @since 2.6.0
  349. */
  350. export declare const chainW: <E2, A, B>(f: (a: A) => Either<E2, B>) => <E1>(ma: Either<E1, A>) => Either<E2 | E1, B>
  351. /**
  352. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  353. *
  354. * @category sequencing
  355. * @since 2.0.0
  356. */
  357. export declare const chain: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: Either<E, A>) => Either<E, B>
  358. /**
  359. * @category instances
  360. * @since 2.10.0
  361. */
  362. export declare const Chain: Chain2<URI>
  363. /**
  364. * @category instances
  365. * @since 2.7.0
  366. */
  367. export declare const Monad: Monad2<URI>
  368. /**
  369. * Left-associative fold of a structure.
  370. *
  371. * @example
  372. * import { pipe } from 'fp-ts/function'
  373. * import * as E from 'fp-ts/Either'
  374. *
  375. * const startWith = 'prefix'
  376. * const concat = (a: string, b: string) => `${a}:${b}`
  377. *
  378. * assert.deepStrictEqual(
  379. * pipe(E.right('a'), E.reduce(startWith, concat)),
  380. * 'prefix:a'
  381. * )
  382. *
  383. * assert.deepStrictEqual(
  384. * pipe(E.left('e'), E.reduce(startWith, concat)),
  385. * 'prefix'
  386. * )
  387. *
  388. * @category folding
  389. * @since 2.0.0
  390. */
  391. export declare const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => <E>(fa: Either<E, A>) => B
  392. /**
  393. * Map each element of the structure to a monoid, and combine the results.
  394. *
  395. * @example
  396. * import { pipe } from 'fp-ts/function'
  397. * import * as E from 'fp-ts/Either'
  398. * import * as S from 'fp-ts/string'
  399. *
  400. * const yell = (a: string) => `${a}!`
  401. *
  402. * assert.deepStrictEqual(
  403. * pipe(E.right('a'), E.foldMap(S.Monoid)(yell)),
  404. * 'a!'
  405. * )
  406. *
  407. * assert.deepStrictEqual(
  408. * pipe(E.left('e'), E.foldMap(S.Monoid)(yell)),
  409. * S.Monoid.empty
  410. * )
  411. *
  412. * @category folding
  413. * @since 2.0.0
  414. */
  415. export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => <E>(fa: Either<E, A>) => M
  416. /**
  417. * Right-associative fold of a structure.
  418. *
  419. * @example
  420. * import { pipe } from 'fp-ts/function'
  421. * import * as E from 'fp-ts/Either'
  422. *
  423. * const startWith = 'postfix'
  424. * const concat = (a: string, b: string) => `${a}:${b}`
  425. *
  426. * assert.deepStrictEqual(
  427. * pipe(E.right('a'), E.reduceRight(startWith, concat)),
  428. * 'a:postfix'
  429. * )
  430. *
  431. * assert.deepStrictEqual(
  432. * pipe(E.left('e'), E.reduceRight(startWith, concat)),
  433. * 'postfix'
  434. * )
  435. *
  436. * @category folding
  437. * @since 2.0.0
  438. */
  439. export declare const reduceRight: <A, B>(b: B, f: (a: A, b: B) => B) => <E>(fa: Either<E, A>) => B
  440. /**
  441. * @category instances
  442. * @since 2.7.0
  443. */
  444. export declare const Foldable: Foldable2<URI>
  445. /**
  446. * Map each element of a structure to an action, evaluate these actions from left to right, and collect the results.
  447. *
  448. * @example
  449. * import { pipe } from 'fp-ts/function'
  450. * import * as RA from 'fp-ts/ReadonlyArray'
  451. * import * as E from 'fp-ts/Either'
  452. * import * as O from 'fp-ts/Option'
  453. *
  454. * assert.deepStrictEqual(
  455. * pipe(E.right(['a']), E.traverse(O.Applicative)(RA.head)),
  456. * O.some(E.right('a'))
  457. * )
  458. *
  459. * assert.deepStrictEqual(
  460. * pipe(E.right([]), E.traverse(O.Applicative)(RA.head)),
  461. * O.none
  462. * )
  463. *
  464. * @category traversing
  465. * @since 2.6.3
  466. */
  467. export declare const traverse: PipeableTraverse2<URI>
  468. /**
  469. * Evaluate each monadic action in the structure from left to right, and collect the results.
  470. *
  471. * @example
  472. * import { pipe } from 'fp-ts/function'
  473. * import * as E from 'fp-ts/Either'
  474. * import * as O from 'fp-ts/Option'
  475. *
  476. * assert.deepStrictEqual(
  477. * pipe(E.right(O.some('a')), E.sequence(O.Applicative)),
  478. * O.some(E.right('a'))
  479. * )
  480. *
  481. * assert.deepStrictEqual(
  482. * pipe(E.right(O.none), E.sequence(O.Applicative)),
  483. * O.none
  484. * )
  485. *
  486. * @category traversing
  487. * @since 2.6.3
  488. */
  489. export declare const sequence: Traversable2<URI>['sequence']
  490. /**
  491. * @category instances
  492. * @since 2.7.0
  493. */
  494. export declare const Traversable: Traversable2<URI>
  495. /**
  496. * Map a pair of functions over the two type arguments of the bifunctor.
  497. *
  498. * @category mapping
  499. * @since 2.0.0
  500. */
  501. export declare const bimap: <E, G, A, B>(f: (e: E) => G, g: (a: A) => B) => (fa: Either<E, A>) => Either<G, B>
  502. /**
  503. * Map a function over the first type argument of a bifunctor.
  504. *
  505. * @category error handling
  506. * @since 2.0.0
  507. */
  508. export declare const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: Either<E, A>) => Either<G, A>
  509. /**
  510. * @category instances
  511. * @since 2.7.0
  512. */
  513. export declare const Bifunctor: Bifunctor2<URI>
  514. /**
  515. * Less strict version of [`alt`](#alt).
  516. *
  517. * The `W` suffix (short for **W**idening) means that the error and the return types will be merged.
  518. *
  519. * @category error handling
  520. * @since 2.9.0
  521. */
  522. export declare const altW: <E2, B>(that: Lazy<Either<E2, B>>) => <E1, A>(fa: Either<E1, A>) => Either<E2, A | B>
  523. /**
  524. * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
  525. * types of kind `* -> *`.
  526. *
  527. * In case of `Either` returns the left-most non-`Left` value (or the right-most `Left` value if both values are `Left`).
  528. *
  529. * | x | y | pipe(x, alt(() => y) |
  530. * | -------- | -------- | -------------------- |
  531. * | left(a) | left(b) | left(b) |
  532. * | left(a) | right(2) | right(2) |
  533. * | right(1) | left(b) | right(1) |
  534. * | right(1) | right(2) | right(1) |
  535. *
  536. * @example
  537. * import * as E from 'fp-ts/Either'
  538. * import { pipe } from 'fp-ts/function'
  539. *
  540. * assert.deepStrictEqual(
  541. * pipe(
  542. * E.left('a'),
  543. * E.alt(() => E.left('b'))
  544. * ),
  545. * E.left('b')
  546. * )
  547. * assert.deepStrictEqual(
  548. * pipe(
  549. * E.left('a'),
  550. * E.alt(() => E.right(2))
  551. * ),
  552. * E.right(2)
  553. * )
  554. * assert.deepStrictEqual(
  555. * pipe(
  556. * E.right(1),
  557. * E.alt(() => E.left('b'))
  558. * ),
  559. * E.right(1)
  560. * )
  561. * assert.deepStrictEqual(
  562. * pipe(
  563. * E.right(1),
  564. * E.alt(() => E.right(2))
  565. * ),
  566. * E.right(1)
  567. * )
  568. *
  569. * @category error handling
  570. * @since 2.0.0
  571. */
  572. export declare const alt: <E, A>(that: Lazy<Either<E, A>>) => (fa: Either<E, A>) => Either<E, A>
  573. /**
  574. * @category instances
  575. * @since 2.7.0
  576. */
  577. export declare const Alt: Alt2<URI>
  578. /**
  579. * @since 2.0.0
  580. */
  581. export declare const extend: <E, A, B>(f: (wa: Either<E, A>) => B) => (wa: Either<E, A>) => Either<E, B>
  582. /**
  583. * @category instances
  584. * @since 2.7.0
  585. */
  586. export declare const Extend: Extend2<URI>
  587. /**
  588. * @category instances
  589. * @since 2.7.0
  590. */
  591. export declare const ChainRec: ChainRec2<URI>
  592. /**
  593. * @since 2.6.3
  594. */
  595. export declare const throwError: MonadThrow2<URI>['throwError']
  596. /**
  597. * @category instances
  598. * @since 2.7.0
  599. */
  600. export declare const MonadThrow: MonadThrow2<URI>
  601. /**
  602. * @category instances
  603. * @since 2.10.0
  604. */
  605. export declare const FromEither: FromEither2<URI>
  606. /**
  607. * @example
  608. * import { fromPredicate, left, right } from 'fp-ts/Either'
  609. * import { pipe } from 'fp-ts/function'
  610. *
  611. * assert.deepStrictEqual(
  612. * pipe(
  613. * 1,
  614. * fromPredicate(
  615. * (n) => n > 0,
  616. * () => 'error'
  617. * )
  618. * ),
  619. * right(1)
  620. * )
  621. * assert.deepStrictEqual(
  622. * pipe(
  623. * -1,
  624. * fromPredicate(
  625. * (n) => n > 0,
  626. * () => 'error'
  627. * )
  628. * ),
  629. * left('error')
  630. * )
  631. *
  632. * @category lifting
  633. * @since 2.0.0
  634. */
  635. export declare const fromPredicate: {
  636. <A, B extends A, E>(refinement: Refinement<A, B>, onFalse: (a: A) => E): (a: A) => Either<E, B>
  637. <A, E>(predicate: Predicate<A>, onFalse: (a: A) => E): <B extends A>(b: B) => Either<E, B>
  638. <A, E>(predicate: Predicate<A>, onFalse: (a: A) => E): (a: A) => Either<E, A>
  639. }
  640. /**
  641. * @example
  642. * import * as E from 'fp-ts/Either'
  643. * import { pipe } from 'fp-ts/function'
  644. * import * as O from 'fp-ts/Option'
  645. *
  646. * assert.deepStrictEqual(
  647. * pipe(
  648. * O.some(1),
  649. * E.fromOption(() => 'error')
  650. * ),
  651. * E.right(1)
  652. * )
  653. * assert.deepStrictEqual(
  654. * pipe(
  655. * O.none,
  656. * E.fromOption(() => 'error')
  657. * ),
  658. * E.left('error')
  659. * )
  660. *
  661. * @category conversions
  662. * @since 2.0.0
  663. */
  664. export declare const fromOption: <E>(onNone: Lazy<E>) => <A>(fa: Option<A>) => Either<E, A>
  665. /**
  666. * Returns `true` if the either is an instance of `Left`, `false` otherwise.
  667. *
  668. * @category refinements
  669. * @since 2.0.0
  670. */
  671. export declare const isLeft: <E>(ma: Either<E, unknown>) => ma is Left<E>
  672. /**
  673. * Returns `true` if the either is an instance of `Right`, `false` otherwise.
  674. *
  675. * @category refinements
  676. * @since 2.0.0
  677. */
  678. export declare const isRight: <A>(ma: Either<unknown, A>) => ma is Right<A>
  679. /**
  680. * Less strict version of [`match`](#match).
  681. *
  682. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  683. *
  684. * @category pattern matching
  685. * @since 2.10.0
  686. */
  687. export declare const matchW: <E, B, A, C>(onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either<E, A>) => B | C
  688. /**
  689. * Alias of [`matchW`](#matchw).
  690. *
  691. * @category pattern matching
  692. * @since 2.10.0
  693. */
  694. export declare const foldW: <E, B, A, C>(onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either<E, A>) => B | C
  695. /**
  696. * Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the first function,
  697. * if the value is a `Right` the inner value is applied to the second function.
  698. *
  699. * @example
  700. * import { match, left, right } from 'fp-ts/Either'
  701. * import { pipe } from 'fp-ts/function'
  702. *
  703. * function onLeft(errors: Array<string>): string {
  704. * return `Errors: ${errors.join(', ')}`
  705. * }
  706. *
  707. * function onRight(value: number): string {
  708. * return `Ok: ${value}`
  709. * }
  710. *
  711. * assert.strictEqual(
  712. * pipe(
  713. * right(1),
  714. * match(onLeft, onRight)
  715. * ),
  716. * 'Ok: 1'
  717. * )
  718. * assert.strictEqual(
  719. * pipe(
  720. * left(['error 1', 'error 2']),
  721. * match(onLeft, onRight)
  722. * ),
  723. * 'Errors: error 1, error 2'
  724. * )
  725. *
  726. * @category pattern matching
  727. * @since 2.10.0
  728. */
  729. export declare const match: <E, A, B>(onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either<E, A>) => B
  730. /**
  731. * Alias of [`match`](#match).
  732. *
  733. * @category pattern matching
  734. * @since 2.0.0
  735. */
  736. export declare const fold: <E, A, B>(onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: Either<E, A>) => B
  737. /**
  738. * Less strict version of [`getOrElse`](#getorelse).
  739. *
  740. * The `W` suffix (short for **W**idening) means that the handler return type will be merged.
  741. *
  742. * @category error handling
  743. * @since 2.6.0
  744. */
  745. export declare const getOrElseW: <E, B>(onLeft: (e: E) => B) => <A>(ma: Either<E, A>) => B | A
  746. /**
  747. * Returns the wrapped value if it's a `Right` or a default value if is a `Left`.
  748. *
  749. * @example
  750. * import { getOrElse, left, right } from 'fp-ts/Either'
  751. * import { pipe } from 'fp-ts/function'
  752. *
  753. * assert.deepStrictEqual(
  754. * pipe(
  755. * right(1),
  756. * getOrElse(() => 0)
  757. * ),
  758. * 1
  759. * )
  760. * assert.deepStrictEqual(
  761. * pipe(
  762. * left('error'),
  763. * getOrElse(() => 0)
  764. * ),
  765. * 0
  766. * )
  767. *
  768. * @category error handling
  769. * @since 2.0.0
  770. */
  771. export declare const getOrElse: <E, A>(onLeft: (e: E) => A) => (ma: Either<E, A>) => A
  772. /**
  773. * @category mapping
  774. * @since 2.10.0
  775. */
  776. export declare const flap: <A>(a: A) => <E, B>(fab: Either<E, (a: A) => B>) => Either<E, B>
  777. /**
  778. * Combine two effectful actions, keeping only the result of the first.
  779. *
  780. * @since 2.0.0
  781. */
  782. export declare const apFirst: <E, B>(second: Either<E, B>) => <A>(first: Either<E, A>) => Either<E, A>
  783. /**
  784. * Less strict version of [`apFirst`](#apfirst)
  785. *
  786. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  787. *
  788. * @since 2.12.0
  789. */
  790. export declare const apFirstW: <E2, B>(second: Either<E2, B>) => <E1, A>(first: Either<E1, A>) => Either<E1 | E2, A>
  791. /**
  792. * Combine two effectful actions, keeping only the result of the second.
  793. *
  794. * @since 2.0.0
  795. */
  796. export declare const apSecond: <E, B>(second: Either<E, B>) => <A>(first: Either<E, A>) => Either<E, B>
  797. /**
  798. * Less strict version of [`apSecond`](#apsecond)
  799. *
  800. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  801. *
  802. * @since 2.12.0
  803. */
  804. export declare const apSecondW: <E2, B>(second: Either<E2, B>) => <E1, A>(first: Either<E1, A>) => Either<E1 | E2, B>
  805. /**
  806. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  807. * keeping only the result of the first.
  808. *
  809. * @category sequencing
  810. * @since 2.0.0
  811. */
  812. export declare const chainFirst: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: Either<E, A>) => Either<E, A>
  813. /**
  814. * Less strict version of [`chainFirst`](#chainfirst)
  815. *
  816. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  817. *
  818. * @category sequencing
  819. * @since 2.8.0
  820. */
  821. export declare const chainFirstW: <E2, A, B>(
  822. f: (a: A) => Either<E2, B>
  823. ) => <E1>(ma: Either<E1, A>) => Either<E1 | E2, A>
  824. /**
  825. * Less strict version of [`flatten`](#flatten).
  826. *
  827. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  828. *
  829. * @category sequencing
  830. * @since 2.11.0
  831. */
  832. export declare const flattenW: <E1, E2, A>(mma: Either<E1, Either<E2, A>>) => Either<E1 | E2, A>
  833. /**
  834. * The `flatten` function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level.
  835. *
  836. * @example
  837. * import * as E from 'fp-ts/Either'
  838. *
  839. * assert.deepStrictEqual(E.flatten(E.right(E.right('a'))), E.right('a'))
  840. * assert.deepStrictEqual(E.flatten(E.right(E.left('e'))), E.left('e'))
  841. * assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e'))
  842. *
  843. * @category sequencing
  844. * @since 2.0.0
  845. */
  846. export declare const flatten: <E, A>(mma: Either<E, Either<E, A>>) => Either<E, A>
  847. /**
  848. * @since 2.0.0
  849. */
  850. export declare const duplicate: <E, A>(ma: Either<E, A>) => Either<E, Either<E, A>>
  851. /**
  852. * @category lifting
  853. * @since 2.10.0
  854. */
  855. export declare const fromOptionK: <E>(
  856. onNone: Lazy<E>
  857. ) => <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => Option<B>) => (...a: A) => Either<E, B>
  858. /**
  859. * @category sequencing
  860. * @since 2.11.0
  861. */
  862. export declare const chainOptionK: <E>(
  863. onNone: Lazy<E>
  864. ) => <A, B>(f: (a: A) => Option<B>) => (ma: Either<E, A>) => Either<E, B>
  865. /**
  866. * @example
  867. * import * as E from 'fp-ts/Either'
  868. * import { pipe } from 'fp-ts/function'
  869. *
  870. * assert.deepStrictEqual(
  871. * pipe(
  872. * E.right(1),
  873. * E.filterOrElse(
  874. * (n) => n > 0,
  875. * () => 'error'
  876. * )
  877. * ),
  878. * E.right(1)
  879. * )
  880. * assert.deepStrictEqual(
  881. * pipe(
  882. * E.right(-1),
  883. * E.filterOrElse(
  884. * (n) => n > 0,
  885. * () => 'error'
  886. * )
  887. * ),
  888. * E.left('error')
  889. * )
  890. * assert.deepStrictEqual(
  891. * pipe(
  892. * E.left('a'),
  893. * E.filterOrElse(
  894. * (n) => n > 0,
  895. * () => 'error'
  896. * )
  897. * ),
  898. * E.left('a')
  899. * )
  900. *
  901. * @category filtering
  902. * @since 2.0.0
  903. */
  904. export declare const filterOrElse: {
  905. <A, B extends A, E>(refinement: Refinement<A, B>, onFalse: (a: A) => E): (ma: Either<E, A>) => Either<E, B>
  906. <A_1, E_1>(predicate: Predicate<A_1>, onFalse: (a: A_1) => E_1): <B_1 extends A_1>(
  907. mb: Either<E_1, B_1>
  908. ) => Either<E_1, B_1>
  909. <A_2, E_2>(predicate: Predicate<A_2>, onFalse: (a: A_2) => E_2): (ma: Either<E_2, A_2>) => Either<E_2, A_2>
  910. }
  911. /**
  912. * Less strict version of [`filterOrElse`](#filterorelse).
  913. *
  914. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  915. *
  916. * @category filtering
  917. * @since 2.9.0
  918. */
  919. export declare const filterOrElseW: {
  920. <A, B extends A, E2>(refinement: Refinement<A, B>, onFalse: (a: A) => E2): <E1>(
  921. ma: Either<E1, A>
  922. ) => Either<E1 | E2, B>
  923. <A, E2>(predicate: Predicate<A>, onFalse: (a: A) => E2): <E1, B extends A>(mb: Either<E1, B>) => Either<E1 | E2, B>
  924. <A, E2>(predicate: Predicate<A>, onFalse: (a: A) => E2): <E1>(ma: Either<E1, A>) => Either<E1 | E2, A>
  925. }
  926. /**
  927. * Returns a `Right` if is a `Left` (and vice versa).
  928. *
  929. * @since 2.0.0
  930. */
  931. export declare const swap: <E, A>(ma: Either<E, A>) => Either<A, E>
  932. /**
  933. * Less strict version of [`orElse`](#orelse).
  934. *
  935. * The `W` suffix (short for **W**idening) means that the return types will be merged.
  936. *
  937. * @category error handling
  938. * @since 2.10.0
  939. */
  940. export declare const orElseW: <E1, E2, B>(
  941. onLeft: (e: E1) => Either<E2, B>
  942. ) => <A>(ma: Either<E1, A>) => Either<E2, B | A>
  943. /**
  944. * Useful for recovering from errors.
  945. *
  946. * @category error handling
  947. * @since 2.0.0
  948. */
  949. export declare const orElse: <E1, A, E2>(onLeft: (e: E1) => Either<E2, A>) => (ma: Either<E1, A>) => Either<E2, A>
  950. /**
  951. * Takes a default and a nullable value, if the value is not nully, turn it into a `Right`, if the value is nully use
  952. * the provided default as a `Left`.
  953. *
  954. * @example
  955. * import { fromNullable, left, right } from 'fp-ts/Either'
  956. *
  957. * const parse = fromNullable('nully')
  958. *
  959. * assert.deepStrictEqual(parse(1), right(1))
  960. * assert.deepStrictEqual(parse(null), left('nully'))
  961. *
  962. * @category conversions
  963. * @since 2.0.0
  964. */
  965. export declare const fromNullable: <E>(e: E) => <A>(a: A) => Either<E, NonNullable<A>>
  966. /**
  967. * Constructs a new `Either` from a function that might throw.
  968. *
  969. * See also [`tryCatchK`](#trycatchk).
  970. *
  971. * @example
  972. * import * as E from 'fp-ts/Either'
  973. *
  974. * const unsafeHead = <A>(as: ReadonlyArray<A>): A => {
  975. * if (as.length > 0) {
  976. * return as[0]
  977. * } else {
  978. * throw new Error('empty array')
  979. * }
  980. * }
  981. *
  982. * const head = <A>(as: ReadonlyArray<A>): E.Either<Error, A> =>
  983. * E.tryCatch(() => unsafeHead(as), e => (e instanceof Error ? e : new Error('unknown error')))
  984. *
  985. * assert.deepStrictEqual(head([]), E.left(new Error('empty array')))
  986. * assert.deepStrictEqual(head([1, 2, 3]), E.right(1))
  987. *
  988. * @category interop
  989. * @since 2.0.0
  990. */
  991. export declare const tryCatch: <E, A>(f: Lazy<A>, onThrow: (e: unknown) => E) => Either<E, A>
  992. /**
  993. * Converts a function that may throw to one returning a `Either`.
  994. *
  995. * @category interop
  996. * @since 2.10.0
  997. */
  998. export declare const tryCatchK: <A extends readonly unknown[], B, E>(
  999. f: (...a: A) => B,
  1000. onThrow: (error: unknown) => E
  1001. ) => (...a: A) => Either<E, B>
  1002. /**
  1003. * @category lifting
  1004. * @since 2.9.0
  1005. */
  1006. export declare const fromNullableK: <E>(
  1007. e: E
  1008. ) => <A extends readonly unknown[], B>(f: (...a: A) => B | null | undefined) => (...a: A) => Either<E, NonNullable<B>>
  1009. /**
  1010. * @category sequencing
  1011. * @since 2.9.0
  1012. */
  1013. export declare const chainNullableK: <E>(
  1014. e: E
  1015. ) => <A, B>(f: (a: A) => B | null | undefined) => (ma: Either<E, A>) => Either<E, NonNullable<B>>
  1016. /**
  1017. * @category conversions
  1018. * @since 2.10.0
  1019. */
  1020. export declare const toUnion: <E, A>(fa: Either<E, A>) => E | A
  1021. /**
  1022. * Default value for the `onError` argument of `tryCatch`
  1023. *
  1024. * @since 2.0.0
  1025. */
  1026. export declare function toError(e: unknown): Error
  1027. /**
  1028. * @since 2.0.0
  1029. */
  1030. export declare function elem<A>(E: Eq<A>): {
  1031. (a: A): <E>(ma: Either<E, A>) => boolean
  1032. <E>(a: A, ma: Either<E, A>): boolean
  1033. }
  1034. /**
  1035. * Returns `false` if `Left` or returns the result of the application of the given predicate to the `Right` value.
  1036. *
  1037. * @example
  1038. * import { exists, left, right } from 'fp-ts/Either'
  1039. *
  1040. * const gt2 = exists((n: number) => n > 2)
  1041. *
  1042. * assert.strictEqual(gt2(left('a')), false)
  1043. * assert.strictEqual(gt2(right(1)), false)
  1044. * assert.strictEqual(gt2(right(3)), true)
  1045. *
  1046. * @since 2.0.0
  1047. */
  1048. export declare const exists: <A>(predicate: Predicate<A>) => (ma: Either<unknown, A>) => boolean
  1049. /**
  1050. * @category do notation
  1051. * @since 2.9.0
  1052. */
  1053. export declare const Do: Either<never, {}>
  1054. /**
  1055. * @category do notation
  1056. * @since 2.8.0
  1057. */
  1058. export declare const bindTo: <N extends string>(
  1059. name: N
  1060. ) => <E, A>(fa: Either<E, A>) => Either<E, { readonly [K in N]: A }>
  1061. declare const let_: <N extends string, A, B>(
  1062. name: Exclude<N, keyof A>,
  1063. f: (a: A) => B
  1064. ) => <E>(fa: Either<E, A>) => Either<E, { readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  1065. export {
  1066. /**
  1067. * @category do notation
  1068. * @since 2.13.0
  1069. */
  1070. let_ as let
  1071. }
  1072. /**
  1073. * @category do notation
  1074. * @since 2.8.0
  1075. */
  1076. export declare const bind: <N extends string, A, E, B>(
  1077. name: Exclude<N, keyof A>,
  1078. f: (a: A) => Either<E, B>
  1079. ) => (ma: Either<E, A>) => Either<E, { readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  1080. /**
  1081. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1082. *
  1083. * @category do notation
  1084. * @since 2.8.0
  1085. */
  1086. export declare const bindW: <N extends string, A, E2, B>(
  1087. name: Exclude<N, keyof A>,
  1088. f: (a: A) => Either<E2, B>
  1089. ) => <E1>(fa: Either<E1, A>) => Either<
  1090. E1 | E2,
  1091. {
  1092. readonly [K in keyof A | N]: K extends keyof A ? A[K] : B
  1093. }
  1094. >
  1095. /**
  1096. * @category do notation
  1097. * @since 2.8.0
  1098. */
  1099. export declare const apS: <N extends string, A, E, B>(
  1100. name: Exclude<N, keyof A>,
  1101. fb: Either<E, B>
  1102. ) => (fa: Either<E, A>) => Either<E, { readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  1103. /**
  1104. * Less strict version of [`apS`](#aps).
  1105. *
  1106. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1107. *
  1108. * @category do notation
  1109. * @since 2.8.0
  1110. */
  1111. export declare const apSW: <A, N extends string, E2, B>(
  1112. name: Exclude<N, keyof A>,
  1113. fb: Either<E2, B>
  1114. ) => <E1>(fa: Either<E1, A>) => Either<
  1115. E1 | E2,
  1116. {
  1117. readonly [K in keyof A | N]: K extends keyof A ? A[K] : B
  1118. }
  1119. >
  1120. /**
  1121. * @since 2.11.0
  1122. */
  1123. export declare const ApT: Either<never, readonly []>
  1124. /**
  1125. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
  1126. *
  1127. * @category traversing
  1128. * @since 2.11.0
  1129. */
  1130. export declare const traverseReadonlyNonEmptyArrayWithIndex: <A, E, B>(
  1131. f: (index: number, a: A) => Either<E, B>
  1132. ) => (as: ReadonlyNonEmptyArray<A>) => Either<E, ReadonlyNonEmptyArray<B>>
  1133. /**
  1134. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  1135. *
  1136. * @category traversing
  1137. * @since 2.11.0
  1138. */
  1139. export declare const traverseReadonlyArrayWithIndex: <A, E, B>(
  1140. f: (index: number, a: A) => Either<E, B>
  1141. ) => (as: readonly A[]) => Either<E, readonly B[]>
  1142. /**
  1143. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  1144. *
  1145. * @category traversing
  1146. * @since 2.9.0
  1147. */
  1148. export declare const traverseArrayWithIndex: <E, A, B>(
  1149. f: (index: number, a: A) => Either<E, B>
  1150. ) => (as: ReadonlyArray<A>) => Either<E, ReadonlyArray<B>>
  1151. /**
  1152. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  1153. *
  1154. * @category traversing
  1155. * @since 2.9.0
  1156. */
  1157. export declare const traverseArray: <E, A, B>(
  1158. f: (a: A) => Either<E, B>
  1159. ) => (as: readonly A[]) => Either<E, readonly B[]>
  1160. /**
  1161. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  1162. *
  1163. * @category traversing
  1164. * @since 2.9.0
  1165. */
  1166. export declare const sequenceArray: <E, A>(as: ReadonlyArray<Either<E, A>>) => Either<E, ReadonlyArray<A>>
  1167. /**
  1168. * Use [`Json`](./Json.ts.html) module instead.
  1169. *
  1170. * @category zone of death
  1171. * @since 2.6.7
  1172. * @deprecated
  1173. */
  1174. export declare type Json = boolean | number | string | null | JsonArray | JsonRecord
  1175. /**
  1176. * Use [`Json`](./Json.ts.html) module instead.
  1177. *
  1178. * @category zone of death
  1179. * @since 2.6.7
  1180. * @deprecated
  1181. */
  1182. export interface JsonRecord {
  1183. readonly [key: string]: Json
  1184. }
  1185. /**
  1186. * Use [`Json`](./Json.ts.html) module instead.
  1187. *
  1188. * @category zone of death
  1189. * @since 2.6.7
  1190. * @deprecated
  1191. */
  1192. export interface JsonArray extends ReadonlyArray<Json> {}
  1193. /**
  1194. * Use [`parse`](./Json.ts.html#parse) instead.
  1195. *
  1196. * @category zone of death
  1197. * @since 2.0.0
  1198. * @deprecated
  1199. */
  1200. export declare function parseJSON<E>(s: string, onError: (reason: unknown) => E): Either<E, Json>
  1201. /**
  1202. * Use [`stringify`](./Json.ts.html#stringify) instead.
  1203. *
  1204. * @category zone of death
  1205. * @since 2.0.0
  1206. * @deprecated
  1207. */
  1208. export declare const stringifyJSON: <E>(u: unknown, onError: (reason: unknown) => E) => Either<E, string>
  1209. /**
  1210. * This instance is deprecated, use small, specific instances instead.
  1211. * For example if a function needs a `Functor` instance, pass `E.Functor` instead of `E.either`
  1212. * (where `E` is from `import E from 'fp-ts/Either'`)
  1213. *
  1214. * @category zone of death
  1215. * @since 2.0.0
  1216. * @deprecated
  1217. */
  1218. export declare const either: Monad2<URI> &
  1219. Foldable2<URI> &
  1220. Traversable2<URI> &
  1221. Bifunctor2<URI> &
  1222. Alt2<URI> &
  1223. Extend2<URI> &
  1224. ChainRec2<URI> &
  1225. MonadThrow2<URI>
  1226. /**
  1227. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  1228. *
  1229. * Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values
  1230. * are concatenated using the provided `Semigroup`
  1231. *
  1232. * @category zone of death
  1233. * @since 2.0.0
  1234. * @deprecated
  1235. */
  1236. export declare const getApplySemigroup: <E, A>(S: Semigroup<A>) => Semigroup<Either<E, A>>
  1237. /**
  1238. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  1239. *
  1240. * @category zone of death
  1241. * @since 2.0.0
  1242. * @deprecated
  1243. */
  1244. export declare const getApplyMonoid: <E, A>(M: Monoid<A>) => Monoid<Either<E, A>>
  1245. /**
  1246. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  1247. *
  1248. * @category zone of death
  1249. * @since 2.0.0
  1250. * @deprecated
  1251. */
  1252. export declare const getValidationSemigroup: <E, A>(SE: Semigroup<E>, SA: Semigroup<A>) => Semigroup<Either<E, A>>
  1253. /**
  1254. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  1255. *
  1256. * @category zone of death
  1257. * @since 2.0.0
  1258. * @deprecated
  1259. */
  1260. export declare const getValidationMonoid: <E, A>(SE: Semigroup<E>, MA: Monoid<A>) => Monoid<Either<E, A>>
  1261. /**
  1262. * Use [`getApplicativeValidation`](#getapplicativevalidation) and [`getAltValidation`](#getaltvalidation) instead.
  1263. *
  1264. * @category zone of death
  1265. * @since 2.0.0
  1266. * @deprecated
  1267. */
  1268. export declare function getValidation<E>(
  1269. SE: Semigroup<E>
  1270. ): Monad2C<URI, E> &
  1271. Foldable2<URI> &
  1272. Traversable2<URI> &
  1273. Bifunctor2<URI> &
  1274. Alt2C<URI, E> &
  1275. Extend2<URI> &
  1276. ChainRec2C<URI, E> &
  1277. MonadThrow2C<URI, E>