版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FromEither.js 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * The `FromEither` type class represents those data types which support errors.
  3. *
  4. * @since 2.10.0
  5. */
  6. import { chainFirst } from './Chain';
  7. import { flow } from './function';
  8. import * as _ from './internal';
  9. export function fromOption(F) {
  10. return function (onNone) { return function (ma) { return F.fromEither(_.isNone(ma) ? _.left(onNone()) : _.right(ma.value)); }; };
  11. }
  12. export function fromPredicate(F) {
  13. return function (predicate, onFalse) {
  14. return function (a) {
  15. return F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a)));
  16. };
  17. };
  18. }
  19. export function fromOptionK(F) {
  20. var fromOptionF = fromOption(F);
  21. return function (onNone) {
  22. var from = fromOptionF(onNone);
  23. return function (f) { return flow(f, from); };
  24. };
  25. }
  26. export function chainOptionK(F, M) {
  27. var fromOptionKF = fromOptionK(F);
  28. return function (onNone) {
  29. var from = fromOptionKF(onNone);
  30. return function (f) { return function (ma) { return M.chain(ma, from(f)); }; };
  31. };
  32. }
  33. export function fromEitherK(F) {
  34. return function (f) { return flow(f, F.fromEither); };
  35. }
  36. export function chainEitherK(F, M) {
  37. var fromEitherKF = fromEitherK(F);
  38. return function (f) { return function (ma) { return M.chain(ma, fromEitherKF(f)); }; };
  39. }
  40. export function chainFirstEitherK(F, M) {
  41. return flow(fromEitherK(F), chainFirst(M));
  42. }
  43. export function filterOrElse(F, M) {
  44. return function (predicate, onFalse) {
  45. return function (ma) {
  46. return M.chain(ma, function (a) { return F.fromEither(predicate(a) ? _.right(a) : _.left(onFalse(a))); });
  47. };
  48. };
  49. }