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

1267 строки
34 KiB

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