版博士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.

Option.d.ts 29 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. /**
  2. * ```ts
  3. * type Option<A> = None | Some<A>
  4. * ```
  5. *
  6. * `Option<A>` is a container for an optional value of type `A`. If the value of type `A` is present, the `Option<A>` is
  7. * an instance of `Some<A>`, containing the present value of type `A`. If the value is absent, the `Option<A>` is an
  8. * instance of `None`.
  9. *
  10. * An option could be looked at as a collection or foldable structure with either one or zero elements.
  11. * Another way to look at `Option` is: it represents the effect of a possibly failing computation.
  12. *
  13. *
  14. * @example
  15. * import * as O from 'fp-ts/Option'
  16. * import { pipe } from 'fp-ts/function'
  17. *
  18. * const double = (n: number): number => n * 2
  19. *
  20. * export const imperative = (as: ReadonlyArray<number>): string => {
  21. * const head = (as: ReadonlyArray<number>): number => {
  22. * if (as.length === 0) {
  23. * throw new Error()
  24. * }
  25. * return as[0]
  26. * }
  27. * const inverse = (n: number): number => {
  28. * if (n === 0) {
  29. * throw new Error()
  30. * }
  31. * return 1 / n
  32. * }
  33. * try {
  34. * return `Result is ${inverse(double(head(as)))}`
  35. * } catch (e) {
  36. * return 'no result'
  37. * }
  38. * }
  39. *
  40. * export const functional = (as: ReadonlyArray<number>): string => {
  41. * const head = <A>(as: ReadonlyArray<A>): O.Option<A> =>
  42. * as.length === 0 ? O.none : O.some(as[0])
  43. * const inverse = (n: number): O.Option<number> =>
  44. * n === 0 ? O.none : O.some(1 / n)
  45. * return pipe(
  46. * as,
  47. * head,
  48. * O.map(double),
  49. * O.chain(inverse),
  50. * O.match(
  51. * () => 'no result', // onNone handler
  52. * (head) => `Result is ${head}` // onSome handler
  53. * )
  54. * )
  55. * }
  56. *
  57. * assert.deepStrictEqual(imperative([1, 2, 3]), functional([1, 2, 3]))
  58. * assert.deepStrictEqual(imperative([]), functional([]))
  59. * assert.deepStrictEqual(imperative([0]), functional([0]))
  60. *
  61. * @since 2.0.0
  62. */
  63. import { Alt1 } from './Alt'
  64. import { Alternative1 } from './Alternative'
  65. import { Applicative1 } from './Applicative'
  66. import { Apply1 } from './Apply'
  67. import { Chain1 } from './Chain'
  68. import { Compactable1 } from './Compactable'
  69. import { Either } from './Either'
  70. import { Eq } from './Eq'
  71. import { Extend1 } from './Extend'
  72. import { Filterable1 } from './Filterable'
  73. import { Foldable1 } from './Foldable'
  74. import { FromEither1 } from './FromEither'
  75. import { Lazy } from './function'
  76. import { Functor1 } from './Functor'
  77. import { Monad1 } from './Monad'
  78. import { MonadThrow1 } from './MonadThrow'
  79. import { Monoid } from './Monoid'
  80. import { Ord } from './Ord'
  81. import { Pointed1 } from './Pointed'
  82. import { Predicate } from './Predicate'
  83. import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray'
  84. import { Refinement } from './Refinement'
  85. import { Semigroup } from './Semigroup'
  86. import { Separated } from './Separated'
  87. import { Show } from './Show'
  88. import { PipeableTraverse1, Traversable1 } from './Traversable'
  89. import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable'
  90. import { Zero1 } from './Zero'
  91. /**
  92. * @category model
  93. * @since 2.0.0
  94. */
  95. export interface None {
  96. readonly _tag: 'None'
  97. }
  98. /**
  99. * @category model
  100. * @since 2.0.0
  101. */
  102. export interface Some<A> {
  103. readonly _tag: 'Some'
  104. readonly value: A
  105. }
  106. /**
  107. * @category model
  108. * @since 2.0.0
  109. */
  110. export declare type Option<A> = None | Some<A>
  111. /**
  112. * `None` doesn't have a constructor, instead you can use it directly as a value. Represents a missing value.
  113. *
  114. * @category constructors
  115. * @since 2.0.0
  116. */
  117. export declare const none: Option<never>
  118. /**
  119. * Constructs a `Some`. Represents an optional value that exists.
  120. *
  121. * @category constructors
  122. * @since 2.0.0
  123. */
  124. export declare const some: <A>(a: A) => Option<A>
  125. /**
  126. * Returns a *smart constructor* based on the given predicate.
  127. *
  128. * @example
  129. * import { none, some, fromPredicate } from 'fp-ts/Option'
  130. *
  131. * const getOption = fromPredicate((n: number) => n >= 0)
  132. *
  133. * assert.deepStrictEqual(getOption(-1), none)
  134. * assert.deepStrictEqual(getOption(1), some(1))
  135. *
  136. * @category lifting
  137. * @since 2.0.0
  138. */
  139. export declare function fromPredicate<A, B extends A>(refinement: Refinement<A, B>): (a: A) => Option<B>
  140. export declare function fromPredicate<A>(predicate: Predicate<A>): <B extends A>(b: B) => Option<B>
  141. export declare function fromPredicate<A>(predicate: Predicate<A>): (a: A) => Option<A>
  142. /**
  143. * Returns the `Left` value of an `Either` if possible.
  144. *
  145. * @example
  146. * import { getLeft, none, some } from 'fp-ts/Option'
  147. * import { right, left } from 'fp-ts/Either'
  148. *
  149. * assert.deepStrictEqual(getLeft(right(1)), none)
  150. * assert.deepStrictEqual(getLeft(left('a')), some('a'))
  151. *
  152. * @category constructors
  153. * @since 2.0.0
  154. */
  155. export declare const getLeft: <E, A>(ma: Either<E, A>) => Option<E>
  156. /**
  157. * Returns the `Right` value of an `Either` if possible.
  158. *
  159. * @example
  160. * import { getRight, none, some } from 'fp-ts/Option'
  161. * import { right, left } from 'fp-ts/Either'
  162. *
  163. * assert.deepStrictEqual(getRight(right(1)), some(1))
  164. * assert.deepStrictEqual(getRight(left('a')), none)
  165. *
  166. * @category constructors
  167. * @since 2.0.0
  168. */
  169. export declare const getRight: <E, A>(ma: Either<E, A>) => Option<A>
  170. /**
  171. * @category type lambdas
  172. * @since 2.0.0
  173. */
  174. export declare const URI = 'Option'
  175. /**
  176. * @category type lambdas
  177. * @since 2.0.0
  178. */
  179. export declare type URI = typeof URI
  180. declare module './HKT' {
  181. interface URItoKind<A> {
  182. readonly [URI]: Option<A>
  183. }
  184. }
  185. /**
  186. * @category instances
  187. * @since 2.0.0
  188. */
  189. export declare const getShow: <A>(S: Show<A>) => Show<Option<A>>
  190. /**
  191. * @example
  192. * import { none, some, getEq } from 'fp-ts/Option'
  193. * import * as N from 'fp-ts/number'
  194. *
  195. * const E = getEq(N.Eq)
  196. * assert.strictEqual(E.equals(none, none), true)
  197. * assert.strictEqual(E.equals(none, some(1)), false)
  198. * assert.strictEqual(E.equals(some(1), none), false)
  199. * assert.strictEqual(E.equals(some(1), some(2)), false)
  200. * assert.strictEqual(E.equals(some(1), some(1)), true)
  201. *
  202. * @category instances
  203. * @since 2.0.0
  204. */
  205. export declare const getEq: <A>(E: Eq<A>) => Eq<Option<A>>
  206. /**
  207. * The `Ord` instance allows `Option` values to be compared with
  208. * `compare`, whenever there is an `Ord` instance for
  209. * the type the `Option` contains.
  210. *
  211. * `None` is considered to be less than any `Some` value.
  212. *
  213. *
  214. * @example
  215. * import { none, some, getOrd } from 'fp-ts/Option'
  216. * import * as N from 'fp-ts/number'
  217. *
  218. * const O = getOrd(N.Ord)
  219. * assert.strictEqual(O.compare(none, none), 0)
  220. * assert.strictEqual(O.compare(none, some(1)), -1)
  221. * assert.strictEqual(O.compare(some(1), none), 1)
  222. * assert.strictEqual(O.compare(some(1), some(2)), -1)
  223. * assert.strictEqual(O.compare(some(1), some(1)), 0)
  224. *
  225. * @category instances
  226. * @since 2.0.0
  227. */
  228. export declare const getOrd: <A>(O: Ord<A>) => Ord<Option<A>>
  229. /**
  230. * Monoid returning the left-most non-`None` value. If both operands are `Some`s then the inner values are
  231. * concatenated using the provided `Semigroup`
  232. *
  233. * | x | y | concat(x, y) |
  234. * | ------- | ------- | ------------------ |
  235. * | none | none | none |
  236. * | some(a) | none | some(a) |
  237. * | none | some(b) | some(b) |
  238. * | some(a) | some(b) | some(concat(a, b)) |
  239. *
  240. * @example
  241. * import { getMonoid, some, none } from 'fp-ts/Option'
  242. * import { SemigroupSum } from 'fp-ts/number'
  243. *
  244. * const M = getMonoid(SemigroupSum)
  245. * assert.deepStrictEqual(M.concat(none, none), none)
  246. * assert.deepStrictEqual(M.concat(some(1), none), some(1))
  247. * assert.deepStrictEqual(M.concat(none, some(1)), some(1))
  248. * assert.deepStrictEqual(M.concat(some(1), some(2)), some(3))
  249. *
  250. * @category instances
  251. * @since 2.0.0
  252. */
  253. export declare const getMonoid: <A>(S: Semigroup<A>) => Monoid<Option<A>>
  254. /**
  255. * @category mapping
  256. * @since 2.0.0
  257. */
  258. export declare const map: <A, B>(f: (a: A) => B) => (fa: Option<A>) => Option<B>
  259. /**
  260. * @category instances
  261. * @since 2.7.0
  262. */
  263. export declare const Functor: Functor1<URI>
  264. /**
  265. * @category constructors
  266. * @since 2.7.0
  267. */
  268. export declare const of: <A>(a: A) => Option<A>
  269. /**
  270. * @category instances
  271. * @since 2.10.0
  272. */
  273. export declare const Pointed: Pointed1<URI>
  274. /**
  275. * @since 2.0.0
  276. */
  277. export declare const ap: <A>(fa: Option<A>) => <B>(fab: Option<(a: A) => B>) => Option<B>
  278. /**
  279. * @category instances
  280. * @since 2.10.0
  281. */
  282. export declare const Apply: Apply1<URI>
  283. /**
  284. * @category instances
  285. * @since 2.7.0
  286. */
  287. export declare const Applicative: Applicative1<URI>
  288. /**
  289. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  290. *
  291. * @category sequencing
  292. * @since 2.0.0
  293. */
  294. export declare const chain: <A, B>(f: (a: A) => Option<B>) => (ma: Option<A>) => Option<B>
  295. /**
  296. * @category instances
  297. * @since 2.10.0
  298. */
  299. export declare const Chain: Chain1<URI>
  300. /**
  301. * @category instances
  302. * @since 2.7.0
  303. */
  304. export declare const Monad: Monad1<URI>
  305. /**
  306. * @category folding
  307. * @since 2.0.0
  308. */
  309. export declare const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => (fa: Option<A>) => B
  310. /**
  311. * @category folding
  312. * @since 2.0.0
  313. */
  314. export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => (fa: Option<A>) => M
  315. /**
  316. * @category folding
  317. * @since 2.0.0
  318. */
  319. export declare const reduceRight: <A, B>(b: B, f: (a: A, b: B) => B) => (fa: Option<A>) => B
  320. /**
  321. * @category instances
  322. * @since 2.7.0
  323. */
  324. export declare const Foldable: Foldable1<URI>
  325. /**
  326. * Less strict version of [`alt`](#alt).
  327. *
  328. * The `W` suffix (short for **W**idening) means that the return types will be merged.
  329. *
  330. * @category error handling
  331. * @since 2.9.0
  332. */
  333. export declare const altW: <B>(that: Lazy<Option<B>>) => <A>(fa: Option<A>) => Option<A | B>
  334. /**
  335. * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
  336. * types of kind `* -> *`.
  337. *
  338. * In case of `Option` returns the left-most non-`None` value.
  339. *
  340. * | x | y | pipe(x, alt(() => y) |
  341. * | ------- | ------- | -------------------- |
  342. * | none | none | none |
  343. * | some(a) | none | some(a) |
  344. * | none | some(b) | some(b) |
  345. * | some(a) | some(b) | some(a) |
  346. *
  347. * @example
  348. * import * as O from 'fp-ts/Option'
  349. * import { pipe } from 'fp-ts/function'
  350. *
  351. * assert.deepStrictEqual(
  352. * pipe(
  353. * O.none,
  354. * O.alt(() => O.none)
  355. * ),
  356. * O.none
  357. * )
  358. * assert.deepStrictEqual(
  359. * pipe(
  360. * O.some('a'),
  361. * O.alt<string>(() => O.none)
  362. * ),
  363. * O.some('a')
  364. * )
  365. * assert.deepStrictEqual(
  366. * pipe(
  367. * O.none,
  368. * O.alt(() => O.some('b'))
  369. * ),
  370. * O.some('b')
  371. * )
  372. * assert.deepStrictEqual(
  373. * pipe(
  374. * O.some('a'),
  375. * O.alt(() => O.some('b'))
  376. * ),
  377. * O.some('a')
  378. * )
  379. *
  380. * @category error handling
  381. * @since 2.0.0
  382. */
  383. export declare const alt: <A>(that: Lazy<Option<A>>) => (fa: Option<A>) => Option<A>
  384. /**
  385. * @category instances
  386. * @since 2.7.0
  387. */
  388. export declare const Alt: Alt1<URI>
  389. /**
  390. * @since 2.7.0
  391. */
  392. export declare const zero: <A>() => Option<A>
  393. /**
  394. * @category instances
  395. * @since 2.11.0
  396. */
  397. export declare const Zero: Zero1<URI>
  398. /**
  399. * @category do notation
  400. * @since 2.11.0
  401. */
  402. export declare const guard: (b: boolean) => Option<void>
  403. /**
  404. * @category instances
  405. * @since 2.7.0
  406. */
  407. export declare const Alternative: Alternative1<URI>
  408. /**
  409. * @since 2.0.0
  410. */
  411. export declare const extend: <A, B>(f: (wa: Option<A>) => B) => (wa: Option<A>) => Option<B>
  412. /**
  413. * @category instances
  414. * @since 2.7.0
  415. */
  416. export declare const Extend: Extend1<URI>
  417. /**
  418. * @category filtering
  419. * @since 2.0.0
  420. */
  421. export declare const compact: <A>(fa: Option<Option<A>>) => Option<A>
  422. /**
  423. * @category filtering
  424. * @since 2.0.0
  425. */
  426. export declare const separate: <A, B>(ma: Option<Either<A, B>>) => Separated<Option<A>, Option<B>>
  427. /**
  428. * @category instances
  429. * @since 2.7.0
  430. */
  431. export declare const Compactable: Compactable1<URI>
  432. /**
  433. * @category filtering
  434. * @since 2.0.0
  435. */
  436. export declare const filter: {
  437. <A, B extends A>(refinement: Refinement<A, B>): (fa: Option<A>) => Option<B>
  438. <A>(predicate: Predicate<A>): <B extends A>(fb: Option<B>) => Option<B>
  439. <A>(predicate: Predicate<A>): (fa: Option<A>) => Option<A>
  440. }
  441. /**
  442. * @category filtering
  443. * @since 2.0.0
  444. */
  445. export declare const filterMap: <A, B>(f: (a: A) => Option<B>) => (fa: Option<A>) => Option<B>
  446. /**
  447. * @category filtering
  448. * @since 2.0.0
  449. */
  450. export declare const partition: {
  451. <A, B extends A>(refinement: Refinement<A, B>): (fa: Option<A>) => Separated<Option<A>, Option<B>>
  452. <A>(predicate: Predicate<A>): <B extends A>(fb: Option<B>) => Separated<Option<B>, Option<B>>
  453. <A>(predicate: Predicate<A>): (fa: Option<A>) => Separated<Option<A>, Option<A>>
  454. }
  455. /**
  456. * @category filtering
  457. * @since 2.0.0
  458. */
  459. export declare const partitionMap: <A, B, C>(
  460. f: (a: A) => Either<B, C>
  461. ) => (fa: Option<A>) => Separated<Option<B>, Option<C>>
  462. /**
  463. * @category instances
  464. * @since 2.7.0
  465. */
  466. export declare const Filterable: Filterable1<URI>
  467. /**
  468. * @category traversing
  469. * @since 2.6.3
  470. */
  471. export declare const traverse: PipeableTraverse1<URI>
  472. /**
  473. * @category traversing
  474. * @since 2.6.3
  475. */
  476. export declare const sequence: Traversable1<URI>['sequence']
  477. /**
  478. * @category instances
  479. * @since 2.7.0
  480. */
  481. export declare const Traversable: Traversable1<URI>
  482. /**
  483. * @category filtering
  484. * @since 2.6.5
  485. */
  486. export declare const wither: PipeableWither1<URI>
  487. /**
  488. * @category filtering
  489. * @since 2.6.5
  490. */
  491. export declare const wilt: PipeableWilt1<URI>
  492. /**
  493. * @category instances
  494. * @since 2.7.0
  495. */
  496. export declare const Witherable: Witherable1<URI>
  497. /**
  498. * @since 2.7.0
  499. */
  500. export declare const throwError: MonadThrow1<URI>['throwError']
  501. /**
  502. * @category instances
  503. * @since 2.7.0
  504. */
  505. export declare const MonadThrow: MonadThrow1<URI>
  506. /**
  507. * Transforms an `Either` to an `Option` discarding the error.
  508. *
  509. * Alias of [getRight](#getright)
  510. *
  511. * @category conversions
  512. * @since 2.0.0
  513. */
  514. export declare const fromEither: <A>(fa: Either<unknown, A>) => Option<A>
  515. /**
  516. * @category instances
  517. * @since 2.11.0
  518. */
  519. export declare const FromEither: FromEither1<URI>
  520. /**
  521. * Returns `true` if the option is an instance of `Some`, `false` otherwise.
  522. *
  523. * @example
  524. * import { some, none, isSome } from 'fp-ts/Option'
  525. *
  526. * assert.strictEqual(isSome(some(1)), true)
  527. * assert.strictEqual(isSome(none), false)
  528. *
  529. * @category refinements
  530. * @since 2.0.0
  531. */
  532. export declare const isSome: <A>(fa: Option<A>) => fa is Some<A>
  533. /**
  534. * Returns `true` if the option is `None`, `false` otherwise.
  535. *
  536. * @example
  537. * import { some, none, isNone } from 'fp-ts/Option'
  538. *
  539. * assert.strictEqual(isNone(some(1)), false)
  540. * assert.strictEqual(isNone(none), true)
  541. *
  542. * @category refinements
  543. * @since 2.0.0
  544. */
  545. export declare const isNone: (fa: Option<unknown>) => fa is None
  546. /**
  547. * Less strict version of [`match`](#match).
  548. *
  549. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  550. *
  551. * @category pattern matching
  552. * @since 2.10.0
  553. */
  554. export declare const matchW: <B, A, C>(onNone: Lazy<B>, onSome: (a: A) => C) => (ma: Option<A>) => B | C
  555. /**
  556. * Alias of [`matchW`](#matchw).
  557. *
  558. * @category pattern matching
  559. * @since 2.10.0
  560. */
  561. export declare const foldW: <B, A, C>(onNone: Lazy<B>, onSome: (a: A) => C) => (ma: Option<A>) => B | C
  562. /**
  563. * Takes a (lazy) default value, a function, and an `Option` value, if the `Option` value is `None` the default value is
  564. * returned, otherwise the function is applied to the value inside the `Some` and the result is returned.
  565. *
  566. * @example
  567. * import { some, none, match } from 'fp-ts/Option'
  568. * import { pipe } from 'fp-ts/function'
  569. *
  570. * assert.strictEqual(
  571. * pipe(
  572. * some(1),
  573. * match(() => 'a none', a => `a some containing ${a}`)
  574. * ),
  575. * 'a some containing 1'
  576. * )
  577. *
  578. * assert.strictEqual(
  579. * pipe(
  580. * none,
  581. * match(() => 'a none', a => `a some containing ${a}`)
  582. * ),
  583. * 'a none'
  584. * )
  585. *
  586. * @category pattern matching
  587. * @since 2.10.0
  588. */
  589. export declare const match: <A, B>(onNone: Lazy<B>, onSome: (a: A) => B) => (ma: Option<A>) => B
  590. /**
  591. * Alias of [`match`](#match).
  592. *
  593. * @category pattern matching
  594. * @since 2.0.0
  595. */
  596. export declare const fold: <A, B>(onNone: Lazy<B>, onSome: (a: A) => B) => (ma: Option<A>) => B
  597. /**
  598. * Less strict version of [`getOrElse`](#getorelse).
  599. *
  600. * The `W` suffix (short for **W**idening) means that the handler return type will be merged.
  601. *
  602. * @category error handling
  603. * @since 2.6.0
  604. */
  605. export declare const getOrElseW: <B>(onNone: Lazy<B>) => <A>(ma: Option<A>) => B | A
  606. /**
  607. * Extracts the value out of the structure, if it exists. Otherwise returns the given default value
  608. *
  609. * @example
  610. * import { some, none, getOrElse } from 'fp-ts/Option'
  611. * import { pipe } from 'fp-ts/function'
  612. *
  613. * assert.strictEqual(
  614. * pipe(
  615. * some(1),
  616. * getOrElse(() => 0)
  617. * ),
  618. * 1
  619. * )
  620. * assert.strictEqual(
  621. * pipe(
  622. * none,
  623. * getOrElse(() => 0)
  624. * ),
  625. * 0
  626. * )
  627. *
  628. * @category error handling
  629. * @since 2.0.0
  630. */
  631. export declare const getOrElse: <A>(onNone: Lazy<A>) => (ma: Option<A>) => A
  632. /**
  633. * @category mapping
  634. * @since 2.10.0
  635. */
  636. export declare const flap: <A>(a: A) => <B>(fab: Option<(a: A) => B>) => Option<B>
  637. /**
  638. * Combine two effectful actions, keeping only the result of the first.
  639. *
  640. * @since 2.0.0
  641. */
  642. export declare const apFirst: <B>(second: Option<B>) => <A>(first: Option<A>) => Option<A>
  643. /**
  644. * Combine two effectful actions, keeping only the result of the second.
  645. *
  646. * @since 2.0.0
  647. */
  648. export declare const apSecond: <B>(second: Option<B>) => <A>(first: Option<A>) => Option<B>
  649. /**
  650. * @category sequencing
  651. * @since 2.0.0
  652. */
  653. export declare const flatten: <A>(mma: Option<Option<A>>) => Option<A>
  654. /**
  655. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  656. * keeping only the result of the first.
  657. *
  658. * @category sequencing
  659. * @since 2.0.0
  660. */
  661. export declare const chainFirst: <A, B>(f: (a: A) => Option<B>) => (first: Option<A>) => Option<A>
  662. /**
  663. * @since 2.0.0
  664. */
  665. export declare const duplicate: <A>(ma: Option<A>) => Option<Option<A>>
  666. /**
  667. * @category lifting
  668. * @since 2.11.0
  669. */
  670. export declare const fromEitherK: <E, A extends ReadonlyArray<unknown>, B>(
  671. f: (...a: A) => Either<E, B>
  672. ) => (...a: A) => Option<B>
  673. /**
  674. * @category sequencing
  675. * @since 2.11.0
  676. */
  677. export declare const chainEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: Option<A>) => Option<B>
  678. /**
  679. * @category sequencing
  680. * @since 2.12.0
  681. */
  682. export declare const chainFirstEitherK: <E, A, B>(f: (a: A) => Either<E, B>) => (ma: Option<A>) => Option<A>
  683. /**
  684. * Constructs a new `Option` from a nullable type. If the value is `null` or `undefined`, returns `None`, otherwise
  685. * returns the value wrapped in a `Some`.
  686. *
  687. * @example
  688. * import { none, some, fromNullable } from 'fp-ts/Option'
  689. *
  690. * assert.deepStrictEqual(fromNullable(undefined), none)
  691. * assert.deepStrictEqual(fromNullable(null), none)
  692. * assert.deepStrictEqual(fromNullable(1), some(1))
  693. *
  694. * @category conversions
  695. * @since 2.0.0
  696. */
  697. export declare const fromNullable: <A>(a: A) => Option<NonNullable<A>>
  698. /**
  699. * Transforms an exception into an `Option`. If `f` throws, returns `None`, otherwise returns the output wrapped in a
  700. * `Some`.
  701. *
  702. * See also [`tryCatchK`](#trycatchk).
  703. *
  704. * @example
  705. * import { none, some, tryCatch } from 'fp-ts/Option'
  706. *
  707. * assert.deepStrictEqual(
  708. * tryCatch(() => {
  709. * throw new Error()
  710. * }),
  711. * none
  712. * )
  713. * assert.deepStrictEqual(tryCatch(() => 1), some(1))
  714. *
  715. * @category interop
  716. * @since 2.0.0
  717. */
  718. export declare const tryCatch: <A>(f: Lazy<A>) => Option<A>
  719. /**
  720. * Converts a function that may throw to one returning a `Option`.
  721. *
  722. * @category interop
  723. * @since 2.10.0
  724. */
  725. export declare const tryCatchK: <A extends readonly unknown[], B>(f: (...a: A) => B) => (...a: A) => Option<B>
  726. /**
  727. * Returns a *smart constructor* from a function that returns a nullable value.
  728. *
  729. * @example
  730. * import { fromNullableK, none, some } from 'fp-ts/Option'
  731. *
  732. * const f = (s: string): number | undefined => {
  733. * const n = parseFloat(s)
  734. * return isNaN(n) ? undefined : n
  735. * }
  736. *
  737. * const g = fromNullableK(f)
  738. *
  739. * assert.deepStrictEqual(g('1'), some(1))
  740. * assert.deepStrictEqual(g('a'), none)
  741. *
  742. * @category lifting
  743. * @since 2.9.0
  744. */
  745. export declare const fromNullableK: <A extends ReadonlyArray<unknown>, B>(
  746. f: (...a: A) => B | null | undefined
  747. ) => (...a: A) => Option<NonNullable<B>>
  748. /**
  749. * This is `chain` + `fromNullable`, useful when working with optional values.
  750. *
  751. * @example
  752. * import { some, none, fromNullable, chainNullableK } from 'fp-ts/Option'
  753. * import { pipe } from 'fp-ts/function'
  754. *
  755. * interface Employee {
  756. * readonly company?: {
  757. * readonly address?: {
  758. * readonly street?: {
  759. * readonly name?: string
  760. * }
  761. * }
  762. * }
  763. * }
  764. *
  765. * const employee1: Employee = { company: { address: { street: { name: 'high street' } } } }
  766. *
  767. * assert.deepStrictEqual(
  768. * pipe(
  769. * fromNullable(employee1.company),
  770. * chainNullableK(company => company.address),
  771. * chainNullableK(address => address.street),
  772. * chainNullableK(street => street.name)
  773. * ),
  774. * some('high street')
  775. * )
  776. *
  777. * const employee2: Employee = { company: { address: { street: {} } } }
  778. *
  779. * assert.deepStrictEqual(
  780. * pipe(
  781. * fromNullable(employee2.company),
  782. * chainNullableK(company => company.address),
  783. * chainNullableK(address => address.street),
  784. * chainNullableK(street => street.name)
  785. * ),
  786. * none
  787. * )
  788. *
  789. * @category sequencing
  790. * @since 2.9.0
  791. */
  792. export declare const chainNullableK: <A, B>(
  793. f: (a: A) => B | null | undefined
  794. ) => (ma: Option<A>) => Option<NonNullable<B>>
  795. /**
  796. * Extracts the value out of the structure, if it exists. Otherwise returns `null`.
  797. *
  798. * @example
  799. * import { some, none, toNullable } from 'fp-ts/Option'
  800. * import { pipe } from 'fp-ts/function'
  801. *
  802. * assert.strictEqual(
  803. * pipe(
  804. * some(1),
  805. * toNullable
  806. * ),
  807. * 1
  808. * )
  809. * assert.strictEqual(
  810. * pipe(
  811. * none,
  812. * toNullable
  813. * ),
  814. * null
  815. * )
  816. *
  817. * @category conversions
  818. * @since 2.0.0
  819. */
  820. export declare const toNullable: <A>(ma: Option<A>) => A | null
  821. /**
  822. * Extracts the value out of the structure, if it exists. Otherwise returns `undefined`.
  823. *
  824. * @example
  825. * import { some, none, toUndefined } from 'fp-ts/Option'
  826. * import { pipe } from 'fp-ts/function'
  827. *
  828. * assert.strictEqual(
  829. * pipe(
  830. * some(1),
  831. * toUndefined
  832. * ),
  833. * 1
  834. * )
  835. * assert.strictEqual(
  836. * pipe(
  837. * none,
  838. * toUndefined
  839. * ),
  840. * undefined
  841. * )
  842. *
  843. * @category conversions
  844. * @since 2.0.0
  845. */
  846. export declare const toUndefined: <A>(ma: Option<A>) => A | undefined
  847. /**
  848. * Returns `true` if `ma` contains `a`
  849. *
  850. * @example
  851. * import { some, none, elem } from 'fp-ts/Option'
  852. * import { pipe } from 'fp-ts/function'
  853. * import * as N from 'fp-ts/number'
  854. *
  855. * assert.strictEqual(pipe(some(1), elem(N.Eq)(1)), true)
  856. * assert.strictEqual(pipe(some(1), elem(N.Eq)(2)), false)
  857. * assert.strictEqual(pipe(none, elem(N.Eq)(1)), false)
  858. *
  859. * @since 2.0.0
  860. */
  861. export declare function elem<A>(E: Eq<A>): {
  862. (a: A): (ma: Option<A>) => boolean
  863. (a: A, ma: Option<A>): boolean
  864. }
  865. /**
  866. * Returns `true` if the predicate is satisfied by the wrapped value
  867. *
  868. * @example
  869. * import { some, none, exists } from 'fp-ts/Option'
  870. * import { pipe } from 'fp-ts/function'
  871. *
  872. * assert.strictEqual(
  873. * pipe(
  874. * some(1),
  875. * exists(n => n > 0)
  876. * ),
  877. * true
  878. * )
  879. * assert.strictEqual(
  880. * pipe(
  881. * some(1),
  882. * exists(n => n > 1)
  883. * ),
  884. * false
  885. * )
  886. * assert.strictEqual(
  887. * pipe(
  888. * none,
  889. * exists(n => n > 0)
  890. * ),
  891. * false
  892. * )
  893. *
  894. * @since 2.0.0
  895. */
  896. export declare const exists: <A>(predicate: Predicate<A>) => (ma: Option<A>) => boolean
  897. /**
  898. * @category do notation
  899. * @since 2.9.0
  900. */
  901. export declare const Do: Option<{}>
  902. /**
  903. * @category do notation
  904. * @since 2.8.0
  905. */
  906. export declare const bindTo: <N extends string>(name: N) => <A>(fa: Option<A>) => Option<{ readonly [K in N]: A }>
  907. declare const let_: <N extends string, A, B>(
  908. name: Exclude<N, keyof A>,
  909. f: (a: A) => B
  910. ) => (fa: Option<A>) => Option<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  911. export {
  912. /**
  913. * @category do notation
  914. * @since 2.13.0
  915. */
  916. let_ as let
  917. }
  918. /**
  919. * @category do notation
  920. * @since 2.8.0
  921. */
  922. export declare const bind: <N extends string, A, B>(
  923. name: Exclude<N, keyof A>,
  924. f: (a: A) => Option<B>
  925. ) => (ma: Option<A>) => Option<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  926. /**
  927. * @category do notation
  928. * @since 2.8.0
  929. */
  930. export declare const apS: <N extends string, A, B>(
  931. name: Exclude<N, keyof A>,
  932. fb: Option<B>
  933. ) => (fa: Option<A>) => Option<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  934. /**
  935. * @since 2.11.0
  936. */
  937. export declare const ApT: Option<readonly []>
  938. /**
  939. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
  940. *
  941. * @category traversing
  942. * @since 2.11.0
  943. */
  944. export declare const traverseReadonlyNonEmptyArrayWithIndex: <A, B>(
  945. f: (index: number, a: A) => Option<B>
  946. ) => (as: ReadonlyNonEmptyArray<A>) => Option<ReadonlyNonEmptyArray<B>>
  947. /**
  948. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  949. *
  950. * @category traversing
  951. * @since 2.11.0
  952. */
  953. export declare const traverseReadonlyArrayWithIndex: <A, B>(
  954. f: (index: number, a: A) => Option<B>
  955. ) => (as: readonly A[]) => Option<readonly B[]>
  956. /**
  957. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  958. *
  959. * @category traversing
  960. * @since 2.9.0
  961. */
  962. export declare const traverseArrayWithIndex: <A, B>(
  963. f: (index: number, a: A) => Option<B>
  964. ) => (as: ReadonlyArray<A>) => Option<ReadonlyArray<B>>
  965. /**
  966. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  967. *
  968. * @category traversing
  969. * @since 2.9.0
  970. */
  971. export declare const traverseArray: <A, B>(f: (a: A) => Option<B>) => (as: readonly A[]) => Option<readonly B[]>
  972. /**
  973. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  974. *
  975. * @category traversing
  976. * @since 2.9.0
  977. */
  978. export declare const sequenceArray: <A>(arr: ReadonlyArray<Option<A>>) => Option<ReadonlyArray<A>>
  979. /**
  980. * Use `Refinement` module instead.
  981. *
  982. * @category zone of death
  983. * @since 2.0.0
  984. * @deprecated
  985. */
  986. export declare function getRefinement<A, B extends A>(getOption: (a: A) => Option<B>): Refinement<A, B>
  987. /**
  988. * Use [`chainNullableK`](#chainnullablek) instead.
  989. *
  990. * @category zone of death
  991. * @since 2.0.0
  992. * @deprecated
  993. */
  994. export declare const mapNullable: <A, B>(f: (a: A) => B | null | undefined) => (ma: Option<A>) => Option<NonNullable<B>>
  995. /**
  996. * This instance is deprecated, use small, specific instances instead.
  997. * For example if a function needs a `Functor` instance, pass `O.Functor` instead of `O.option`
  998. * (where `O` is from `import O from 'fp-ts/Option'`)
  999. *
  1000. * @category zone of death
  1001. * @since 2.0.0
  1002. * @deprecated
  1003. */
  1004. export declare const option: Monad1<URI> &
  1005. Foldable1<URI> &
  1006. Alternative1<URI> &
  1007. Extend1<URI> &
  1008. Witherable1<URI> &
  1009. MonadThrow1<URI>
  1010. /**
  1011. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  1012. *
  1013. * @category zone of death
  1014. * @since 2.0.0
  1015. * @deprecated
  1016. */
  1017. export declare const getApplySemigroup: <A>(S: Semigroup<A>) => Semigroup<Option<A>>
  1018. /**
  1019. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  1020. *
  1021. * @category zone of death
  1022. * @since 2.0.0
  1023. * @deprecated
  1024. */
  1025. export declare const getApplyMonoid: <A>(M: Monoid<A>) => Monoid<Option<A>>
  1026. /**
  1027. * Use
  1028. *
  1029. * ```ts
  1030. * import { first } from 'fp-ts/Semigroup'
  1031. * import { getMonoid } from 'fp-ts/Option'
  1032. *
  1033. * getMonoid(first())
  1034. * ```
  1035. *
  1036. * instead.
  1037. *
  1038. * Monoid returning the left-most non-`None` value
  1039. *
  1040. * | x | y | concat(x, y) |
  1041. * | ------- | ------- | ------------ |
  1042. * | none | none | none |
  1043. * | some(a) | none | some(a) |
  1044. * | none | some(b) | some(b) |
  1045. * | some(a) | some(b) | some(a) |
  1046. *
  1047. * @example
  1048. * import { getFirstMonoid, some, none } from 'fp-ts/Option'
  1049. *
  1050. * const M = getFirstMonoid<number>()
  1051. * assert.deepStrictEqual(M.concat(none, none), none)
  1052. * assert.deepStrictEqual(M.concat(some(1), none), some(1))
  1053. * assert.deepStrictEqual(M.concat(none, some(2)), some(2))
  1054. * assert.deepStrictEqual(M.concat(some(1), some(2)), some(1))
  1055. *
  1056. * @category zone of death
  1057. * @since 2.0.0
  1058. * @deprecated
  1059. */
  1060. export declare const getFirstMonoid: <A = never>() => Monoid<Option<A>>
  1061. /**
  1062. * Use
  1063. *
  1064. * ```ts
  1065. * import { last } from 'fp-ts/Semigroup'
  1066. * import { getMonoid } from 'fp-ts/Option'
  1067. *
  1068. * getMonoid(last())
  1069. * ```
  1070. *
  1071. * instead.
  1072. *
  1073. * Monoid returning the right-most non-`None` value
  1074. *
  1075. * | x | y | concat(x, y) |
  1076. * | ------- | ------- | ------------ |
  1077. * | none | none | none |
  1078. * | some(a) | none | some(a) |
  1079. * | none | some(b) | some(b) |
  1080. * | some(a) | some(b) | some(b) |
  1081. *
  1082. * @example
  1083. * import { getLastMonoid, some, none } from 'fp-ts/Option'
  1084. *
  1085. * const M = getLastMonoid<number>()
  1086. * assert.deepStrictEqual(M.concat(none, none), none)
  1087. * assert.deepStrictEqual(M.concat(some(1), none), some(1))
  1088. * assert.deepStrictEqual(M.concat(none, some(2)), some(2))
  1089. * assert.deepStrictEqual(M.concat(some(1), some(2)), some(2))
  1090. *
  1091. * @category zone of death
  1092. * @since 2.0.0
  1093. * @deprecated
  1094. */
  1095. export declare const getLastMonoid: <A = never>() => Monoid<Option<A>>