版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

467 wiersze
13 KiB

  1. /**
  2. * A data structure providing "inclusive-or" as opposed to `Either`'s "exclusive-or".
  3. *
  4. * If you interpret `Either<E, A>` as suggesting the computation may either fail or succeed (exclusively), then
  5. * `These<E, A>` may fail, succeed, or do both at the same time.
  6. *
  7. * There are a few ways to interpret the both case:
  8. *
  9. * - You can think of a computation that has a non-fatal error.
  10. * - You can think of a computation that went as far as it could before erroring.
  11. * - You can think of a computation that keeps track of errors as it completes.
  12. *
  13. * Another way you can think of `These<E, A>` is saying that we want to handle `E` kind of data, `A` kind of data, or
  14. * both `E` and `A` kind of data at the same time. This is particularly useful when it comes to displaying UI's.
  15. *
  16. * (description adapted from https://package.elm-lang.org/packages/joneshf/elm-these)
  17. *
  18. * Adapted from https://github.com/purescript-contrib/purescript-these
  19. *
  20. * @since 2.0.0
  21. */
  22. import { Applicative2C } from './Applicative'
  23. import { Apply2C } from './Apply'
  24. import { Bifunctor2 } from './Bifunctor'
  25. import { Chain2C } from './Chain'
  26. import { Either, Left, Right } from './Either'
  27. import { Eq } from './Eq'
  28. import { Foldable2 } from './Foldable'
  29. import { FromEither2 } from './FromEither'
  30. import { FromThese2 } from './FromThese'
  31. import { Lazy } from './function'
  32. import { Functor2 } from './Functor'
  33. import { Monad2C } from './Monad'
  34. import { MonadThrow2C } from './MonadThrow'
  35. import { Monoid } from './Monoid'
  36. import { Option } from './Option'
  37. import { Pointed2 } from './Pointed'
  38. import { Predicate } from './Predicate'
  39. import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray'
  40. import { Refinement } from './Refinement'
  41. import { Semigroup } from './Semigroup'
  42. import { Show } from './Show'
  43. import { PipeableTraverse2, Traversable2 } from './Traversable'
  44. /**
  45. * @category model
  46. * @since 2.0.0
  47. */
  48. export interface Both<E, A> {
  49. readonly _tag: 'Both'
  50. readonly left: E
  51. readonly right: A
  52. }
  53. /**
  54. * @category model
  55. * @since 2.0.0
  56. */
  57. export declare type These<E, A> = Either<E, A> | Both<E, A>
  58. /**
  59. * Returns `true` if the these is an instance of `Left`, `false` otherwise
  60. *
  61. * @category refinements
  62. * @since 2.0.0
  63. */
  64. export declare const isLeft: <E>(fa: These<E, unknown>) => fa is Left<E>
  65. /**
  66. * Returns `true` if the these is an instance of `Right`, `false` otherwise
  67. *
  68. * @category refinements
  69. * @since 2.0.0
  70. */
  71. export declare const isRight: <A>(fa: These<unknown, A>) => fa is Right<A>
  72. /**
  73. * Returns `true` if the these is an instance of `Both`, `false` otherwise
  74. *
  75. * @category refinements
  76. * @since 2.0.0
  77. */
  78. export declare function isBoth<E, A>(fa: These<E, A>): fa is Both<E, A>
  79. /**
  80. * @category constructors
  81. * @since 2.0.0
  82. */
  83. export declare function left<E = never, A = never>(left: E): These<E, A>
  84. /**
  85. * @category constructors
  86. * @since 2.0.0
  87. */
  88. export declare function right<E = never, A = never>(right: A): These<E, A>
  89. /**
  90. * @category constructors
  91. * @since 2.0.0
  92. */
  93. export declare function both<E, A>(left: E, right: A): These<E, A>
  94. /**
  95. * Less strict version of [`match`](#match).
  96. *
  97. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  98. *
  99. * @category pattern matching
  100. * @since 2.10.0
  101. */
  102. export declare const matchW: <E, B, A, C, D>(
  103. onLeft: (e: E) => B,
  104. onRight: (a: A) => C,
  105. onBoth: (e: E, a: A) => D
  106. ) => (fa: These<E, A>) => B | C | D
  107. /**
  108. * Alias of [`matchW`](#matchw).
  109. *
  110. * @category pattern matching
  111. * @since 2.10.0
  112. */
  113. export declare const foldW: <E, B, A, C, D>(
  114. onLeft: (e: E) => B,
  115. onRight: (a: A) => C,
  116. onBoth: (e: E, a: A) => D
  117. ) => (fa: These<E, A>) => B | C | D
  118. /**
  119. * @category pattern matching
  120. * @since 2.10.0
  121. */
  122. export declare const match: <E, A, B>(
  123. onLeft: (e: E) => B,
  124. onRight: (a: A) => B,
  125. onBoth: (e: E, a: A) => B
  126. ) => (fa: These<E, A>) => B
  127. /**
  128. * Alias of [`match`](#match).
  129. *
  130. * @category pattern matching
  131. * @since 2.0.0
  132. */
  133. export declare const fold: <E, A, B>(
  134. onLeft: (e: E) => B,
  135. onRight: (a: A) => B,
  136. onBoth: (e: E, a: A) => B
  137. ) => (fa: These<E, A>) => B
  138. /**
  139. * @since 2.4.0
  140. */
  141. export declare const swap: <E, A>(fa: These<E, A>) => These<A, E>
  142. /**
  143. * @category instances
  144. * @since 2.0.0
  145. */
  146. export declare function getShow<E, A>(SE: Show<E>, SA: Show<A>): Show<These<E, A>>
  147. /**
  148. * @category instances
  149. * @since 2.0.0
  150. */
  151. export declare function getEq<E, A>(EE: Eq<E>, EA: Eq<A>): Eq<These<E, A>>
  152. /**
  153. * @category instances
  154. * @since 2.0.0
  155. */
  156. export declare function getSemigroup<E, A>(SE: Semigroup<E>, SA: Semigroup<A>): Semigroup<These<E, A>>
  157. /**
  158. * @category instances
  159. * @since 2.10.0
  160. */
  161. export declare const getApply: <E>(S: Semigroup<E>) => Apply2C<'These', E>
  162. /**
  163. * @category instances
  164. * @since 2.7.0
  165. */
  166. export declare function getApplicative<E>(S: Semigroup<E>): Applicative2C<URI, E>
  167. /**
  168. * @category instances
  169. * @since 2.10.0
  170. */
  171. export declare function getChain<E>(S: Semigroup<E>): Chain2C<URI, E>
  172. /**
  173. * @category instances
  174. * @since 2.0.0
  175. */
  176. export declare function getMonad<E>(S: Semigroup<E>): Monad2C<URI, E> & MonadThrow2C<URI, E>
  177. /**
  178. * Returns an `E` value if possible
  179. *
  180. * @example
  181. * import { getLeft, left, right, both } from 'fp-ts/These'
  182. * import { none, some } from 'fp-ts/Option'
  183. *
  184. * assert.deepStrictEqual(getLeft(left('a')), some('a'))
  185. * assert.deepStrictEqual(getLeft(right(1)), none)
  186. * assert.deepStrictEqual(getLeft(both('a', 1)), some('a'))
  187. *
  188. * @category conversions
  189. * @since 2.0.0
  190. */
  191. export declare function getLeft<E, A>(fa: These<E, A>): Option<E>
  192. /**
  193. * Returns an `A` value if possible
  194. *
  195. * @example
  196. * import { getRight, left, right, both } from 'fp-ts/These'
  197. * import { none, some } from 'fp-ts/Option'
  198. *
  199. * assert.deepStrictEqual(getRight(left('a')), none)
  200. * assert.deepStrictEqual(getRight(right(1)), some(1))
  201. * assert.deepStrictEqual(getRight(both('a', 1)), some(1))
  202. *
  203. * @category conversions
  204. * @since 2.0.0
  205. */
  206. export declare function getRight<E, A>(fa: These<E, A>): Option<A>
  207. /**
  208. * @example
  209. * import { leftOrBoth, left, both } from 'fp-ts/These'
  210. * import { none, some } from 'fp-ts/Option'
  211. *
  212. * assert.deepStrictEqual(leftOrBoth('a')(none), left('a'))
  213. * assert.deepStrictEqual(leftOrBoth('a')(some(1)), both('a', 1))
  214. *
  215. * @category constructors
  216. * @since 2.0.0
  217. */
  218. export declare function leftOrBoth<E>(e: E): <A>(ma: Option<A>) => These<E, A>
  219. /**
  220. * @example
  221. * import { rightOrBoth, right, both } from 'fp-ts/These'
  222. * import { none, some } from 'fp-ts/Option'
  223. *
  224. * assert.deepStrictEqual(rightOrBoth(1)(none), right(1))
  225. * assert.deepStrictEqual(rightOrBoth(1)(some('a')), both('a', 1))
  226. *
  227. * @category constructors
  228. * @since 2.0.0
  229. */
  230. export declare function rightOrBoth<A>(a: A): <E>(me: Option<E>) => These<E, A>
  231. /**
  232. * Returns the `E` value if and only if the value is constructed with `Left`
  233. *
  234. * @example
  235. * import { getLeftOnly, left, right, both } from 'fp-ts/These'
  236. * import { none, some } from 'fp-ts/Option'
  237. *
  238. * assert.deepStrictEqual(getLeftOnly(left('a')), some('a'))
  239. * assert.deepStrictEqual(getLeftOnly(right(1)), none)
  240. * assert.deepStrictEqual(getLeftOnly(both('a', 1)), none)
  241. *
  242. * @category conversions
  243. * @since 2.0.0
  244. */
  245. export declare function getLeftOnly<E, A>(fa: These<E, A>): Option<E>
  246. /**
  247. * Returns the `A` value if and only if the value is constructed with `Right`
  248. *
  249. * @example
  250. * import { getRightOnly, left, right, both } from 'fp-ts/These'
  251. * import { none, some } from 'fp-ts/Option'
  252. *
  253. * assert.deepStrictEqual(getRightOnly(left('a')), none)
  254. * assert.deepStrictEqual(getRightOnly(right(1)), some(1))
  255. * assert.deepStrictEqual(getRightOnly(both('a', 1)), none)
  256. *
  257. * @category conversions
  258. * @since 2.0.0
  259. */
  260. export declare function getRightOnly<E, A>(fa: These<E, A>): Option<A>
  261. /**
  262. * Takes a pair of `Option`s and attempts to create a `These` from them
  263. *
  264. * @example
  265. * import { fromOptions, left, right, both } from 'fp-ts/These'
  266. * import { none, some } from 'fp-ts/Option'
  267. *
  268. * assert.deepStrictEqual(fromOptions(none, none), none)
  269. * assert.deepStrictEqual(fromOptions(some('a'), none), some(left('a')))
  270. * assert.deepStrictEqual(fromOptions(none, some(1)), some(right(1)))
  271. * assert.deepStrictEqual(fromOptions(some('a'), some(1)), some(both('a', 1)))
  272. *
  273. * @category conversions
  274. * @since 2.0.0
  275. */
  276. export declare const fromOptions: <E, A>(fe: Option<E>, fa: Option<A>) => Option<These<E, A>>
  277. /**
  278. * Map a pair of functions over the two type arguments of the bifunctor.
  279. *
  280. * @category mapping
  281. * @since 2.0.0
  282. */
  283. export declare const bimap: <E, G, A, B>(f: (e: E) => G, g: (a: A) => B) => (fa: These<E, A>) => These<G, B>
  284. /**
  285. * Map a function over the first type argument of a bifunctor.
  286. *
  287. * @category error handling
  288. * @since 2.0.0
  289. */
  290. export declare const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: These<E, A>) => These<G, A>
  291. /**
  292. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  293. * use the type constructor `F` to represent some computational context.
  294. *
  295. * @category mapping
  296. * @since 2.0.0
  297. */
  298. export declare const map: <A, B>(f: (a: A) => B) => <E>(fa: These<E, A>) => These<E, B>
  299. /**
  300. * @category folding
  301. * @since 2.0.0
  302. */
  303. export declare const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => <E>(fa: These<E, A>) => B
  304. /**
  305. * @category folding
  306. * @since 2.0.0
  307. */
  308. export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => <E>(fa: These<E, A>) => M
  309. /**
  310. * @category folding
  311. * @since 2.0.0
  312. */
  313. export declare const reduceRight: <A, B>(b: B, f: (a: A, b: B) => B) => <E>(fa: These<E, A>) => B
  314. /**
  315. * @category traversing
  316. * @since 2.6.3
  317. */
  318. export declare const traverse: PipeableTraverse2<URI>
  319. /**
  320. * @category traversing
  321. * @since 2.6.3
  322. */
  323. export declare const sequence: Traversable2<URI>['sequence']
  324. /**
  325. * @category constructors
  326. * @since 2.0.0
  327. */
  328. export declare const of: <E = never, A = never>(right: A) => These<E, A>
  329. /**
  330. * @category type lambdas
  331. * @since 2.0.0
  332. */
  333. export declare const URI = 'These'
  334. /**
  335. * @category type lambdas
  336. * @since 2.0.0
  337. */
  338. export declare type URI = typeof URI
  339. declare module './HKT' {
  340. interface URItoKind2<E, A> {
  341. readonly [URI]: These<E, A>
  342. }
  343. }
  344. /**
  345. * @category instances
  346. * @since 2.7.0
  347. */
  348. export declare const Functor: Functor2<URI>
  349. /**
  350. * @category mapping
  351. * @since 2.10.0
  352. */
  353. export declare const flap: <A>(a: A) => <E, B>(fab: These<E, (a: A) => B>) => These<E, B>
  354. /**
  355. * @category instances
  356. * @since 2.10.0
  357. */
  358. export declare const Pointed: Pointed2<URI>
  359. /**
  360. * @category instances
  361. * @since 2.7.0
  362. */
  363. export declare const Bifunctor: Bifunctor2<URI>
  364. /**
  365. * @category instances
  366. * @since 2.11.0
  367. */
  368. export declare const FromThese: FromThese2<URI>
  369. /**
  370. * @category instances
  371. * @since 2.7.0
  372. */
  373. export declare const Foldable: Foldable2<URI>
  374. /**
  375. * @category instances
  376. * @since 2.7.0
  377. */
  378. export declare const Traversable: Traversable2<URI>
  379. /**
  380. * @category instances
  381. * @since 2.10.0
  382. */
  383. export declare const FromEither: FromEither2<URI>
  384. /**
  385. * @category lifting
  386. * @since 2.13.0
  387. */
  388. export declare const fromPredicate: {
  389. <A, B extends A, E>(refinement: Refinement<A, B>, onFalse: (a: A) => E): (a: A) => These<E, B>
  390. <A, E>(predicate: Predicate<A>, onFalse: (a: A) => E): <B extends A>(b: B) => These<E, B>
  391. <A, E>(predicate: Predicate<A>, onFalse: (a: A) => E): (a: A) => These<E, A>
  392. }
  393. /**
  394. * @category conversions
  395. * @since 2.10.0
  396. */
  397. export declare const fromOption: <E>(onNone: Lazy<E>) => <A>(fa: Option<A>) => These<E, A>
  398. /**
  399. * @category lifting
  400. * @since 2.10.0
  401. */
  402. export declare const fromOptionK: <E>(
  403. onNone: Lazy<E>
  404. ) => <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => Option<B>) => (...a: A) => These<E, B>
  405. /**
  406. * @since 2.11.0
  407. */
  408. export declare const elem: <A>(E: Eq<A>) => (a: A) => <E>(ma: These<E, A>) => boolean
  409. /**
  410. * @since 2.11.0
  411. */
  412. export declare const exists: <A>(predicate: Predicate<A>) => (ma: These<unknown, A>) => boolean
  413. /**
  414. * @example
  415. * import { toTuple2, left, right, both } from 'fp-ts/These'
  416. *
  417. * assert.deepStrictEqual(toTuple2(() => 'a', () => 1)(left('b')), ['b', 1])
  418. * assert.deepStrictEqual(toTuple2(() => 'a', () => 1)(right(2)), ['a', 2])
  419. * assert.deepStrictEqual(toTuple2(() => 'a', () => 1)(both('b', 2)), ['b', 2])
  420. *
  421. * @category conversions
  422. * @since 2.10.0
  423. */
  424. export declare const toTuple2: <E, A>(e: Lazy<E>, a: Lazy<A>) => (fa: These<E, A>) => readonly [E, A]
  425. /**
  426. * Use [`toTuple2`](#totuple2) instead.
  427. *
  428. * @category zone of death
  429. * @since 2.0.0
  430. * @deprecated
  431. */
  432. export declare const toTuple: <E, A>(e: E, a: A) => (fa: These<E, A>) => [E, A]
  433. /**
  434. * @since 2.11.0
  435. */
  436. export declare const ApT: These<never, readonly []>
  437. /**
  438. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(getApplicative(S))`.
  439. *
  440. * @category traversing
  441. * @since 2.11.0
  442. */
  443. export declare const traverseReadonlyNonEmptyArrayWithIndex: <E>(
  444. S: Semigroup<E>
  445. ) => <A, B>(
  446. f: (index: number, a: A) => These<E, B>
  447. ) => (as: ReadonlyNonEmptyArray<A>) => These<E, ReadonlyNonEmptyArray<B>>
  448. /**
  449. * Equivalent to `ReadonlyArray#traverseWithIndex(getApplicative(S))`.
  450. *
  451. * @category traversing
  452. * @since 2.11.0
  453. */
  454. export declare const traverseReadonlyArrayWithIndex: <E>(
  455. S: Semigroup<E>
  456. ) => <A, B>(f: (index: number, a: A) => These<E, B>) => (as: readonly A[]) => These<E, readonly B[]>
  457. /**
  458. * This instance is deprecated, use small, specific instances instead.
  459. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.these`
  460. * (where `T` is from `import T from 'fp-ts/These'`)
  461. *
  462. * @category zone of death
  463. * @since 2.0.0
  464. * @deprecated
  465. */
  466. export declare const these: Functor2<URI> & Bifunctor2<URI> & Foldable2<URI> & Traversable2<URI>